Merge branch 'master' into release/0.8.0
* master: Add `Result.peek(Consumer)`
This commit is contained in:
commit
9aa6ad1733
5 changed files with 48 additions and 2 deletions
|
@ -136,10 +136,10 @@ public interface Maybe<T> extends Functor<T, Maybe<?>> {
|
||||||
Maybe<T> filter(Predicate<T> predicate);
|
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
|
* @param consumer the Consumer to the value if present
|
||||||
* @return the Maybe
|
* @return this Maybe
|
||||||
*/
|
*/
|
||||||
Maybe<T> peek(Consumer<T> consumer);
|
Maybe<T> peek(Consumer<T> consumer);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
* @param <T> the type of the value in the Result if it has been a success
|
||||||
*/
|
*/
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@SuppressWarnings("methodcount")
|
||||||
class Err<T> implements Result<T> {
|
class Err<T> implements Result<T> {
|
||||||
|
|
||||||
private final Throwable error;
|
private final Throwable error;
|
||||||
|
@ -74,6 +75,11 @@ class Err<T> implements Result<T> {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result<T> peek(final Consumer<T> consumer) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(final Object other) {
|
public boolean equals(final Object other) {
|
||||||
return other instanceof Err && Objects.equals(error, ((Err) other).error);
|
return other instanceof Err && Objects.equals(error, ((Err) other).error);
|
||||||
|
|
|
@ -169,4 +169,12 @@ public interface Result<T> extends Functor<T, Result<?>> {
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("illegalthrows")
|
@SuppressWarnings("illegalthrows")
|
||||||
T orElseThrow() throws Throwable;
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@ import java.util.function.Predicate;
|
||||||
* @param <T> the type of the value in the Result
|
* @param <T> the type of the value in the Result
|
||||||
*/
|
*/
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@SuppressWarnings("methodcount")
|
||||||
class Success<T> implements Result<T> {
|
class Success<T> implements Result<T> {
|
||||||
|
|
||||||
private final T value;
|
private final T value;
|
||||||
|
@ -77,6 +78,12 @@ class Success<T> implements Result<T> {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result<T> peek(final Consumer<T> consumer) {
|
||||||
|
consumer.accept(value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(final Object other) {
|
public boolean equals(final Object other) {
|
||||||
return other instanceof Success && Objects.equals(value, ((Success) other).value);
|
return other instanceof Success && Objects.equals(value, ((Success) other).value);
|
||||||
|
|
|
@ -8,6 +8,7 @@ import org.junit.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
public class ResultTest implements WithAssertions {
|
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
|
@RequiredArgsConstructor
|
||||||
private static class UseCase {
|
private static class UseCase {
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue