Added Result.maybe()

This commit is contained in:
Paul Campbell 2018-06-23 18:22:05 +01:00
parent 47193e6480
commit 62a61213cc
4 changed files with 88 additions and 0 deletions

View file

@ -22,9 +22,11 @@
package net.kemitix.mon.result; package net.kemitix.mon.result;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.kemitix.mon.maybe.Maybe;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate;
/** /**
* An Error Result. * An Error Result.
@ -60,4 +62,9 @@ class Err<T> implements Result<T> {
public void match(final Consumer<T> onSuccess, final Consumer<Throwable> onError) { public void match(final Consumer<T> onSuccess, final Consumer<Throwable> onError) {
onError.accept(error); onError.accept(error);
} }
@Override
public Result<Maybe<T>> maybe(final Predicate<T> predicate) {
return Result.error(error);
}
} }

View file

@ -21,8 +21,11 @@
package net.kemitix.mon.result; package net.kemitix.mon.result;
import net.kemitix.mon.maybe.Maybe;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate;
/** /**
* An Either type for holding a result or an error (exception). * An Either type for holding a result or an error (exception).
@ -97,4 +100,11 @@ public interface Result<T> {
*/ */
void match(Consumer<T> onSuccess, Consumer<Throwable> onError); void match(Consumer<T> onSuccess, Consumer<Throwable> onError);
/**
* Wraps the value within the Result in a Maybe, either a Just if the predicate is true, or Nothing.
*
* @param predicate the test to decide
* @return a Result containing a Maybe that may or may not contain a value
*/
Result<Maybe<T>> maybe(Predicate<T> predicate);
} }

View file

@ -22,9 +22,11 @@
package net.kemitix.mon.result; package net.kemitix.mon.result;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.kemitix.mon.maybe.Maybe;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate;
/** /**
* A Successful Result. * A Successful Result.
@ -60,4 +62,12 @@ class Success<T> implements Result<T> {
public void match(final Consumer<T> onSuccess, final Consumer<Throwable> onError) { public void match(final Consumer<T> onSuccess, final Consumer<Throwable> onError) {
onSuccess.accept(value); onSuccess.accept(value);
} }
@Override
public Result<Maybe<T>> maybe(final Predicate<T> predicate) {
if (predicate.test(value)) {
return Result.ok(Maybe.just(value));
}
return Result.ok(Maybe.nothing());
}
} }

View file

@ -1,10 +1,13 @@
package net.kemitix.mon; package net.kemitix.mon;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.kemitix.mon.maybe.Maybe;
import net.kemitix.mon.result.Result; import net.kemitix.mon.result.Result;
import org.assertj.core.api.WithAssertions; import org.assertj.core.api.WithAssertions;
import org.junit.Test; import org.junit.Test;
import java.util.function.Predicate;
public class ResultTest implements WithAssertions { public class ResultTest implements WithAssertions {
@Test @Test
@ -134,6 +137,64 @@ public class ResultTest implements WithAssertions {
); );
} }
@Test
public void successMaybe_whenPasses_isSuccessJust() {
//given
final Result<Integer> okResult = Result.ok(1);
//when
final Result<Maybe<Integer>> maybeResult = okResult.maybe(value -> value >= 0);
//then
assertThat(maybeResult.isOkay()).isTrue();
maybeResult.match(
success -> assertThat(success.toOptional()).contains(1),
error -> fail("not an error")
);
}
@Test
public void successMaybe_whenFails_isSuccessNothing() {
//given
final Result<Integer> okResult = Result.ok(1);
//when
final Result<Maybe<Integer>> maybeResult = okResult.maybe(value -> value >= 4);
//then
assertThat(maybeResult.isOkay()).isTrue();
maybeResult.match(
success -> assertThat(success.toOptional()).isEmpty(),
error -> fail("not an error")
);
}
@Test
public void errorMaybe_whenPasses_isError() {
//given
final RuntimeException exception = new RuntimeException();
final Result<Integer> errorResult = Result.error(exception);
//when
final Result<Maybe<Integer>> maybeResult = errorResult.maybe(value -> value >= 0);
//then
assertThat(maybeResult.isError()).isTrue();
maybeResult.match(
success -> fail("not a success"),
error -> assertThat(error).isSameAs(exception)
);
}
@Test
public void errorMaybe_whenFails_isError() {
//given
final RuntimeException exception = new RuntimeException();
final Result<Integer> errorResult = Result.error(exception);
//when
final Result<Maybe<Integer>> maybeResult = errorResult.maybe(value -> value >= 4);
//then
assertThat(maybeResult.isError()).isTrue();
maybeResult.match(
success -> fail("not a success"),
error -> assertThat(error).isSameAs(exception)
);
}
@Test @Test
public void useCase_whenOkay_thenReturnSuccess() { public void useCase_whenOkay_thenReturnSuccess() {
//given //given