optional examples (#2537)
This commit is contained in:
parent
2f3e36405a
commit
49acba206c
|
@ -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>
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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("john@gmail.com", "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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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>
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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>
|
|
@ -0,0 +1,166 @@
|
|||
package com.stackify.optional;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.Test;
|
||||
|
||||
public class OptionalTest {
|
||||
|
||||
private User user;
|
||||
|
||||
private Logger logger = LogManager.getLogger(OptionalTest.class);
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void testNull() {
|
||||
String isocode = user.getAddress().getCountry().getIsocode().toUpperCase();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
if (user != null) {
|
||||
Address address = user.getAddress();
|
||||
if (address != null) {
|
||||
Country country = address.getCountry();
|
||||
if (country != null) {
|
||||
String isocode = country.getIsocode();
|
||||
if (isocode != null) {
|
||||
isocode = isocode.toUpperCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void whenCreateEmptyOptional_thenNull() {
|
||||
Optional<User> emptyOpt = Optional.empty();
|
||||
emptyOpt.get();
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void whenCreateOfEmptyOptional_thenNullPointerException() {
|
||||
Optional<User> opt = Optional.of(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateOfNullableOptional_thenOk() {
|
||||
String name = "John";
|
||||
Optional<String> opt = Optional.ofNullable(name);
|
||||
assertEquals("John", opt.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckIsPresent_thenOk() {
|
||||
user = new User("john@gmail.com", "1234");
|
||||
Optional<User> opt = Optional.ofNullable(user);
|
||||
assertTrue(opt.isPresent());
|
||||
|
||||
assertEquals(user.getEmail(), opt.get().getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckIfPresent_thenOk() {
|
||||
user = new User("john@gmail.com", "1234");
|
||||
Optional<User> opt = Optional.ofNullable(user);
|
||||
assertTrue(opt.isPresent());
|
||||
|
||||
opt.ifPresent(u -> assertEquals(user.getEmail(), u.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEmptyValue_thenReturnDefault() {
|
||||
User user = null;
|
||||
User user2 = new User("anna@gmail.com", "1234");
|
||||
User result = Optional.ofNullable(user).orElse(user2);
|
||||
|
||||
assertEquals("anna@gmail.com", result.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValueNotNull_thenIgnoreDefault() {
|
||||
User user = new User("john@gmail.com", "1234");
|
||||
User user2 = new User("anna@gmail.com", "1234");
|
||||
User result = Optional.ofNullable(user).orElse(user2);
|
||||
|
||||
assertEquals("john@gmail.com", result.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSetDefaultOrElseGet_thenOk() {
|
||||
User user = null;
|
||||
User user2 = new User("anna@gmail.com", "1234");
|
||||
User result = Optional.ofNullable(user).orElseGet(() -> user2);
|
||||
|
||||
assertEquals("anna@gmail.com", result.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPresentValue_whenCompare_thenOk() {
|
||||
User user = new User("john@gmail.com", "1234");
|
||||
logger.info("Using orElse");
|
||||
User result = Optional.ofNullable(user).orElse(createNewUser());
|
||||
logger.info("Using orElseGet");
|
||||
User result2 = Optional.ofNullable(user).orElseGet(() -> createNewUser());
|
||||
}
|
||||
|
||||
private User createNewUser() {
|
||||
logger.info("Creating New User");
|
||||
return new User("extra@gmail.com", "1234");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyValue_whenCompare_thenOk() {
|
||||
User user = null;
|
||||
logger.info("Using orElse");
|
||||
User result = Optional.ofNullable(user).orElse(createNewUser());
|
||||
logger.info("Using orElseGet");
|
||||
User result2 = Optional.ofNullable(user).orElseGet(() -> createNewUser());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void whenThrowException_thenOk() {
|
||||
User result = Optional.ofNullable(user).orElseThrow(() -> new IllegalArgumentException());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMap_thenOk() {
|
||||
user = new User("anna@gmail.com", "1234");
|
||||
String email = Optional.ofNullable(user).map(u -> u.getEmail()).orElse("default@gmail.com");
|
||||
assertEquals(email, user.getEmail());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFlatMap_thenOk() {
|
||||
user = new User("anna@gmail.com", "1234");
|
||||
user.setPosition("Developer");
|
||||
String position = Optional.ofNullable(user).flatMap(u -> u.getPosition()).orElse("default");
|
||||
assertEquals(position, user.getPosition().get());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilter_thenOk() {
|
||||
user = new User("anna@gmail.com", "1234");
|
||||
Optional<User> result = Optional.ofNullable(user).filter(u -> u.getEmail() != null && u.getEmail().contains("@"));
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStream_thenOk() {
|
||||
List<User> users = new ArrayList<>();
|
||||
User user = users.stream().findFirst().orElse(new User("default", "1234"));
|
||||
assertEquals(user.getEmail(), "default");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.stackify.optional.chaining;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class OptionalChainingTest {
|
||||
|
||||
@Test
|
||||
public void whenChaining_thenOk() {
|
||||
User user = new User("anna@gmail.com", "1234");
|
||||
|
||||
String result = Optional.ofNullable(user)
|
||||
.flatMap(u -> u.getAddress())
|
||||
.flatMap(a -> a.getCountry())
|
||||
.map(c -> c.getIsocode())
|
||||
.orElse("default");
|
||||
|
||||
assertEquals(result, "default");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChainingWithMethodReferences_thenOk() {
|
||||
User user = new User("anna@gmail.com", "1234");
|
||||
|
||||
String result = Optional.ofNullable(user)
|
||||
.flatMap(User::getAddress)
|
||||
.flatMap(Address::getCountry)
|
||||
.map(Country::getIsocode)
|
||||
.orElse("default");
|
||||
|
||||
assertEquals(result, "default");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue