optional orelse optional ex
This commit is contained in:
parent
146c1bb2a9
commit
ca9f1bbc42
|
@ -47,6 +47,11 @@
|
|||
<version>${awaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -89,6 +94,7 @@
|
|||
<awaitility.version>1.7.0</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
<guava.version>25.1-jre</guava.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.optionals;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Optionals {
|
||||
|
||||
public static <T> Optional<T> or(Optional<T> optional, Optional<T> fallback) {
|
||||
return optional.isPresent() ? optional : fallback;
|
||||
}
|
||||
|
||||
public static Optional<String> getName(Optional<String> name) {
|
||||
return name.or(() -> getCustomMessage());
|
||||
}
|
||||
|
||||
public static com.google.common.base.Optional<String> getOptionalGuavaName(com.google.common.base.Optional<String> name) {
|
||||
return name.or(getCustomMessageGuava());
|
||||
}
|
||||
|
||||
private static Optional<String> getCustomMessage() {
|
||||
return Optional.of("Name not provided");
|
||||
}
|
||||
|
||||
private static com.google.common.base.Optional<String> getCustomMessageGuava() {
|
||||
return com.google.common.base.Optional.of("Name not provided");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.optionals;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class OptionalsTest {
|
||||
|
||||
@Test
|
||||
public void givenOptional_whenEmptyValue_thenCustomMessage() {
|
||||
assertEquals(Optional.of("Name not provided"), Optionals.getName(Optional.ofNullable(null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOptional_whenValue_thenOptional() {
|
||||
String name = "Filan Fisteku";
|
||||
Optional<String> optionalString = Optional.ofNullable(name);
|
||||
assertEquals(optionalString, Optionals.getName(optionalString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOptional_whenValue_thenOptionalGeneralMethod() {
|
||||
String name = "Filan Fisteku";
|
||||
String missingOptional = "Name not provided";
|
||||
Optional<String> optionalString = Optional.ofNullable(name);
|
||||
Optional<String> fallbackOptionalString = Optional.ofNullable(missingOptional);
|
||||
assertEquals(optionalString, Optionals.or(optionalString, fallbackOptionalString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyOptional_whenValue_thenOptionalGeneralMethod() {
|
||||
String missingOptional = "Name not provided";
|
||||
Optional<String> optionalString = Optional.empty();
|
||||
Optional<String> fallbackOptionalString = Optional.ofNullable(missingOptional);
|
||||
assertEquals(fallbackOptionalString, Optionals.or(optionalString, fallbackOptionalString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuavaOptional_whenInvoke_thenOptional() {
|
||||
String name = "Filan Fisteku";
|
||||
com.google.common.base.Optional<String> stringOptional = com.google.common.base.Optional.of(name);
|
||||
assertEquals(stringOptional, Optionals.getOptionalGuavaName(stringOptional));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuavaOptional_whenNull_thenDefaultText() {
|
||||
assertEquals(com.google.common.base.Optional.of("Name not provided"), Optionals.getOptionalGuavaName(com.google.common.base.Optional.fromNullable(null)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue