diff --git a/README.md b/README.md index 9e232f3..6f103d6 100644 --- a/README.md +++ b/README.md @@ -59,25 +59,36 @@ A non-final substitute for Optional with `peek()` and `stream()` methods. class Test { @Test public void maybeTests() { + // Constructors: maybe(T), just(T) and nothing() assertThat(Maybe.maybe(null)).isEqualTo(Maybe.nothing()); assertThat(Maybe.maybe(1)).isEqualTo(Maybe.just(1)); + // .orElseGet(Supplier) assertThat(Maybe.nothing().orElseGet(() -> 1)).isEqualTo(1); assertThat(Maybe.just(1).orElseGet(() -> 2)).isEqualTo(1); + // .orElse(Supplier) assertThat(Maybe.nothing().orElse(1)).isEqualTo(1); assertThat(Maybe.just(1).orElse(2)).isEqualTo(1); + // .filter(Predicate) assertThat(Maybe.just(1).filter(v -> v > 2)).isEqualTo(Maybe.nothing()); assertThat(Maybe.just(3).filter(v -> v > 2)).isEqualTo(Maybe.just(3)); assertThat(Maybe.just(1).toOptional()).isEqualTo(Optional.of(1)); assertThat(Maybe.nothing().toOptional()).isEqualTo(Optional.empty()); + // .fromOptional(Optional) is deprecated assertThat(Maybe.fromOptional(Optional.of(1))).isEqualTo(Maybe.just(1)); assertThat(Maybe.fromOptional(Optional.empty())).isEqualTo(Maybe.nothing()); + // An alternative to using .fromOptional(Optional) + assertThat(Optional.of(1).map(Maybe::just).orElse(Maybe::nothing)).isEqualTo(Maybe.just(1)); + assertThat(Optional.empty().map(Maybe::just).orElse(Maybe::nothing)).isEqualTo(Maybe.nothing()); + // .peek(Consumer) final AtomicInteger reference = new AtomicInteger(0); assertThat(Maybe.just(1).peek(reference::set)).isEqualTo(Maybe.just(1)); assertThat(reference).hasValue(1); assertThat(Maybe.nothing().peek(v -> reference.incrementAndGet())).isEqualTo(Maybe.nothing()); assertThat(reference).hasValue(1); + // .orElseThrow(Supplier) assertThatCode(() -> Maybe.just(1).orElseThrow(IllegalStateException::new)).doesNotThrowAnyException(); assertThatThrownBy(() -> Maybe.nothing().orElseThrow(IllegalStateException::new)).isInstanceOf(IllegalStateException.class); + // .stream() assertThat(Maybe.just(1).stream()).containsExactly(1); assertThat(Maybe.nothing().stream()).isEmpty(); } diff --git a/src/main/java/net/kemitix/mon/maybe/Maybe.java b/src/main/java/net/kemitix/mon/maybe/Maybe.java index 25180ba..c26ed13 100644 --- a/src/main/java/net/kemitix/mon/maybe/Maybe.java +++ b/src/main/java/net/kemitix/mon/maybe/Maybe.java @@ -83,7 +83,7 @@ public interface Maybe extends Functor>, MaybeStream, MaybeOpt * @return a Maybe * @deprecated need to find a better way of converting an Optional to a Maybe, but * without having to pass the Optional as a parameter - * Try: Optional.of(1).map(Maybe::just).orElseGet(Maybe::nothing) + * Try: Optional.of(1).map(Maybe::just).orElse(Maybe::nothing) */ @Deprecated static Maybe fromOptional(final Optional optional) {