Skip to content

Commit

Permalink
Add Result.swap support (#6)
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 15, 2021
1 parent 135764e commit f60395b
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ void orNullPointerException() {
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void swap() {
assertThat(result.swap()).hasValue(reason);
}

@Test
void toOptional() {
assertThat(result.toOptional()).isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ void orNullPointerException() {
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void swap() {
assertThat(result.swap()).hasReason(value);
}

@Test
void toOptional() {
assertThat(result.toOptional()).hasValue(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ public Result<S, F> or(Supplier<? extends Result<? extends S, F>> supplier) {
return (Result<S, F>) requireNonNull(supplier.get());
}

/**
* {@inheritDoc}
*
* @return a {@code Result} with swapped content
* @since 0.1.0
*/
@Override
public Result<F, S> swap() {
return new Success<>(this.reason());
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ default boolean isFailure() {
*/
Result<S, F> or(Supplier<? extends Result<? extends S, F>> supplier);

/**
* Swaps the {@code Result} success value with the failure reason or vise versa.
*
* @return a {@code Result} with swapped content
* @since 0.1.0
*/
Result<F, S> swap();

/**
* If the {@code Result} is a {@link Success}, returns an {@link Optional}
* for the {@link Success#value()}. Otherwise, an empty {@code Optional} is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ public Result<S, F> or(Supplier<? extends Result<? extends S, F>> supplier) {
return this;
}

/**
* {@inheritDoc}
*
* @return a {@code Result} with swapped content
* @since 0.1.0
*/
@Override
public Result<F, S> swap() {
return new Failure<>(this.value());
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@ public Result<S, F> or(Supplier<? extends Result<? extends S, F>> supplier) {
return (Result<S, F>) requireNonNull(supplier.get());
}

/**
* {@inheritDoc}
*
* @return a {@code Result} with swapped content
* @since 0.1.0
*/
@Override
public Result<F, S> swap() {
return new Success<>(this.reason());
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ default boolean isFailure() {
*/
Result<S, F> or(Supplier<? extends Result<? extends S, F>> supplier);

/**
* Swaps the {@code Result} success value with the failure reason or vise versa.
*
* @return a {@code Result} with swapped content
* @since 0.1.0
*/
Result<F, S> swap();

/**
* If the {@code Result} is a {@link Success}, returns an {@link Optional}
* for the {@link Success#value()}. Otherwise, an empty {@code Optional} is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@ public Result<S, F> or(Supplier<? extends Result<? extends S, F>> supplier) {
return this;
}

/**
* {@inheritDoc}
*
* @return a {@code Result} with swapped content
* @since 0.1.0
*/
@Override
public Result<F, S> swap() {
return new Failure<>(this.value());
}

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

@Test
void swap() {
var value = 12;
var res = new Success<Integer, Integer>(value);
assertThat(res.<Integer>fold(r -> -1, identity())).isEqualTo(value);
assertThat(res.swap().<Integer>fold(r -> -1, identity())).isEqualTo(-1);
assertThat(res.swap().swap().<Integer>fold(r -> -1, identity())).isEqualTo(value);
}
}

0 comments on commit f60395b

Please sign in to comment.