Skip to content

Commit

Permalink
Merge pull request #20 from zalando/feature/partially-instance
Browse files Browse the repository at this point in the history
Added overloaded version of partially
  • Loading branch information
whiskeysierra authored Nov 2, 2017
2 parents 1a0cfec + 5ff4735 commit bd93241
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ future.exceptionally(partially(e -> {
that the `CompletionException` has no cause, in which case it will be passed to the given function.
3. Will automatically wrap any thrown `Exception` inside a `CompletionException`, if needed.

The last example is actually so common, that there is an overloaded version of `partially` that caters for this use
particular case:

```java

future.exceptionally(partially(NoRouteToHostException.class, this::fallbackValueFor))
```

## Getting Help

If you have questions, concerns, bug reports, etc., please file an issue in this repository's [Issue Tracker](../../issues).
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/zalando/fauxpas/FauxPas.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public static <T, U, X extends Throwable> ThrowingBiPredicate<T, U, X> throwingB
return predicate;
}

public static <T extends Throwable, R> Function<Throwable, R> partially(final Class<T> type,
final ThrowingFunction<T, R, Throwable> function) {
return partially(e -> {
if (type.isInstance(e)) {
return function.apply(type.cast(e));
}
throw e;
});
}

public static <R> Function<Throwable, R> partially(final ThrowingFunction<Throwable, R, Throwable> function) {
return throwable -> {
try {
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/zalando/fauxpas/ExceptionallyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,31 @@ private void shouldRethrowPackedWhenExplicitlyCompletedExceptionally(final Excep
assertThat(thrown.getCause(), is(sameInstance(exception)));
}

@Test
void shouldHandleIfInstanceOf() {
final CompletableFuture<String> original = new CompletableFuture<>();
final CompletableFuture<String> unit = original.exceptionally(partially(
IllegalStateException.class, e -> "foo"));

final IllegalStateException exception = new IllegalStateException();
original.completeExceptionally(exception);

assertThat(unit.join(), is("foo"));
}

@Test
void shouldThrowIfNotInstanceOf() {
final CompletableFuture<String> original = new CompletableFuture<>();
final CompletableFuture<String> unit = original.exceptionally(partially(
IllegalArgumentException.class, e -> "foo"));

final IllegalStateException exception = new IllegalStateException();
original.completeExceptionally(exception);

final CompletionException thrown = assertThrows(CompletionException.class, unit::join);
assertThat(thrown.getCause(), is(sameInstance(exception)));
}

private Function<String, String> failWith(final RuntimeException e) {
return result -> {
throw e;
Expand Down

0 comments on commit bd93241

Please sign in to comment.