Add Maybe.stream()
This commit is contained in:
parent
b2fa6e3dc2
commit
5bf0fcbdf9
5 changed files with 39 additions and 5 deletions
|
@ -6,6 +6,7 @@ CHANGELOG
|
|||
|
||||
* Add `Result`
|
||||
* Moved `Maybe` to `net.kemitix.mon.maybe.Maybe`
|
||||
* Add `Maybe.stream()`
|
||||
|
||||
0.6.0
|
||||
-----
|
||||
|
|
|
@ -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<T> implements Maybe<T> {
|
|||
public void orElseThrow(final Supplier<Exception> e) {
|
||||
// do not throw
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<T> stream() {
|
||||
return Stream.of(value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<T> extends Functor<T, Maybe<?>> {
|
|||
@SuppressWarnings("illegalthrows")
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -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<T> implements Maybe<T> {
|
|||
public void orElseThrow(final Supplier<Exception> e) throws Exception {
|
||||
throw e.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<T> stream() {
|
||||
return Stream.empty();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 <T> Predicate<T> 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<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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue