Skip to content

Commit

Permalink
Add Result.tryMap (#33)
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 authored Dec 13, 2021
1 parent 46149c8 commit 9da9521
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 12 deletions.
2 changes: 1 addition & 1 deletion common-testing/3RD-PARTY-LICENSES/sbom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<name>common-types</name>
<groupId>io.carbynestack</groupId>
<artifactId>common-types</artifactId>
<version>0.1-SNAPSHOT</version>
<version>0.2-SNAPSHOT</version>
<licenses>
<license>
<name>Apache-2.0</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ void mapAndTransformType() {
.hasReason(reason);
}

@Test
void tryMap() {
assertThat(result.tryMap(v -> v * 2, reason * 2)).hasReason(reason);
}

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

@Test
void tryMapAndTransformType() {
assertThat(result.tryMap(v -> String.format("%s * 2 -> %s", v, v * 2),
reason * 2)).hasReason(reason);
}

@Test
void tryMapWithException() {
assertThat(result.tryMap(v -> {
throw new IOException("-11");
}, reason * 2)).hasReason(reason);
}

@Test
void peek() {
AtomicInteger output = new AtomicInteger(-1);
Expand All @@ -80,11 +104,9 @@ void tryPeek() {

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ void mapAndTransformType() {
.hasValue("12 * 2 -> 24");
}

@Test
void tryMap() {
assertThat(result.tryMap(v -> v * 2, -11)).hasValue(24);
}

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

@Test
void tryMapAndTransformType() {
assertThat(result.tryMap(v -> String.format("%s * 2 -> %s", v, v * 2),
-11)).hasValue("12 * 2 -> 24");
}

@Test
void tryMapWithException() {
int reason = -11;
assertThat(result.tryMap(v -> {
throw new IOException("-11");
}, reason)).hasReason(reason);
}

@Test
void peek() {
AtomicInteger output = new AtomicInteger(-1);
Expand All @@ -82,11 +107,9 @@ void tryPeek() {
@Test
void tryPeekWithException() {
int reason = -11;
Result<Integer, Integer> res = result.tryPeek(v -> {
assertThat(result.tryPeek(v -> {
throw new IOException("-11");
}, reason);
assertThat(res).isFailure();
assertThat(res).hasReason(reason);
}, reason)).hasReason(reason);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -89,6 +90,28 @@ public <N> Result<N, F> map(Function<? super S, ? super N> function) {
return new Failure<>(this.reason());
}

/**
* {@inheritDoc}
*
* @param function the mapping function to apply to a {@link Success#value()}
* @param reason the failure reason in case of the function throwing
* a {@code Throwable}
* @param <N> the success type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Success} or this {@link Failure}
* @throws NullPointerException if the mapping function is {@code null}
* @version JDK 8
* @see #recover(Function)
* @see #peek(Consumer)
* @since 0.2.0
*/
@Override
public <N> Result<N, F> tryMap(AnyThrowingFunction<? super S, ? super N> function, F reason) {
requireNonNull(function);
return new Failure<>(this.reason());
}

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -272,9 +295,7 @@ public Stream<S> stream() {
@Override
@Generated
public String toString() {
return "Failure{" +
"reason=" + reason +
'}';
return "Failure{" + "reason=" + reason + '}';
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

Expand Down Expand Up @@ -159,6 +160,28 @@ default boolean isFailure() {
*/
<N> Result<N, F> map(Function<? super S, ? super N> function);

/**
* If the {@code Result} is a {@link Success}, returns the result of
* applying the given mapping function to the {@link Success#value()}.
* Otherwise, a cast version of the {@link Failure} is returned.
*
* <p>In case the function throws a {@link Throwable} the failure reason
* is returned as a {@code Failure}.<br>
*
* @param function the mapping function to apply to a {@link Success#value()}
* @param reason the failure reason in case of the function throwing
* a {@code Throwable}
* @param <N> the success type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Success} or this {@link Failure}
* @throws NullPointerException if the mapping function is {@code null}
* @see #recover(Function)
* @see #peek(Consumer)
* @since 0.2.0
*/
<N> Result<N, F> tryMap(AnyThrowingFunction<? super S, ? super N> function, F reason);

/**
* If the {@code Result} is a {@link Success}, invokes the provided
* consumer with the {@link Success#value()}. Otherwise, the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -90,6 +91,33 @@ public <N> Result<N, F> map(Function<? super S, ? super N> function) {
return new Success<>((N) function.apply(this.value()));
}

/**
* {@inheritDoc}
*
* @param function the mapping function to apply to a {@link Success#value()}
* @param reason the failure reason in case of the function throwing
* a {@code Throwable}
* @param <N> the success type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Success} or this {@link Failure}
* @throws NullPointerException if the mapping function is {@code null}
* @version JDK 8
* @see #recover(Function)
* @see #peek(Consumer)
* @since 0.2.0
*/
@Override
@SuppressWarnings("unchecked")
public <N> Result<N, F> tryMap(AnyThrowingFunction<? super S, ? super N> function, F reason) {
requireNonNull(function);
try {
return new Success<>((N) function.apply(this.value()));
} catch (Throwable throwable) {
return new Failure<>(reason);
}
}

/**
* {@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.function.AnyThrowingConsumer;
import io.carbynestack.common.function.AnyThrowingFunction;

import java.util.Optional;
import java.util.function.Consumer;
Expand Down Expand Up @@ -60,6 +61,28 @@ public <N> Result<N, F> map(Function<? super S, ? super N> function) {
return new Failure<>(this.reason());
}

/**
* {@inheritDoc}
*
* @param function the mapping function to apply to a {@link Success#value()}
* @param reason the failure reason in case of the function throwing
* a {@code Throwable}
* @param <N> the success type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Success} or this {@link Failure}
* @throws NullPointerException if the mapping function is {@code null}
* @version JDK 17
* @see #recover(Function)
* @see #peek(Consumer)
* @since 0.2.0
*/
@Override
public <N> Result<N, F> tryMap(AnyThrowingFunction<? super S, ? super N> function, F reason) {
requireNonNull(function);
return new Failure<>(this.reason());
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.carbynestack.common.CsFailureReason;
import io.carbynestack.common.function.AnyThrowingConsumer;
import io.carbynestack.common.function.AnyThrowingSupplier;
import io.carbynestack.common.function.AnyThrowingFunction;
import io.carbynestack.common.function.ThrowingSupplier;

import java.util.Map;
Expand Down Expand Up @@ -159,6 +160,28 @@ default boolean isFailure() {
*/
<N> Result<N, F> map(Function<? super S, ? super N> function);

/**
* If the {@code Result} is a {@link Success}, returns the result of
* applying the given mapping function to the {@link Success#value()}.
* Otherwise, a cast version of the {@link Failure} is returned.
*
* <p>In case the function throws a {@link Throwable} the failure reason
* is returned as a {@code Failure}.<br>
*
* @param function the mapping function to apply to a {@link Success#value()}
* @param reason the failure reason in case of the function throwing
* a {@code Throwable}
* @param <N> the success type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Success} or this {@link Failure}
* @throws NullPointerException if the mapping function is {@code null}
* @see #recover(Function)
* @see #peek(Consumer)
* @since 0.2.0
*/
<N> Result<N, F> tryMap(AnyThrowingFunction<? super S, ? super N> function, F reason);

/**
* If the {@code Result} is a {@link Success}, invokes the provided
* consumer with the {@link Success#value()}. Otherwise, the
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.function.AnyThrowingConsumer;
import io.carbynestack.common.function.AnyThrowingFunction;

import java.util.Optional;
import java.util.function.Consumer;
Expand Down Expand Up @@ -61,6 +62,33 @@ public <N> Result<N, F> map(Function<? super S, ? super N> function) {
return new Success<>((N) function.apply(this.value()));
}

/**
* {@inheritDoc}
*
* @param function the mapping function to apply to a {@link Success#value()}
* @param reason the failure reason in case of the function throwing
* a {@code Throwable}
* @param <N> the success type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Success} or this {@link Failure}
* @throws NullPointerException if the mapping function is {@code null}
* @version JDK 17
* @see #recover(Function)
* @see #peek(Consumer)
* @since 0.2.0
*/
@Override
@SuppressWarnings("unchecked")
public <N> Result<N, F> tryMap(AnyThrowingFunction<? super S, ? super N> function, F reason) {
requireNonNull(function);
try {
return new Success<>((N) function.apply(this.value()));
} catch (Throwable throwable) {
return new Failure<>(reason);
}
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ void tryPeek() {
.isFailure()).isTrue();
}

@Test
void tryMap() {
assertThat(new Success<>(12)
.tryMap(v -> v * 2, -11)
.<Integer>tryMap(v -> {
throw new RuntimeException();
}, -21)
.tryMap(v -> v * 2, -11)
.isFailure()).isTrue();
}

@Test
void of() {
var value = 12;
Expand Down

0 comments on commit 9da9521

Please sign in to comment.