Skip to content

Commit

Permalink
Add Result.tryPeek (#14)
Browse files Browse the repository at this point in the history
Signed-off-by: David Greven <[email protected]>
Signed-off-by: Sebastian Becker <[email protected]>
  • Loading branch information
grevend-bosch committed Nov 16, 2021
1 parent 4d8d588 commit 3c6c68c
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -70,6 +71,28 @@ void peekNullPointerException() {
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void tryPeek() {
AtomicInteger output = new AtomicInteger(-1);
result.tryPeek(output::set, reason * 2);
assertThat(output).hasValue(-1);
}

@Test
void tryPeekWithException() {
Result<Integer, Integer> res = result.tryPeek(v -> {
throw new IOException("-11");
}, reason * 2);
assertThat(res).isFailure();
assertThat(res).hasReason(reason);
}

@Test
void tryPeekNullPointerException() {
assertThatThrownBy(() -> result.tryPeek(null, reason * 2))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void recover() {
assertThat(result.recover(r -> r * 2)).hasValue(42);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
Expand Down Expand Up @@ -71,6 +72,29 @@ void peekNullPointerException() {
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void tryPeek() {
AtomicInteger output = new AtomicInteger(-1);
result.tryPeek(output::set, -11);
assertThat(output).hasValue(value);
}

@Test
void tryPeekWithException() {
int reason = -11;
Result<Integer, Integer> res = result.tryPeek(v -> {
throw new IOException("-11");
}, reason);
assertThat(res).isFailure();
assertThat(res).hasReason(reason);
}

@Test
void tryPeekNullPointerException() {
assertThatThrownBy(() -> result.tryPeek(null, -11))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void recover() {
assertThat(result.recover(identity())).hasValue(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package io.carbynestack.common.result;

import io.carbynestack.common.Generated;
import io.carbynestack.common.function.AnyThrowingConsumer;

import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -104,6 +105,24 @@ public Result<S, F> peek(Consumer<? super S> consumer) {
return this;
}

/**
* {@inheritDoc}
*
* @param consumer the consumer of {@link Success#value()}
* @param reason the failure reason in case of the consumer throwing
* a {@code Throwable}
* @return {@code this}
* @throws NullPointerException if the consumer is {@code null}
* @version JDK 8
* @see #map(Function)
* @since 0.1.0
*/
@Override
public Result<S, F> tryPeek(AnyThrowingConsumer<? super S> consumer, F reason) {
requireNonNull(consumer);
return this;
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package io.carbynestack.common.result;

import io.carbynestack.common.CsFailureReason;
import io.carbynestack.common.function.AnyThrowingConsumer;
import io.carbynestack.common.function.AnyThrowingSupplier;
import io.carbynestack.common.function.ThrowingSupplier;

Expand Down Expand Up @@ -123,6 +124,24 @@ default boolean isFailure() {
*/
Result<S, F> peek(Consumer<? super S> consumer);

/**
* If the {@code Result} is a {@link Success}, invokes the provided
* consumer with the {@link Success#value()}. Otherwise, the
* {@link Failure} is returned.
*
* <p>In case the consumer throws a {@link Throwable} the failure reason
* is returned as a {@code Failure}.<br>
*
* @param consumer the consumer of {@link Success#value()}
* @param reason the failure reason in case of the consumer throwing
* a {@code Throwable}
* @return {@code this}
* @throws NullPointerException if the consumer is {@code null}
* @see #map(Function)
* @since 0.1.0
*/
Result<S, F> tryPeek(AnyThrowingConsumer<? super S> consumer, F reason);

/**
* If the {@code Result} is a {@link Failure}, returns the result of
* applying the given mapping function to the {@link Failure#reason()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package io.carbynestack.common.result;

import io.carbynestack.common.Generated;
import io.carbynestack.common.function.AnyThrowingConsumer;

import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -105,6 +106,29 @@ public Result<S, F> peek(Consumer<? super S> consumer) {
return this;
}

/**
* {@inheritDoc}
*
* @param consumer the consumer of {@link Success#value()}
* @param reason the failure reason in case of the consumer throwing
* a {@code Throwable}
* @return {@code this}
* @throws NullPointerException if the consumer is {@code null}
* @version JDK 8
* @see #map(Function)
* @since 0.1.0
*/
@Override
public Result<S, F> tryPeek(AnyThrowingConsumer<? super S> consumer, F reason) {
requireNonNull(consumer);
try {
consumer.accept(this.value());
return this;
} catch (Throwable throwable) {
return new Failure<>(reason);
}
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package io.carbynestack.common.result;

import io.carbynestack.common.function.AnyThrowingConsumer;

import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -74,6 +76,24 @@ public Result<S, F> peek(Consumer<? super S> consumer) {
return this;
}

/**
* {@inheritDoc}
*
* @param consumer the consumer of {@link Success#value()}
* @param reason the failure reason in case of the consumer throwing
* a {@code Throwable}
* @return {@code this}
* @throws NullPointerException if the consumer is {@code null}
* @version JDK 17
* @see #map(Function)
* @since 0.1.0
*/
@Override
public Result<S, F> tryPeek(AnyThrowingConsumer<? super S> consumer, F reason) {
requireNonNull(consumer);
return this;
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package io.carbynestack.common.result;

import io.carbynestack.common.CsFailureReason;
import io.carbynestack.common.function.AnyThrowingConsumer;
import io.carbynestack.common.function.AnyThrowingSupplier;
import io.carbynestack.common.function.ThrowingSupplier;

Expand Down Expand Up @@ -123,6 +124,24 @@ default boolean isFailure() {
*/
Result<S, F> peek(Consumer<? super S> consumer);

/**
* If the {@code Result} is a {@link Success}, invokes the provided
* consumer with the {@link Success#value()}. Otherwise, the
* {@link Failure} is returned.
*
* <p>In case the consumer throws a {@link Throwable} the failure reason
* is returned as a {@code Failure}.<br>
*
* @param consumer the consumer of {@link Success#value()}
* @param reason the failure reason in case of the consumer throwing
* a {@code Throwable}
* @return {@code this}
* @throws NullPointerException if the consumer is {@code null}
* @see #map(Function)
* @since 0.1.0
*/
Result<S, F> tryPeek(AnyThrowingConsumer<? super S> consumer, F reason);

/**
* If the {@code Result} is a {@link Failure}, returns the result of
* applying the given mapping function to the {@link Failure#reason()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package io.carbynestack.common.result;

import io.carbynestack.common.function.AnyThrowingConsumer;

import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -75,6 +77,29 @@ public Result<S, F> peek(Consumer<? super S> consumer) {
return this;
}

/**
* {@inheritDoc}
*
* @param consumer the consumer of {@link Success#value()}
* @param reason the failure reason in case of the consumer throwing
* a {@code Throwable}
* @return {@code this}
* @throws NullPointerException if the consumer is {@code null}
* @version JDK 17
* @see #map(Function)
* @since 0.1.0
*/
@Override
public Result<S, F> tryPeek(AnyThrowingConsumer<? super S> consumer, F reason) {
requireNonNull(consumer);
try {
consumer.accept(this.value());
return this;
} catch (Throwable throwable) {
return new Failure<>(reason);
}
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ void result() {
assertThat(res.stream().toList()).isEmpty();
}

@Test
void tryPeek() {
assertThat(new Success<>(12)
.tryPeek(System.out::println, -1)
.tryPeek(v -> {
throw new IOException();
}, -1)
.tryPeek(System.out::println, -1)
.isFailure()).isTrue();
}

@Test
void of() {
assertThat(Result.of(() -> 12).isSuccess()).isTrue();
Expand Down

0 comments on commit 3c6c68c

Please sign in to comment.