Skip to content

Commit 49acba2

Browse files
lor6Eugen
authored andcommitted
optional examples (eugenp#2537)
1 parent 2f3e364 commit 49acba2

13 files changed

Lines changed: 583 additions & 0 deletions

File tree

guest/core-java-9/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.stackify</groupId>
4+
<artifactId>core-java-9</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
7+
<dependencies>
8+
<dependency>
9+
<groupId>junit</groupId>
10+
<artifactId>junit</artifactId>
11+
<version>4.12</version>
12+
</dependency>
13+
</dependencies>
14+
15+
<build>
16+
<plugins>
17+
<plugin>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.6.2</version>
20+
<configuration>
21+
<source>1.9</source>
22+
<target>1.9</target>
23+
</configuration>
24+
</plugin>
25+
</plugins>
26+
</build>
27+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.stackify.optional;
2+
3+
public class User {
4+
private String email;
5+
private String password;
6+
7+
public User(String email, String password) {
8+
super();
9+
this.email = email;
10+
this.password = password;
11+
}
12+
13+
public String getEmail() {
14+
return email;
15+
}
16+
17+
public void setEmail(String email) {
18+
this.email = email;
19+
}
20+
21+
public String getPassword() {
22+
return password;
23+
}
24+
25+
public void setPassword(String password) {
26+
this.password = password;
27+
}
28+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.stackify.optional;
2+
3+
import org.junit.Test;
4+
import java.util.Optional;
5+
import java.util.List;
6+
7+
import static org.junit.Assert.*;
8+
import java.util.stream.Collectors;
9+
10+
public class OptionalTest {
11+
12+
private User user;
13+
14+
@Test
15+
public void whenEmptyOptional_thenGetValueFromOr() {
16+
User result = Optional.ofNullable(user)
17+
.or( () -> Optional.of(new User("default","1234"))).get();
18+
19+
assertEquals(result.getEmail(), "default");
20+
}
21+
22+
@Test
23+
public void whenIfPresentOrElse_thenOk() {
24+
Optional.ofNullable(user)
25+
.ifPresentOrElse( u -> System.out.println("User is:" + u.getEmail()), () -> System.out.println("User not found"));
26+
}
27+
28+
@Test
29+
public void whenGetStream_thenOk() {
30+
User user = new User("[email protected]", "1234");
31+
List<String> emails = Optional.ofNullable(user)
32+
.stream()
33+
.filter(u -> u.getEmail() != null && u.getEmail().contains("@"))
34+
.map( u -> u.getEmail())
35+
.collect(Collectors.toList());
36+
37+
assertTrue(emails.size() == 1);
38+
assertEquals(emails.get(0), user.getEmail());
39+
}
40+
41+
}

guest/core-java/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.stackify</groupId>
5+
<artifactId>core-java</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<dependencies>
9+
<dependency>
10+
<groupId>junit</groupId>
11+
<artifactId>junit</artifactId>
12+
<version>4.12</version>
13+
</dependency>
14+
<dependency>
15+
<groupId>org.apache.logging.log4j</groupId>
16+
<artifactId>log4j-core</artifactId>
17+
<version>${log4j2.version}</version>
18+
</dependency>
19+
</dependencies>
20+
21+
<build>
22+
<plugins>
23+
<plugin>
24+
<artifactId>maven-compiler-plugin</artifactId>
25+
<version>3.5</version>
26+
<configuration>
27+
<source>1.8</source>
28+
<target>1.8</target>
29+
</configuration>
30+
</plugin>
31+
</plugins>
32+
</build>
33+
<properties>
34+
<log4j2.version>2.8.2</log4j2.version>
35+
</properties>
36+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.stackify.optional;
2+
3+
public class Address {
4+
private String addressLine;
5+
private String city;
6+
private Country country;
7+
8+
public Address(String addressLine, String city, Country country) {
9+
super();
10+
this.addressLine = addressLine;
11+
this.city = city;
12+
this.country = country;
13+
}
14+
15+
public String getAddressLine() {
16+
return addressLine;
17+
}
18+
19+
public void setAddressLine(String addressLine) {
20+
this.addressLine = addressLine;
21+
}
22+
23+
public String getCity() {
24+
return city;
25+
}
26+
27+
public void setCity(String city) {
28+
this.city = city;
29+
}
30+
31+
public Country getCountry() {
32+
return country;
33+
}
34+
35+
public void setCountry(Country country) {
36+
this.country = country;
37+
}
38+
39+
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.stackify.optional;
2+
3+
public class Country {
4+
private String name;
5+
private String isocode;
6+
7+
public Country(String name, String isocode) {
8+
super();
9+
this.name = name;
10+
this.isocode = isocode;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public String getIsocode() {
22+
return isocode;
23+
}
24+
25+
public void setIsocode(String isocode) {
26+
this.isocode = isocode;
27+
}
28+
29+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.stackify.optional;
2+
3+
import java.util.Optional;
4+
5+
public class User {
6+
private String email;
7+
private String password;
8+
9+
private Address address;
10+
11+
private String position;
12+
13+
public User(String email, String password) {
14+
super();
15+
this.email = email;
16+
this.password = password;
17+
}
18+
19+
public String getEmail() {
20+
return email;
21+
}
22+
23+
public void setEmail(String email) {
24+
this.email = email;
25+
}
26+
27+
public String getPassword() {
28+
return password;
29+
}
30+
31+
public void setPassword(String password) {
32+
this.password = password;
33+
}
34+
35+
public Address getAddress() {
36+
return address;
37+
}
38+
39+
public void setAddress(Address address) {
40+
this.address = address;
41+
}
42+
43+
public Optional<String> getPosition() {
44+
return Optional.ofNullable(position);
45+
}
46+
47+
public void setPosition(String position) {
48+
this.position = position;
49+
}
50+
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.stackify.optional.chaining;
2+
3+
import java.util.Optional;
4+
5+
public class Address {
6+
private String addressLine;
7+
private String city;
8+
private Country country;
9+
10+
public Address(String addressLine, String city) {
11+
this.addressLine = addressLine;
12+
this.city = city;
13+
}
14+
15+
public String getAddressLine() {
16+
return addressLine;
17+
}
18+
19+
public void setAddressLine(String addressLine) {
20+
this.addressLine = addressLine;
21+
}
22+
23+
public String getCity() {
24+
return city;
25+
}
26+
27+
public void setCity(String city) {
28+
this.city = city;
29+
}
30+
31+
public Optional<Country> getCountry() {
32+
return Optional.ofNullable(country);
33+
}
34+
35+
public void setCountry(Country country) {
36+
this.country = country;
37+
}
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.stackify.optional.chaining;
2+
3+
public class Country {
4+
private String name;
5+
private String isocode;
6+
7+
public Country(String name) {
8+
this.name = name;
9+
}
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
public void setName(String name) {
16+
this.name = name;
17+
}
18+
19+
public String getIsocode() {
20+
return isocode;
21+
}
22+
23+
public void setIsocode(String isocode) {
24+
this.isocode = isocode;
25+
}
26+
27+
28+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.stackify.optional.chaining;
2+
3+
import java.util.Optional;
4+
5+
public class User {
6+
private String email;
7+
private String password;
8+
9+
private Address address;
10+
11+
private String position;
12+
13+
public User(String email, String password) {
14+
this.email = email;
15+
this.password = password;
16+
}
17+
18+
public String getEmail() {
19+
return email;
20+
}
21+
22+
public void setEmail(String email) {
23+
this.email = email;
24+
}
25+
26+
public String getPassword() {
27+
return password;
28+
}
29+
30+
public void setPassword(String password) {
31+
this.password = password;
32+
}
33+
34+
public Optional<Address> getAddress() {
35+
return Optional.ofNullable(address);
36+
}
37+
38+
public void setAddress(Address address) {
39+
this.address = address;
40+
}
41+
42+
public Optional<String> getPosition() {
43+
return Optional.ofNullable(position);
44+
}
45+
46+
public void setPosition(String position) {
47+
this.position = position;
48+
}
49+
50+
}

0 commit comments

Comments
 (0)