forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
583 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.stackify</groupId> | ||
<artifactId>core-java-9</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.6.2</version> | ||
<configuration> | ||
<source>1.9</source> | ||
<target>1.9</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
28 changes: 28 additions & 0 deletions
28
guest/core-java-9/src/main/java/com/stackify/optional/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.stackify.optional; | ||
|
||
public class User { | ||
private String email; | ||
private String password; | ||
|
||
public User(String email, String password) { | ||
super(); | ||
this.email = email; | ||
this.password = password; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
guest/core-java-9/src/test/java/com/stackify/optional/OptionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.stackify.optional; | ||
|
||
import org.junit.Test; | ||
import java.util.Optional; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
import java.util.stream.Collectors; | ||
|
||
public class OptionalTest { | ||
|
||
private User user; | ||
|
||
@Test | ||
public void whenEmptyOptional_thenGetValueFromOr() { | ||
User result = Optional.ofNullable(user) | ||
.or( () -> Optional.of(new User("default","1234"))).get(); | ||
|
||
assertEquals(result.getEmail(), "default"); | ||
} | ||
|
||
@Test | ||
public void whenIfPresentOrElse_thenOk() { | ||
Optional.ofNullable(user) | ||
.ifPresentOrElse( u -> System.out.println("User is:" + u.getEmail()), () -> System.out.println("User not found")); | ||
} | ||
|
||
@Test | ||
public void whenGetStream_thenOk() { | ||
User user = new User("[email protected]", "1234"); | ||
List<String> emails = Optional.ofNullable(user) | ||
.stream() | ||
.filter(u -> u.getEmail() != null && u.getEmail().contains("@")) | ||
.map( u -> u.getEmail()) | ||
.collect(Collectors.toList()); | ||
|
||
assertTrue(emails.size() == 1); | ||
assertEquals(emails.get(0), user.getEmail()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.stackify</groupId> | ||
<artifactId>core-java</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<version>${log4j2.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.5</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<properties> | ||
<log4j2.version>2.8.2</log4j2.version> | ||
</properties> | ||
</project> |
40 changes: 40 additions & 0 deletions
40
guest/core-java/src/main/java/com/stackify/optional/Address.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.stackify.optional; | ||
|
||
public class Address { | ||
private String addressLine; | ||
private String city; | ||
private Country country; | ||
|
||
public Address(String addressLine, String city, Country country) { | ||
super(); | ||
this.addressLine = addressLine; | ||
this.city = city; | ||
this.country = country; | ||
} | ||
|
||
public String getAddressLine() { | ||
return addressLine; | ||
} | ||
|
||
public void setAddressLine(String addressLine) { | ||
this.addressLine = addressLine; | ||
} | ||
|
||
public String getCity() { | ||
return city; | ||
} | ||
|
||
public void setCity(String city) { | ||
this.city = city; | ||
} | ||
|
||
public Country getCountry() { | ||
return country; | ||
} | ||
|
||
public void setCountry(Country country) { | ||
this.country = country; | ||
} | ||
|
||
|
||
} |
29 changes: 29 additions & 0 deletions
29
guest/core-java/src/main/java/com/stackify/optional/Country.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.stackify.optional; | ||
|
||
public class Country { | ||
private String name; | ||
private String isocode; | ||
|
||
public Country(String name, String isocode) { | ||
super(); | ||
this.name = name; | ||
this.isocode = isocode; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getIsocode() { | ||
return isocode; | ||
} | ||
|
||
public void setIsocode(String isocode) { | ||
this.isocode = isocode; | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
guest/core-java/src/main/java/com/stackify/optional/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.stackify.optional; | ||
|
||
import java.util.Optional; | ||
|
||
public class User { | ||
private String email; | ||
private String password; | ||
|
||
private Address address; | ||
|
||
private String position; | ||
|
||
public User(String email, String password) { | ||
super(); | ||
this.email = email; | ||
this.password = password; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public Address getAddress() { | ||
return address; | ||
} | ||
|
||
public void setAddress(Address address) { | ||
this.address = address; | ||
} | ||
|
||
public Optional<String> getPosition() { | ||
return Optional.ofNullable(position); | ||
} | ||
|
||
public void setPosition(String position) { | ||
this.position = position; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
guest/core-java/src/main/java/com/stackify/optional/chaining/Address.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.stackify.optional.chaining; | ||
|
||
import java.util.Optional; | ||
|
||
public class Address { | ||
private String addressLine; | ||
private String city; | ||
private Country country; | ||
|
||
public Address(String addressLine, String city) { | ||
this.addressLine = addressLine; | ||
this.city = city; | ||
} | ||
|
||
public String getAddressLine() { | ||
return addressLine; | ||
} | ||
|
||
public void setAddressLine(String addressLine) { | ||
this.addressLine = addressLine; | ||
} | ||
|
||
public String getCity() { | ||
return city; | ||
} | ||
|
||
public void setCity(String city) { | ||
this.city = city; | ||
} | ||
|
||
public Optional<Country> getCountry() { | ||
return Optional.ofNullable(country); | ||
} | ||
|
||
public void setCountry(Country country) { | ||
this.country = country; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
guest/core-java/src/main/java/com/stackify/optional/chaining/Country.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.stackify.optional.chaining; | ||
|
||
public class Country { | ||
private String name; | ||
private String isocode; | ||
|
||
public Country(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getIsocode() { | ||
return isocode; | ||
} | ||
|
||
public void setIsocode(String isocode) { | ||
this.isocode = isocode; | ||
} | ||
|
||
|
||
} |
50 changes: 50 additions & 0 deletions
50
guest/core-java/src/main/java/com/stackify/optional/chaining/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.stackify.optional.chaining; | ||
|
||
import java.util.Optional; | ||
|
||
public class User { | ||
private String email; | ||
private String password; | ||
|
||
private Address address; | ||
|
||
private String position; | ||
|
||
public User(String email, String password) { | ||
this.email = email; | ||
this.password = password; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public Optional<Address> getAddress() { | ||
return Optional.ofNullable(address); | ||
} | ||
|
||
public void setAddress(Address address) { | ||
this.address = address; | ||
} | ||
|
||
public Optional<String> getPosition() { | ||
return Optional.ofNullable(position); | ||
} | ||
|
||
public void setPosition(String position) { | ||
this.position = position; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration status="WARN"> | ||
<Appenders> | ||
<Console name="Console" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%msg%n" /> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="info"> | ||
<AppenderRef ref="Console" /> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
Oops, something went wrong.