diff --git a/CHANGELOG b/CHANGELOG index 3910f7f..dfa1644 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,6 +6,7 @@ CHANGELOG * Add `Result` * Moved `Maybe` to `net.kemitix.mon.maybe.Maybe` +* Add `Maybe.stream()` 0.6.0 ----- diff --git a/src/main/java/net/kemitix/mon/maybe/Just.java b/src/main/java/net/kemitix/mon/maybe/Just.java index cc42450..0e480f0 100644 --- a/src/main/java/net/kemitix/mon/maybe/Just.java +++ b/src/main/java/net/kemitix/mon/maybe/Just.java @@ -30,6 +30,7 @@ import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; +import java.util.stream.Stream; /** * A Maybe where a value is present. @@ -91,4 +92,9 @@ final class Just implements Maybe { public void orElseThrow(final Supplier e) { // do not throw } + + @Override + public Stream stream() { + return Stream.of(value); + } } diff --git a/src/main/java/net/kemitix/mon/maybe/Maybe.java b/src/main/java/net/kemitix/mon/maybe/Maybe.java index a313242..a3fe6eb 100644 --- a/src/main/java/net/kemitix/mon/maybe/Maybe.java +++ b/src/main/java/net/kemitix/mon/maybe/Maybe.java @@ -28,6 +28,7 @@ import java.util.Optional; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.Supplier; +import java.util.stream.Stream; /** * A value that may or may not be present. @@ -143,4 +144,10 @@ public interface Maybe extends Functor> { @SuppressWarnings("illegalthrows") void orElseThrow(Supplier e) throws Exception; + /** + * Converts the Maybe into either a single value stream or and empty stream. + * + * @return a Stream containing the value or nothing. + */ + Stream stream(); } diff --git a/src/main/java/net/kemitix/mon/maybe/Nothing.java b/src/main/java/net/kemitix/mon/maybe/Nothing.java index ded76e6..1ea6eb4 100644 --- a/src/main/java/net/kemitix/mon/maybe/Nothing.java +++ b/src/main/java/net/kemitix/mon/maybe/Nothing.java @@ -26,6 +26,7 @@ import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; +import java.util.stream.Stream; /** * A Maybe where no value is present. @@ -72,4 +73,9 @@ final class Nothing implements Maybe { public void orElseThrow(final Supplier e) throws Exception { throw e.get(); } + + @Override + public Stream stream() { + return Stream.empty(); + } } diff --git a/src/test/java/net/kemitix/mon/MaybeTest.java b/src/test/java/net/kemitix/mon/MaybeTest.java index 9382940..90ae989 100644 --- a/src/test/java/net/kemitix/mon/MaybeTest.java +++ b/src/test/java/net/kemitix/mon/MaybeTest.java @@ -1,22 +1,20 @@ package net.kemitix.mon; import net.kemitix.mon.maybe.Maybe; +import org.assertj.core.api.WithAssertions; import org.junit.Test; import java.util.Objects; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Predicate; +import java.util.stream.Stream; import static net.kemitix.mon.maybe.Maybe.just; import static net.kemitix.mon.maybe.Maybe.maybe; import static net.kemitix.mon.maybe.Maybe.nothing; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -public class MaybeTest { +public class MaybeTest implements WithAssertions { private static Predicate eq(final T value) { return v -> Objects.equals(value, v); @@ -112,4 +110,20 @@ public class MaybeTest { assertThatThrownBy(() -> nothing().orElseThrow(IllegalStateException::new)).isInstanceOf( IllegalStateException.class); } + + @Test + public void justToStream() { + //when + final Stream stream = just(1).stream(); + //then + assertThat(stream).containsExactly(1); + } + + @Test + public void nothingToStream() { + //when + final Stream stream = nothing().stream(); + //then + assertThat(stream).isEmpty(); + } }