From 016182be5eac7f036cc035946bae8dac255bf5df Mon Sep 17 00:00:00 2001 From: Sergei_Dorofeev Date: Sat, 24 Jun 2017 21:48:44 +0300 Subject: [PATCH] optional --- src/test/java/option/OptionalExample.java | 136 ++++++++++++++++++++-- 1 file changed, 125 insertions(+), 11 deletions(-) diff --git a/src/test/java/option/OptionalExample.java b/src/test/java/option/OptionalExample.java index db18993..9cf2c22 100644 --- a/src/test/java/option/OptionalExample.java +++ b/src/test/java/option/OptionalExample.java @@ -1,10 +1,10 @@ -package option; - import org.junit.Test; +import java.util.Objects; import java.util.Optional; import java.util.concurrent.ThreadLocalRandom; import java.util.function.Function; +import java.util.function.Predicate; import static org.junit.Assert.assertEquals; @@ -18,15 +18,13 @@ public void get() { o1.orElse("t"); o1.orElseGet(() -> "t"); - o1.orElseThrow(() -> new UnsupportedOperationException()); + // o1.orElseThrow(() -> new UnsupportedOperationException()); } @Test public void ifPresent() { final Optional o1 = getOptional(); - o1.ifPresent(System.out::println); - if (o1.isPresent()) { System.out.println(o1.get()); } @@ -35,12 +33,10 @@ public void ifPresent() { @Test public void map() { final Optional o1 = getOptional(); - final Function getLength = String::length; - final Optional expected = o1.map(getLength); - final Optional actual; + if (o1.isPresent()) { actual = Optional.of(getLength.apply(o1.get())); } else { @@ -50,9 +46,127 @@ public void map() { assertEquals(expected, actual); } + @Test + public void flatMap() { + final Optional o1 = getOptional(); + final Function> getLengthOpt = s -> Optional.of(s.length()); + final Optional expected = o1.flatMap(getLengthOpt); + final Optional actual; + + if (o1.isPresent()) { + actual = Optional.of(getLengthOpt.apply(o1.get()).get()); + } else { + actual = Optional.empty(); + } + + assertEquals(expected, actual); + } + + @Test + public void filter() { + final Optional o1 = getOptional(); + Predicate predicate = p -> p.startsWith("a"); + final Optional expected = o1.filter(predicate); + final Optional actual; + + if (o1.isPresent()) { + actual = predicate.test(o1.get()) ? Optional.of(o1.get()) : Optional.empty(); + } else { + actual = Optional.empty(); + } + + assertEquals(expected, actual); + } + + @Test + public void orElse() { + final Optional o1 = getOptional(); + String expected = o1.orElse("W"); + String actual; + + if (o1.isPresent()) { + actual = o1.get(); + } else { + actual = "W"; + } + assertEquals(expected, actual); + } + + @Test + public void orElseGet() { + final Optional o1 = getOptional(); + String expected = o1.orElseGet(() -> "what"); + String actual; + + if (o1.isPresent()) { + actual = o1.get(); + } else { + actual = "what"; + } + assertEquals(expected, actual); + } + + @Test + public void orElseThrow() { + final Optional o1 = getOptional(); + String expected = null; + try { + expected = o1.orElseThrow(() -> new Exception("what")); + } catch (Exception e) { + assertEquals(e.getMessage(), "what"); + return; + } + String actual; + actual = o1.get(); + + assertEquals(expected, actual); + } + + @Test + public void toStringOpt() { + final Optional o1 = getOptional(); + + String expected = o1.toString(); + String actual; + if (o1.isPresent()) { + actual = "Optional[" + o1.get() + "]"; + } else { + actual = "Optional.empty"; + } + + assertEquals(expected, actual); + } + + @Test + public void hashcode() { + final Optional o1 = getOptional(); + + Integer expected = o1.hashCode(); + Integer actual; + if (o1.isPresent()) { + actual = o1.get().hashCode(); + } else { + actual = 0; + } + + assertEquals(expected, actual); + } + + @Test + public void equalsOpt() { + final Optional o1 = getOptional(); + Optional o2 = Optional.of("abc"); + boolean expected = o1.equals(o2); + boolean actual; + if (o1.isPresent()) { + actual = o1.get().equals(o2.get()); + } else { + actual = !o2.isPresent(); + } + assertEquals(expected, actual); + } + private Optional getOptional() { - return ThreadLocalRandom.current().nextBoolean() - ? Optional.empty() - : Optional.of("abc"); + return ThreadLocalRandom.current().nextBoolean() ? Optional.empty() : Optional.of("abc"); } }