Add Maybe.stream()

This commit is contained in:
Paul Campbell 2018-06-22 23:17:25 +01:00
parent b2fa6e3dc2
commit 5bf0fcbdf9
5 changed files with 39 additions and 5 deletions

View file

@ -6,6 +6,7 @@ CHANGELOG
* Add `Result` * Add `Result`
* Moved `Maybe` to `net.kemitix.mon.maybe.Maybe` * Moved `Maybe` to `net.kemitix.mon.maybe.Maybe`
* Add `Maybe.stream()`
0.6.0 0.6.0
----- -----

View file

@ -30,6 +30,7 @@ import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Stream;
/** /**
* A Maybe where a value is present. * A Maybe where a value is present.
@ -91,4 +92,9 @@ final class Just<T> implements Maybe<T> {
public void orElseThrow(final Supplier<Exception> e) { public void orElseThrow(final Supplier<Exception> e) {
// do not throw // do not throw
} }
@Override
public Stream<T> stream() {
return Stream.of(value);
}
} }

View file

@ -28,6 +28,7 @@ import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Stream;
/** /**
* A value that may or may not be present. * A value that may or may not be present.
@ -143,4 +144,10 @@ public interface Maybe<T> extends Functor<T, Maybe<?>> {
@SuppressWarnings("illegalthrows") @SuppressWarnings("illegalthrows")
void orElseThrow(Supplier<Exception> e) throws Exception; void orElseThrow(Supplier<Exception> 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<T> stream();
} }

View file

@ -26,6 +26,7 @@ import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Stream;
/** /**
* A Maybe where no value is present. * A Maybe where no value is present.
@ -72,4 +73,9 @@ final class Nothing<T> implements Maybe<T> {
public void orElseThrow(final Supplier<Exception> e) throws Exception { public void orElseThrow(final Supplier<Exception> e) throws Exception {
throw e.get(); throw e.get();
} }
@Override
public Stream<T> stream() {
return Stream.empty();
}
} }

View file

@ -1,22 +1,20 @@
package net.kemitix.mon; package net.kemitix.mon;
import net.kemitix.mon.maybe.Maybe; import net.kemitix.mon.maybe.Maybe;
import org.assertj.core.api.WithAssertions;
import org.junit.Test; import org.junit.Test;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate; 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.just;
import static net.kemitix.mon.maybe.Maybe.maybe; import static net.kemitix.mon.maybe.Maybe.maybe;
import static net.kemitix.mon.maybe.Maybe.nothing; 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 <T> Predicate<T> eq(final T value) { private static <T> Predicate<T> eq(final T value) {
return v -> Objects.equals(value, v); return v -> Objects.equals(value, v);
@ -112,4 +110,20 @@ public class MaybeTest {
assertThatThrownBy(() -> nothing().orElseThrow(IllegalStateException::new)).isInstanceOf( assertThatThrownBy(() -> nothing().orElseThrow(IllegalStateException::new)).isInstanceOf(
IllegalStateException.class); IllegalStateException.class);
} }
@Test
public void justToStream() {
//when
final Stream<Integer> stream = just(1).stream();
//then
assertThat(stream).containsExactly(1);
}
@Test
public void nothingToStream() {
//when
final Stream<Object> stream = nothing().stream();
//then
assertThat(stream).isEmpty();
}
} }