From f89a00fa217bf532390c36c260a75712a99c6315 Mon Sep 17 00:00:00 2001 From: Daniel Hoeggerl Date: Fri, 16 Dec 2016 13:30:05 +0100 Subject: [PATCH 1/3] add Optional.stream() --- .../main/java/solid/optional/Optional.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/streams/src/main/java/solid/optional/Optional.java b/streams/src/main/java/solid/optional/Optional.java index 5184be6..8c06389 100644 --- a/streams/src/main/java/solid/optional/Optional.java +++ b/streams/src/main/java/solid/optional/Optional.java @@ -3,6 +3,7 @@ import solid.functions.Action1; import solid.functions.Func0; import solid.functions.Func1; +import solid.stream.Stream; /** * Optional is a wrapper around a value that can be empty (null). @@ -84,6 +85,24 @@ public Optional map(Func1 func1) { return value == null ? Optional.empty() : Optional.of(func1.call(value)); } + /** + * If a value is present return a sequential {@link Stream} containing only + * that value, otherwise return an empty {@code Stream}. + * + * @return the optional value as a {@code Stream} + */ + public Stream stream() { + if (!isPresent()) { + return Stream.of(); + } else { + if (value instanceof Iterable) { + return Stream.stream((Iterable) value); + } else { + return Stream.of(value); + } + } + } + @Override public boolean equals(Object o) { if (this == o) return true; From 7593e900e35646a02f908aded4e16977cb29507d Mon Sep 17 00:00:00 2001 From: Daniel Hoeggerl Date: Mon, 19 Dec 2016 11:46:56 +0100 Subject: [PATCH 2/3] update Optional#stream to only use Stream#of --- streams/src/main/java/solid/optional/Optional.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/streams/src/main/java/solid/optional/Optional.java b/streams/src/main/java/solid/optional/Optional.java index 8c06389..1b099df 100644 --- a/streams/src/main/java/solid/optional/Optional.java +++ b/streams/src/main/java/solid/optional/Optional.java @@ -92,15 +92,7 @@ public Optional map(Func1 func1) { * @return the optional value as a {@code Stream} */ public Stream stream() { - if (!isPresent()) { - return Stream.of(); - } else { - if (value instanceof Iterable) { - return Stream.stream((Iterable) value); - } else { - return Stream.of(value); - } - } + return value == null ? Stream.of() : Stream.of(value); } @Override From 33c25f782a2a26a67438e16f5f93e2717974ab45 Mon Sep 17 00:00:00 2001 From: Daniel Hoeggerl Date: Mon, 19 Dec 2016 11:47:13 +0100 Subject: [PATCH 3/3] add tests for Optional#stream --- .../java/solid/optional/OptionalTest.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/streams/src/test/java/solid/optional/OptionalTest.java b/streams/src/test/java/solid/optional/OptionalTest.java index 8f14a0b..74df842 100644 --- a/streams/src/test/java/solid/optional/OptionalTest.java +++ b/streams/src/test/java/solid/optional/OptionalTest.java @@ -2,12 +2,17 @@ import org.junit.Test; +import java.util.Arrays; +import java.util.List; + import solid.functions.Action1; import solid.functions.Func0; import solid.functions.Func1; +import solid.stream.Stream; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.eq; @@ -15,6 +20,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static test_utils.AssertIterableEquals.assertIterableEquals; public class OptionalTest { @@ -31,7 +37,9 @@ public void testNull() throws Exception { assertEquals(1, o.or(1)); assertEquals(1, o.or(new Func0() { @Override - public Object call() {return 1;} + public Object call() { + return 1; + } })); assertNull(o.orNull()); assertNull(o.map(null).orNull()); @@ -77,4 +85,26 @@ public String call(Integer value) { assertEquals((Integer) 1, o.get()); } + + @Test + public void testStream() throws Exception { + Optional> o = Optional.of(Arrays.asList(1, 2, 3, 4)); + assertNotNull(o.stream()); + assertTrue(o.stream().first().isPresent()); + + Stream s = o.stream().flatMap(new Func1, Iterable>() { + @Override + public Iterable call(List value) { + return value; + } + }); + assertEquals(Integer.valueOf(1), s.first().get()); + assertEquals(Integer.valueOf(4), s.last().get()); + + assertEquals(Optional.empty().stream(), Stream.of()); + + assertIterableEquals(s, Arrays.asList(1, 2, 3, 4)); + + assertEquals(Optional.of("Test").stream().first().get(), "Test"); + } } \ No newline at end of file