diff --git a/src/test/java/option/OptionalExample.java b/src/test/java/option/OptionalExample.java index db18993..b2eb705 100644 --- a/src/test/java/option/OptionalExample.java +++ b/src/test/java/option/OptionalExample.java @@ -1,15 +1,26 @@ package option; + import org.junit.Test; import java.util.Optional; import java.util.concurrent.ThreadLocalRandom; +import java.util.function.BiFunction; import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; import static org.junit.Assert.assertEquals; public class OptionalExample { + //не используя не final переменные + //не используя isPresent() и get() + //использовать map() и flatMap() + public static Optional zipMap(Optional o1, Optional o2, BiFunction f){ + throw new UnsupportedOperationException(); + } + @Test public void get() { final Optional o1 = Optional.empty(); @@ -21,6 +32,8 @@ public void get() { o1.orElseThrow(() -> new UnsupportedOperationException()); } + + @Test public void ifPresent() { final Optional o1 = getOptional(); @@ -50,6 +63,102 @@ public void map() { assertEquals(expected, actual); } + @Test + public void filter() { + final Optional o1 = getOptional(); + + final Predicate isEmpty = String::isEmpty; + + final Optional expected = o1.filter(isEmpty); + + final Optional actual; + if (o1.isPresent()) { + if(isEmpty.test(o1.get())){ + actual = Optional.empty(); + } + else{ + actual = Optional.of(o1.get()); + } + } else { + actual = Optional.empty(); + } + assertEquals(expected, actual); + } + + + + @Test + public void flatMap() { + + final Optional o1 = getOptional(); + + final Function> getLength = s -> Optional.of(s.length()); + + final Optional expected = o1.flatMap(getLength); + + final Optional actual; + if (o1.isPresent()) { + actual = getLength.apply(o1.get()); + } else { + actual = Optional.empty(); + } + assertEquals(expected, actual); + } + + @Test + public void orElse() { + + final Optional o1 = getOptional(); + + final String aNull = "null"; + final Optional expected = Optional.of(o1.orElse(aNull)); + + final Optional actual; + if (o1.isPresent()) { + actual = Optional.of(o1.get()); + } else { + actual = Optional.of(aNull); + } + assertEquals(expected, actual); + } + @Test + public void orElseGet() { + + final Optional o1 = getOptional(); + final Supplier s = () -> "null"; + + final Optional expected = Optional.of(o1.orElseGet(s)); + + final Optional actual; + if (o1.isPresent()) { + actual = Optional.of(o1.get()); + } else { + actual = Optional.of(s.get()); + } + assertEquals(expected, actual); + } + @Test + public void orElseThrow() { + + final Optional o1 = getOptional(); + final String expectedMessage = "Exception was thrown"; + final Supplier ex =() -> new UnsupportedOperationException("Exception was thrown"); + + final Optional actual; + try{ + final Optional expected = Optional.of(o1.orElseThrow(ex)); + if (o1.isPresent()) { + actual = Optional.of(o1.get()); + assertEquals(expected, actual); + } + } + catch (UnsupportedOperationException e){ + System.out.println(e.getMessage()); + assertEquals(expectedMessage, e.getMessage()); + } + + } + private Optional getOptional() { return ThreadLocalRandom.current().nextBoolean() ? Optional.empty()