Skip to content

Commit

Permalink
Add Result.mapFailure (#34)
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 Dec 13, 2021
1 parent 9da9521 commit a73b4a5
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ void mapAndTransformType() {
.hasReason(reason);
}

@Test
void mapFailure() {
assertThat(result.mapFailure(r -> r * 2)).hasReason(reason * 2);
}

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

@Test
void tryMap() {
assertThat(result.tryMap(v -> v * 2, reason * 2)).hasReason(reason);
Expand All @@ -69,6 +80,12 @@ void tryMapNullPointerException() {
.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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ void mapAndTransformType() {
.hasValue("12 * 2 -> 24");
}

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

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

@Test
void tryMap() {
assertThat(result.tryMap(v -> v * 2, -11)).hasValue(24);
Expand All @@ -70,6 +81,12 @@ void tryMapNullPointerException() {
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ 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 Failure#reason()}
* @param <R> the failure type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Failure} or this {@link Success}
* @throws NullPointerException if the mapping function is {@code null}
* @version JDK 8
* @see #map(Function)
* @see #recover(Function)
* @since 0.2.0
*/
@Override
@SuppressWarnings("unchecked")
public <R> Result<S, R> mapFailure(Function<? super F, ? super R> function) {
return new Failure<>((R) function.apply(this.reason()));
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ default boolean isFailure() {
*/
<N> Result<N, F> map(Function<? super S, ? super N> function);

/**
* If the {@code Result} is a {@link Failure}, returns the result of
* applying the given mapping function to the {@link Failure#reason()}.
* Otherwise, a cast version of the {@link Success} is returned.
*
* @param function the mapping function to apply to a {@link Failure#reason()}
* @param <R> the failure type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Failure} or this {@link Success}
* @throws NullPointerException if the mapping function is {@code null}
* @see #map(Function)
* @see #recover(Function)
* @since 0.2.0
*/
<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()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ 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 Failure#reason()}
* @param <R> the failure type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Failure} or this {@link Success}
* @throws NullPointerException if the mapping function is {@code null}
* @version JDK 8
* @see #map(Function)
* @see #recover(Function)
* @since 0.2.0
*/
@Override
public <R> Result<S, R> mapFailure(Function<? super F, ? super R> function) {
requireNonNull(function);
return new Success<>(this.value());
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ 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 Failure#reason()}
* @param <R> the failure type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Failure} or this {@link Success}
* @throws NullPointerException if the mapping function is {@code null}
* @version JDK 17
* @see #map(Function)
* @see #recover(Function)
* @since 0.2.0
*/
@Override
@SuppressWarnings("unchecked")
public <R> Result<S, R> mapFailure(Function<? super F, ? super R> function) {
return new Failure<>((R) function.apply(this.reason()));
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ default boolean isFailure() {
*/
<N> Result<N, F> map(Function<? super S, ? super N> function);

/**
* If the {@code Result} is a {@link Failure}, returns the result of
* applying the given mapping function to the {@link Failure#reason()}.
* Otherwise, a cast version of the {@link Success} is returned.
*
* @param function the mapping function to apply to a {@link Failure#reason()}
* @param <R> the failure type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Failure} or this {@link Success}
* @throws NullPointerException if the mapping function is {@code null}
* @see #map(Function)
* @see #recover(Function)
* @since 0.2.0
*/
<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()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ 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 Failure#reason()}
* @param <R> the failure type of the value returned from the mapping
* function
* @return the {@code Result} of mapping the given function to the value
* from this {@link Failure} or this {@link Success}
* @throws NullPointerException if the mapping function is {@code null}
* @version JDK 17
* @see #map(Function)
* @see #recover(Function)
* @since 0.2.0
*/
@Override
public <R> Result<S, R> mapFailure(Function<? super F, ? super R> function) {
requireNonNull(function);
return new Success<>(this.value());
}

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

@Test
void mapFailure() {
assertThat(new Success<>(12)
.mapFailure(Long.TYPE::cast)
.flatMap(v -> new Failure<Integer, Long>((long) v * 2))
.mapFailure(Long::intValue)
.isFailure()).isTrue();
}

@Test
void tryPeek() {
assertThat(new Success<>(12)
Expand Down

0 comments on commit a73b4a5

Please sign in to comment.