From 1a5d730eaab72494bc01b1d8b5b612837ab8ab99 Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Fri, 8 Jan 2021 10:40:53 +0000 Subject: [PATCH] Refactor FallbackFrom and ScalarWithFallback #895 --- .../java/org/cactoos/func/FallbackFrom.java | 15 ++++------- .../org/cactoos/func/FuncWithFallback.java | 7 +++--- .../cactoos/scalar/ScalarWithFallback.java | 16 ++++++------ .../org/cactoos/func/FallbackFromTest.java | 11 ++++---- .../scalar/ScalarWithFallbackTest.java | 25 +++++++++++++++---- 5 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/cactoos/func/FallbackFrom.java b/src/main/java/org/cactoos/func/FallbackFrom.java index d9d492fd49..9e7ac5dc9e 100644 --- a/src/main/java/org/cactoos/func/FallbackFrom.java +++ b/src/main/java/org/cactoos/func/FallbackFrom.java @@ -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; @@ -37,7 +38,7 @@ * @param Type of result * @since 0.31 */ -public final class FallbackFrom implements Func { +public final class FallbackFrom implements Fallback { /** * The list of exceptions supported by this instance. @@ -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 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(); diff --git a/src/main/java/org/cactoos/func/FuncWithFallback.java b/src/main/java/org/cactoos/func/FuncWithFallback.java index 1b12249fab..e0a20f6f62 100644 --- a/src/main/java/org/cactoos/func/FuncWithFallback.java +++ b/src/main/java/org/cactoos/func/FuncWithFallback.java @@ -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; @@ -99,7 +100,7 @@ public final class FuncWithFallback implements Func { /** * The fallbacks. */ - private final Iterable> fallbacks; + private final Iterable> fallbacks; /** * Ctor. @@ -107,7 +108,7 @@ public final class FuncWithFallback implements Func { * @param fbk The fallback */ @SuppressWarnings("unchecked") - public FuncWithFallback(final Func fnc, final FallbackFrom fbk) { + public FuncWithFallback(final Func fnc, final Fallback fbk) { this(fnc, new IterableOf<>(fbk)); } @@ -117,7 +118,7 @@ public FuncWithFallback(final Func fnc, final FallbackFrom fbk) { * @param fbks The fallbacks */ public FuncWithFallback( - final Func fnc, final Iterable> fbks + final Func fnc, final Iterable> fbks ) { this.func = fnc; this.fallbacks = fbks; diff --git a/src/main/java/org/cactoos/scalar/ScalarWithFallback.java b/src/main/java/org/cactoos/scalar/ScalarWithFallback.java index 78592b5181..11e5e49875 100644 --- a/src/main/java/org/cactoos/scalar/ScalarWithFallback.java +++ b/src/main/java/org/cactoos/scalar/ScalarWithFallback.java @@ -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; @@ -53,7 +53,7 @@ public final class ScalarWithFallback implements Scalar { /** * The fallback. */ - private final Iterable> fallbacks; + private final Iterable> fallbacks; /** * Ctor. @@ -64,7 +64,7 @@ public final class ScalarWithFallback implements Scalar { public ScalarWithFallback( final Scalar origin, final Class exception, - final Func fallback + final Fallback fallback ) { this(origin, new IterableOf>(exception), fallback); } @@ -78,11 +78,11 @@ public ScalarWithFallback( public ScalarWithFallback( final Scalar origin, final Iterable> exceptions, - final Func fallback + final Fallback fallback ) { this( origin, - new IterableOf>( + new IterableOf>( new FallbackFrom<>( exceptions, fallback ) @@ -96,7 +96,7 @@ public ScalarWithFallback( * @param fbks Fallbacks */ public ScalarWithFallback(final Scalar origin, - final Iterable> fbks) { + final Iterable> fbks) { this.origin = origin; this.fallbacks = fbks; } @@ -126,7 +126,7 @@ public T value() throws Exception { */ @SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes") private T fallback(final Throwable exp) throws Exception { - final Sorted, Integer>> candidates = + final Sorted, Integer>> candidates = new Sorted<>( Comparator.comparing(Map.Entry::getValue), new Filtered<>( @@ -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() ) diff --git a/src/test/java/org/cactoos/func/FallbackFromTest.java b/src/test/java/org/cactoos/func/FallbackFromTest.java index 600e41aacf..23d270970e 100644 --- a/src/test/java/org/cactoos/func/FallbackFromTest.java +++ b/src/test/java/org/cactoos/func/FallbackFromTest.java @@ -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; @@ -35,6 +35,7 @@ * * @since 0.31 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) */ @SuppressWarnings("unchecked") final class FallbackFromTest { @@ -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(); } @@ -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(); } @@ -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(); } diff --git a/src/test/java/org/cactoos/scalar/ScalarWithFallbackTest.java b/src/test/java/org/cactoos/scalar/ScalarWithFallbackTest.java index 054c153b48..12d79f621f 100644 --- a/src/test/java/org/cactoos/scalar/ScalarWithFallbackTest.java +++ b/src/test/java/org/cactoos/scalar/ScalarWithFallbackTest.java @@ -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(); @@ -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(); @@ -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 + ) ) ) ), @@ -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(); @@ -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();