optional examples (#2537)
This commit is contained in:
27
guest/core-java-9/pom.xml
Normal file
27
guest/core-java-9/pom.xml
Normal file
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user