Skip to content

Commit

Permalink
#86: third arg for FuncWithCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 6, 2017
1 parent 0d27ede commit 92cf9a2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/main/java/org/cactoos/func/FuncWithCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,48 @@ public final class FuncWithCallback<X, Y> implements Func<X, Y> {
*/
private final Func<Throwable, Y> callback;

/**
* The follow up.
*/
private final Func<Y, Y> follow;

/**
* Ctor.
* @param fnc The func
* @param cbk The callback
*/
public FuncWithCallback(final Func<X, Y> fnc,
final Func<Throwable, Y> cbk) {
this(fnc, cbk, input -> input);
}

/**
* Ctor.
* @param fnc The func
* @param cbk The callback
* @param flw The follow up func
*/
public FuncWithCallback(final Func<X, Y> fnc,
final Func<Throwable, Y> cbk, final Func<Y, Y> flw) {
this.func = fnc;
this.callback = cbk;
this.follow = flw;
}

@Override
@SuppressWarnings("PMD.AvoidCatchingThrowable")
public Y apply(final X input) throws Exception {
Y result;
try {
result = this.func.apply(input);
result = this.func.apply(input);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
result = this.callback.apply(ex);
// @checkstyle IllegalCatchCheck (1 line)
} catch (final Throwable ex) {
result = this.callback.apply(ex);
}
return result;
return this.follow.apply(result);
}

}
13 changes: 13 additions & 0 deletions src/test/java/org/cactoos/func/FuncWithCallbackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,17 @@ public void usesCallback() throws Exception {
);
}

@Test
public void usesFollowUp() throws Exception {
MatcherAssert.assertThat(
"Can't use the follow-up func",
new FuncWithCallback<>(
input -> "works fine",
ex -> "won't happen",
input -> "follow up"
),
new FuncApplies<>(1, Matchers.containsString("follow"))
);
}

}

0 comments on commit 92cf9a2

Please sign in to comment.