Skip to content

Commit

Permalink
Refactor FallbackFrom and ScalarWithFallback #895
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier B. OURA committed Jan 8, 2021
1 parent 4c784d9 commit 1a5d730
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
15 changes: 5 additions & 10 deletions src/main/java/org/cactoos/func/FallbackFrom.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.cactoos.func;

import org.cactoos.Fallback;
import org.cactoos.Func;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;
Expand All @@ -37,7 +38,7 @@
* @param <T> Type of result
* @since 0.31
*/
public final class FallbackFrom<T> implements Func<Throwable, T> {
public final class FallbackFrom<T> implements Fallback<T> {

/**
* The list of exceptions supported by this instance.
Expand Down Expand Up @@ -77,17 +78,11 @@ public T apply(final Throwable exp) throws Exception {
return this.func.apply(exp);
}

/**
* Calculate level of support of the given exception type.
* @param target Exception type
* @return Level of support: greater or equals to 0 if the target
* is supported and {@link Integer#MIN_VALUE} otherwise
* @see InheritanceLevel
*/
public Integer support(final Class<? extends Throwable> target) {
@Override
public int support(final Throwable exception) {
return new MinOf(
new Mapped<>(
supported -> new InheritanceLevel(target, supported).value(),
supported -> new InheritanceLevel(exception.getClass(), supported).value(),
this.exceptions
)
).intValue();
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/cactoos/func/FuncWithFallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.sql.SQLException;
import java.sql.SQLRecoverableException;
import org.cactoos.Fallback;
import org.cactoos.Func;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.InheritanceLevel;
Expand Down Expand Up @@ -99,15 +100,15 @@ public final class FuncWithFallback<X, Y> implements Func<X, Y> {
/**
* The fallbacks.
*/
private final Iterable<FallbackFrom<Y>> fallbacks;
private final Iterable<Fallback<Y>> fallbacks;

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
*/
@SuppressWarnings("unchecked")
public FuncWithFallback(final Func<X, Y> fnc, final FallbackFrom<Y> fbk) {
public FuncWithFallback(final Func<X, Y> fnc, final Fallback<Y> fbk) {
this(fnc, new IterableOf<>(fbk));
}

Expand All @@ -117,7 +118,7 @@ public FuncWithFallback(final Func<X, Y> fnc, final FallbackFrom<Y> fbk) {
* @param fbks The fallbacks
*/
public FuncWithFallback(
final Func<X, Y> fnc, final Iterable<FallbackFrom<Y>> fbks
final Func<X, Y> fnc, final Iterable<Fallback<Y>> fbks
) {
this.func = fnc;
this.fallbacks = fbks;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/cactoos/scalar/ScalarWithFallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import java.util.Comparator;
import java.util.Map;
import org.cactoos.Func;
import org.cactoos.Fallback;
import org.cactoos.Scalar;
import org.cactoos.func.FallbackFrom;
import org.cactoos.func.FuncWithFallback;
Expand Down Expand Up @@ -53,7 +53,7 @@ public final class ScalarWithFallback<T> implements Scalar<T> {
/**
* The fallback.
*/
private final Iterable<FallbackFrom<T>> fallbacks;
private final Iterable<Fallback<T>> fallbacks;

/**
* Ctor.
Expand All @@ -64,7 +64,7 @@ public final class ScalarWithFallback<T> implements Scalar<T> {
public ScalarWithFallback(
final Scalar<T> origin,
final Class<? extends Throwable> exception,
final Func<Throwable, T> fallback
final Fallback<T> fallback
) {
this(origin, new IterableOf<Class<? extends Throwable>>(exception), fallback);
}
Expand All @@ -78,11 +78,11 @@ public ScalarWithFallback(
public ScalarWithFallback(
final Scalar<T> origin,
final Iterable<Class<? extends Throwable>> exceptions,
final Func<Throwable, T> fallback
final Fallback<T> fallback
) {
this(
origin,
new IterableOf<FallbackFrom<T>>(
new IterableOf<Fallback<T>>(
new FallbackFrom<>(
exceptions, fallback
)
Expand All @@ -96,7 +96,7 @@ public ScalarWithFallback(
* @param fbks Fallbacks
*/
public ScalarWithFallback(final Scalar<T> origin,
final Iterable<FallbackFrom<T>> fbks) {
final Iterable<Fallback<T>> fbks) {
this.origin = origin;
this.fallbacks = fbks;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public T value() throws Exception {
*/
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
private T fallback(final Throwable exp) throws Exception {
final Sorted<Map.Entry<FallbackFrom<T>, Integer>> candidates =
final Sorted<Map.Entry<Fallback<T>, Integer>> candidates =
new Sorted<>(
Comparator.comparing(Map.Entry::getValue),
new Filtered<>(
Expand All @@ -138,7 +138,7 @@ private T fallback(final Throwable exp) throws Exception {
).value(),
new MapOf<>(
fbk -> fbk,
fbk -> fbk.support(exp.getClass()),
fbk -> fbk.support(exp),
this.fallbacks
).entrySet().iterator()
)
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/org/cactoos/func/FallbackFromTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package org.cactoos.func;

import java.io.IOException;
import java.util.IllegalFormatException;
import java.util.IllegalFormatWidthException;
import org.cactoos.iterable.IterableOf;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
Expand All @@ -35,6 +35,7 @@
*
* @since 0.31
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle MagicNumberCheck (500 lines)
*/
@SuppressWarnings("unchecked")
final class FallbackFromTest {
Expand All @@ -46,7 +47,7 @@ void supportsException() {
new FallbackFrom<>(
new IterableOf<>(IOException.class),
exp -> "IOException fallback"
).support(IOException.class),
).support(new IOException()),
new IsEqual<>(0)
).affirm();
}
Expand All @@ -58,8 +59,8 @@ void supportsInheritedException() {
new FallbackFrom<>(
new IterableOf<>(RuntimeException.class),
exp -> "RuntimeException fallback #1"
).support(IllegalFormatException.class),
new IsEqual<>(2)
).support(new IllegalFormatWidthException(1)),
new IsEqual<>(3)
).affirm();
}

Expand All @@ -70,7 +71,7 @@ void doesNotSupportException() {
new FallbackFrom<>(
new IterableOf<>(RuntimeException.class),
exp -> "RuntimeException fallback #2"
).support(ClassNotFoundException.class),
).support(new ClassNotFoundException()),
new IsEqual<>(Integer.MIN_VALUE)
).affirm();
}
Expand Down
25 changes: 20 additions & 5 deletions src/test/java/org/cactoos/scalar/ScalarWithFallbackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public void usesMainFuncFromExceptionAndFallback() throws Exception {
new ScalarWithFallback<>(
() -> message,
IOException.class,
Throwable::getMessage
new FallbackFrom<>(
IOException.class,
Throwable::getMessage
)
),
new ScalarHasValue<>(message)
).affirm();
Expand All @@ -82,7 +85,10 @@ public void usesMainFuncFromIterableExceptionAndFallback() throws Exception {
new ScalarWithFallback<>(
() -> message,
new IterableOf<>(IOException.class),
Throwable::getMessage
new FallbackFrom<>(
IOException.class,
Throwable::getMessage
)
),
new ScalarHasValue<>(message)
).affirm();
Expand All @@ -100,7 +106,10 @@ public void usesFallback() throws Exception {
new IterableOf<>(
new FallbackFrom<>(
new IterableOf<>(IOException.class),
exp -> message
new FallbackFrom<>(
IOException.class,
exp -> message
)
)
)
),
Expand All @@ -118,7 +127,10 @@ public void usesFallbackFromExceptionAndFallback() throws Exception {
throw new IOException("Failure with IOException (exp & flbck)");
},
IOException.class,
exp -> message
new FallbackFrom<>(
IOException.class,
exp -> message
)
),
new ScalarHasValue<>(message)
).affirm();
Expand All @@ -134,7 +146,10 @@ public void usesFallbackFromIterableExceptionAndFallback() throws Exception {
throw new IOException("Failure with IOException (exp iterable & flbck)");
},
new IterableOf<>(IOException.class),
exp -> message
new FallbackFrom<>(
IOException.class,
exp -> message
)
),
new ScalarHasValue<>(message)
).affirm();
Expand Down

0 comments on commit 1a5d730

Please sign in to comment.