Skip to content

Commit

Permalink
optional examples (eugenp#2537)
Browse files Browse the repository at this point in the history
  • Loading branch information
lor6 authored and Eugen committed Aug 31, 2017
1 parent 2f3e364 commit 49acba2
Show file tree
Hide file tree
Showing 13 changed files with 583 additions and 0 deletions.
27 changes: 27 additions & 0 deletions guest/core-java-9/pom.xml
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 guest/core-java-9/src/main/java/com/stackify/optional/User.java
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;
}
}
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());
}

}
36 changes: 36 additions & 0 deletions guest/core-java/pom.xml
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 guest/core-java/src/main/java/com/stackify/optional/Address.java
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 guest/core-java/src/main/java/com/stackify/optional/Country.java
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 guest/core-java/src/main/java/com/stackify/optional/User.java
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;
}

}
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;
}
}
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;
}


}
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;
}

}
13 changes: 13 additions & 0 deletions guest/core-java/src/main/resources/log4j2.xml
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>
Loading

0 comments on commit 49acba2

Please sign in to comment.