diff --git a/src/main/java/org/cactoos/func/FuncWithCallback.java b/src/main/java/org/cactoos/func/FuncWithCallback.java index 2e0dc53e06..5522c75c5c 100644 --- a/src/main/java/org/cactoos/func/FuncWithCallback.java +++ b/src/main/java/org/cactoos/func/FuncWithCallback.java @@ -48,6 +48,11 @@ public final class FuncWithCallback implements Func { */ private final Func callback; + /** + * The follow up. + */ + private final Func follow; + /** * Ctor. * @param fnc The func @@ -55,8 +60,20 @@ public final class FuncWithCallback implements Func { */ public FuncWithCallback(final Func fnc, final Func 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 fnc, + final Func cbk, final Func flw) { this.func = fnc; this.callback = cbk; + this.follow = flw; } @Override @@ -64,7 +81,7 @@ public FuncWithCallback(final Func fnc, 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); @@ -72,7 +89,7 @@ public Y apply(final X input) throws Exception { } catch (final Throwable ex) { result = this.callback.apply(ex); } - return result; + return this.follow.apply(result); } } diff --git a/src/test/java/org/cactoos/func/FuncWithCallbackTest.java b/src/test/java/org/cactoos/func/FuncWithCallbackTest.java index 44f4c89ff9..74339669b0 100644 --- a/src/test/java/org/cactoos/func/FuncWithCallbackTest.java +++ b/src/test/java/org/cactoos/func/FuncWithCallbackTest.java @@ -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")) + ); + } + }