Skip to content

Commit

Permalink
Introduce Varargs and removeexceptions in ScalarWithFallback constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier B. OURA committed Jan 11, 2021
1 parent b6fc5f4 commit 0c9f2af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 48 deletions.
31 changes: 4 additions & 27 deletions src/main/java/org/cactoos/scalar/ScalarWithFallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Map;
import org.cactoos.Fallback;
import org.cactoos.Scalar;
import org.cactoos.func.FallbackFrom;
import org.cactoos.func.FuncWithFallback;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterator.Filtered;
Expand Down Expand Up @@ -58,36 +57,14 @@ public final class ScalarWithFallback<T> implements Scalar<T> {
/**
* Ctor.
* @param origin Original scalar
* @param exception Supported exception type
* @param fallback Function that converts the given exception into fallback value
* @param fbks The fallbacks
*/
@SafeVarargs
public ScalarWithFallback(
final Scalar<T> origin,
final Class<? extends Throwable> exception,
final Fallback<T> fallback
final Fallback<T>... fbks
) {
this(origin, new IterableOf<Class<? extends Throwable>>(exception), fallback);
}

/**
* Ctor.
* @param origin Original scalar
* @param exceptions Supported exceptions types
* @param fallback Function that converts the given exception into fallback value
*/
public ScalarWithFallback(
final Scalar<T> origin,
final Iterable<Class<? extends Throwable>> exceptions,
final Fallback<T> fallback
) {
this(
origin,
new IterableOf<Fallback<T>>(
new FallbackFrom<>(
exceptions, fallback
)
)
);
this(origin, new IterableOf<>(fbks));
}

/**
Expand Down
39 changes: 18 additions & 21 deletions src/test/java/org/cactoos/scalar/ScalarWithFallbackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import java.io.IOException;
import java.util.IllegalFormatException;
import java.util.IllegalFormatWidthException;
import org.cactoos.func.FallbackFrom;

import org.cactoos.Fallback;
import org.cactoos.iterable.IterableOf;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
Expand All @@ -50,7 +51,7 @@ public void usesMainFunc() throws Exception {
new ScalarWithFallback<>(
() -> message,
new IterableOf<>(
new FallbackFrom<>(
new Fallback.From<>(
new IterableOf<>(IOException.class),
Throwable::getMessage
)
Expand All @@ -65,10 +66,9 @@ public void usesMainFuncFromExceptionAndFallback() throws Exception {
final String message = "Main function's result #1 (exp & flbck)";
new Assertion<>(
"Using the main function if no exception (exp & flbck)",
new ScalarWithFallback<>(
new ScalarWithFallback<String>(
() -> message,
IOException.class,
new FallbackFrom<>(
new Fallback.From<>(
IOException.class,
Throwable::getMessage
)
Expand All @@ -82,11 +82,10 @@ public void usesMainFuncFromIterableExceptionAndFallback() throws Exception {
final String message = "Main function's result #1 (exp iterable & flbck)";
new Assertion<>(
"Using the main function if no exception (exp iterable & flbck)",
new ScalarWithFallback<>(
new ScalarWithFallback<String>(
() -> message,
new IterableOf<>(IOException.class),
new FallbackFrom<>(
IOException.class,
new Fallback.From<>(
new IterableOf<>(IOException.class),
Throwable::getMessage
)
),
Expand All @@ -104,9 +103,9 @@ public void usesFallback() throws Exception {
throw new IOException("Failure with IOException");
},
new IterableOf<>(
new FallbackFrom<>(
new Fallback.From<>(
new IterableOf<>(IOException.class),
new FallbackFrom<>(
new Fallback.From<>(
IOException.class,
exp -> message
)
Expand All @@ -122,12 +121,11 @@ public void usesFallbackFromExceptionAndFallback() throws Exception {
final String message = "Fallback from IOException (exp & flbck)";
new Assertion<>(
"Using a single fallback in case of exception (exp & flbck)",
new ScalarWithFallback<>(
new ScalarWithFallback<String>(
() -> {
throw new IOException("Failure with IOException (exp & flbck)");
},
IOException.class,
new FallbackFrom<>(
new Fallback.From<>(
IOException.class,
exp -> message
)
Expand All @@ -141,13 +139,12 @@ public void usesFallbackFromIterableExceptionAndFallback() throws Exception {
final String message = "Fallback from IOException (exp iterable & flbck)";
new Assertion<>(
"Using a single fallback in case of exception (exp iterable & flbck)",
new ScalarWithFallback<>(
new ScalarWithFallback<String>(
() -> {
throw new IOException("Failure with IOException (exp iterable & flbck)");
},
new IterableOf<>(IOException.class),
new FallbackFrom<>(
IOException.class,
new Fallback.From<>(
new IterableOf<>(IOException.class),
exp -> message
)
),
Expand All @@ -167,7 +164,7 @@ public void usesFallbackOfInterruptedException() throws Exception {
);
},
new IterableOf<>(
new FallbackFrom<>(
new Fallback.From<>(
new IterableOf<>(InterruptedException.class),
exp -> message
)
Expand All @@ -187,11 +184,11 @@ public void usesTheClosestFallback() throws Exception {
throw new IllegalFormatWidthException(1);
},
new IterableOf<>(
new FallbackFrom<>(
new Fallback.From<>(
new IterableOf<>(IllegalArgumentException.class),
exp -> "Fallback from IllegalArgumentException"
),
new FallbackFrom<>(
new Fallback.From<>(
new IterableOf<>(IllegalFormatException.class),
exp -> expected
)
Expand Down

0 comments on commit 0c9f2af

Please sign in to comment.