Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jun 6, 2017
2 parents 755bc12 + 92cf9a2 commit 76c3ff1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 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);
}

}
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"))
);
}

}

0 comments on commit 76c3ff1

Please sign in to comment.