Skip to content

Commit

Permalink
Merge branch 'master' into map-failure
Browse files Browse the repository at this point in the history
  • Loading branch information
grevend-bosch committed Dec 13, 2021
2 parents 9acf832 + 9da9521 commit 45e0eb9
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 15 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 @@ -68,12 +68,36 @@ void mapFailureNullPointerException() {
assertThatThrownBy(() -> result.mapFailure(null))
.isExactlyInstanceOf(NullPointerException.class);
}

@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 mapFailureAndTransformType() {
assertThat(result.mapFailure(r -> String.format("%s * 2 -> %s", r, r * 2)))
.hasReason("21 * 2 -> 42");
}

@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() {
Expand All @@ -97,11 +121,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 @@ -60,22 +60,47 @@ void mapAndTransformType() {
}

@Test
public void mapFailure() {
void mapFailure() {
assertThat(result.mapFailure(r -> r * 2)).hasValue(value);
}

@Test
public void mapFailureNullPointerException() {
void mapFailureNullPointerException() {
assertThatThrownBy(() -> result.mapFailure(null))
.isExactlyInstanceOf(NullPointerException.class);
}

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

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

@Test
public void mapFailureAndTransformType() {
void mapFailureAndTransformType() {
assertThat(result.mapFailure(r -> String.format("%s * 2 -> %s", r, r * 2)))
.hasValue(value);
}

@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 @@ -99,11 +124,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 @@ -107,6 +108,28 @@ public <N> Result<N, F> map(Function<? super S, ? super N> function) {
@SuppressWarnings("unchecked")
public <R> Result<S, R> mapFailure(Function<? super F, ? super R> function) {
return new Failure<>((R) function.apply(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());
}

/**
Expand Down Expand Up @@ -292,9 +315,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 @@ -176,6 +177,28 @@ default boolean isFailure() {
*/
<R> Result<S, R> mapFailure(Function<? super F, ? super R> 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 @@ -110,6 +111,33 @@ public <R> Result<S, R> mapFailure(Function<? super F, ? super R> function) {
return new Success<>(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 @@ -80,6 +81,28 @@ public <R> Result<S, R> mapFailure(Function<? super F, ? super R> function) {
return new Failure<>((R) function.apply(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 @@ -176,6 +177,28 @@ default boolean isFailure() {
*/
<R> Result<S, R> mapFailure(Function<? super F, ? super R> 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 @@ -80,6 +81,33 @@ public <R> Result<S, R> mapFailure(Function<? super F, ? super R> function) {
requireNonNull(function);
return new Success<>(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 @@ -97,6 +97,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 45e0eb9

Please sign in to comment.