Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions streams/src/main/java/solid/optional/Optional.java
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -84,6 +85,16 @@ public <R> Optional<R> map(Func1<T, R> func1) {
return value == null ? Optional.<R>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<T> stream() {
return value == null ? Stream.<T>of() : Stream.of(value);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
32 changes: 31 additions & 1 deletion streams/src/test/java/solid/optional/OptionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

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;
import static org.mockito.Mockito.mock;
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 {

Expand All @@ -31,7 +37,9 @@ public void testNull() throws Exception {
assertEquals(1, o.or(1));
assertEquals(1, o.or(new Func0<Object>() {
@Override
public Object call() {return 1;}
public Object call() {
return 1;
}
}));
assertNull(o.orNull());
assertNull(o.map(null).orNull());
Expand Down Expand Up @@ -77,4 +85,26 @@ public String call(Integer value) {

assertEquals((Integer) 1, o.get());
}

@Test
public void testStream() throws Exception {
Optional<List<Integer>> o = Optional.of(Arrays.asList(1, 2, 3, 4));
assertNotNull(o.stream());
assertTrue(o.stream().first().isPresent());

Stream<Integer> s = o.stream().flatMap(new Func1<List<Integer>, Iterable<Integer>>() {
@Override
public Iterable<Integer> call(List<Integer> 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");
}
}