Merge branch 'master' into release/0.8.0

* master:
  Add `Result.peek(Consumer)`
This commit is contained in:
Paul Campbell 2018-06-30 18:15:23 +01:00
commit 9aa6ad1733
5 changed files with 48 additions and 2 deletions

View file

@ -136,10 +136,10 @@ public interface Maybe<T> extends Functor<T, Maybe<?>> {
Maybe<T> filter(Predicate<T> predicate);
/**
* Provide the value within the Maybe, if it exists, to the Supplier, and returns the Maybe.
* Provide the value within the Maybe, if it exists, to the Consumer, and returns this Maybe.
*
* @param consumer the Consumer to the value if present
* @return the Maybe
* @return this Maybe
*/
Maybe<T> peek(Consumer<T> consumer);
}

View file

@ -35,6 +35,7 @@ import java.util.function.Predicate;
* @param <T> the type of the value in the Result if it has been a success
*/
@RequiredArgsConstructor
@SuppressWarnings("methodcount")
class Err<T> implements Result<T> {
private final Throwable error;
@ -74,6 +75,11 @@ class Err<T> implements Result<T> {
throw error;
}
@Override
public Result<T> peek(final Consumer<T> consumer) {
return this;
}
@Override
public boolean equals(final Object other) {
return other instanceof Err && Objects.equals(error, ((Err) other).error);

View file

@ -169,4 +169,12 @@ public interface Result<T> extends Functor<T, Result<?>> {
*/
@SuppressWarnings("illegalthrows")
T orElseThrow() throws Throwable;
/**
* Provide the value within the Result, if it is a success, to the Consumer, and returns this Result.
*
* @param consumer the Consumer to the value if a success
* @return this Result
*/
Result<T> peek(Consumer<T> consumer);
}

View file

@ -35,6 +35,7 @@ import java.util.function.Predicate;
* @param <T> the type of the value in the Result
*/
@RequiredArgsConstructor
@SuppressWarnings("methodcount")
class Success<T> implements Result<T> {
private final T value;
@ -77,6 +78,12 @@ class Success<T> implements Result<T> {
return value;
}
@Override
public Result<T> peek(final Consumer<T> consumer) {
consumer.accept(value);
return this;
}
@Override
public boolean equals(final Object other) {
return other instanceof Success && Objects.equals(value, ((Success) other).value);

View file

@ -8,6 +8,7 @@ import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;
public class ResultTest implements WithAssertions {
@ -427,6 +428,30 @@ public class ResultTest implements WithAssertions {
);
}
@Test
public void success_peek_consumes() {
//given
final Result<Integer> result = Result.ok(1);
final AtomicReference<Integer> consumed = new AtomicReference<>(0);
//when
final Result<Integer> peeked = result.peek(consumed::set);
//then
assertThat(consumed).hasValue(1);
assertThat(peeked).isSameAs(result);
}
@Test
public void error_peek_doesNothing() {
//given
final Result<Integer> result = Result.error(new RuntimeException());
final AtomicReference<Integer> consumed = new AtomicReference<>(0);
//when
final Result<Integer> peeked = result.peek(consumed::set);
//then
assertThat(consumed).hasValue(0);
assertThat(peeked).isSameAs(result);
}
@RequiredArgsConstructor
private static class UseCase {