Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

86: third arg for FuncWithCallback #87

Merged
merged 2 commits into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
7 changes: 6 additions & 1 deletion src/main/java/org/cactoos/io/ResourceAsInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
import org.cactoos.text.FormattedText;

/**
* Jar class resource.
* Classpath resource.
*
* <p>Pay attention that the name of resource must always be
* global, <strong>not</strong> starting with a leading slash. Thus,
* if you want to load a text file from {@code /com/example/Test.txt},
* you must provide this name: {@code "com/example/Test.txt"}.</p>
*
* @author Kirill ([email protected])
* @version $Id$
Expand Down
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"))
);
}

}