Skip to content

Commit

Permalink
Introduce Varargs in FuncWithFallback constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier B. OURA committed Jan 11, 2021
1 parent 0c9f2af commit 1d98a8a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/cactoos/func/FuncWithFallback.java
Expand Up @@ -48,7 +48,7 @@
* {@code
* final Product product = new FuncWithFallback<>(
* id -> new SqlProduct().apply(id),
* new FallbackFrom<>(
* new Fallback.From<>(
* SQLException.class,
* id -> new CachedProduct().apply(id)
* )
Expand All @@ -70,11 +70,11 @@
* final Product product = new FuncWithFallback<>(
* id -> new SqlProduct().apply(id),
* new IterableOf<>(
* new FallbackFrom<>(
* new Fallback.From<>(
* SQLException.class,
* id -> new CachedProduct().apply(id)
* ),
* new FallbackFrom<>(
* new Fallback.From<>(
* SQLRecoverableException.class,
* id -> new SqlProduct().apply(id) // run it again
* )
Expand Down Expand Up @@ -105,11 +105,11 @@ public final class FuncWithFallback<X, Y> implements Func<X, Y> {
/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param fbks The fallbacks
*/
@SuppressWarnings("unchecked")
public FuncWithFallback(final Func<X, Y> fnc, final Fallback<Y> fbk) {
this(fnc, new IterableOf<>(fbk));
@SafeVarargs
public FuncWithFallback(final Func<X, Y> fnc, final Fallback<Y>... fbks) {
this(fnc, new IterableOf<>(fbks));
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/test/java/org/cactoos/func/FuncWithFallbackTest.java
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.util.IllegalFormatException;
import java.util.IllegalFormatWidthException;
import org.cactoos.Fallback;
import org.cactoos.iterable.IterableOf;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
Expand All @@ -46,9 +47,9 @@ public void usesMainFunc() {
final String expected = "It's success";
new Assertion<>(
"Can't use the main function if no exception",
new FuncWithFallback<>(
new FuncWithFallback<Integer, String>(
input -> expected,
new FallbackFrom<>(
new Fallback.From<>(
Exception.class,
ex -> "In case of failure..."
)
Expand All @@ -62,11 +63,11 @@ public void usesFallback() {
final String expected = "Never mind";
new Assertion<>(
"Can't use the callback in case of exception",
new FuncWithFallback<>(
new FuncWithFallback<Integer, String>(
input -> {
throw new IOException("Failure");
},
new FallbackFrom<>(IOException.class, ex -> expected)
new Fallback.From<>(IOException.class, ex -> expected)
),
new FuncApplies<>(1, expected)
).affirm();
Expand All @@ -77,13 +78,13 @@ public void usesFallbackOfInterruptedException() {
final String expected = "Fallback from InterruptedException";
new Assertion<>(
"Can't use a fallback from Interrupted in case of exception",
new FuncWithFallback<>(
new FuncWithFallback<Integer, String>(
input -> {
throw new InterruptedException(
"Failure with InterruptedException"
);
},
new FallbackFrom<>(InterruptedException.class, exp -> expected)
new Fallback.From<>(InterruptedException.class, exp -> expected)
),
new FuncApplies<>(1, expected)
).affirm();
Expand All @@ -99,11 +100,11 @@ public void usesTheClosestFallback() {
throw new IllegalFormatWidthException(1);
},
new IterableOf<>(
new FallbackFrom<>(
new Fallback.From<>(
IllegalArgumentException.class,
exp -> "Fallback from IllegalArgumentException"
),
new FallbackFrom<>(
new Fallback.From<>(
IllegalFormatException.class,
exp -> expected
)
Expand Down

0 comments on commit 1d98a8a

Please sign in to comment.