From 93f1925ae9955a228a0ca4cfe3051b85ed0ee9ba Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Fri, 8 Jan 2021 08:45:49 +0000 Subject: [PATCH] Refactor FallbackFrom by introducing a new interface #1170 --- src/main/java/org/cactoos/Fallback.java | 43 +++++++++++++++++++ .../java/org/cactoos/func/FallbackFrom.java | 15 +++---- 2 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/cactoos/Fallback.java diff --git a/src/main/java/org/cactoos/Fallback.java b/src/main/java/org/cactoos/Fallback.java new file mode 100644 index 0000000000..66199f4a58 --- /dev/null +++ b/src/main/java/org/cactoos/Fallback.java @@ -0,0 +1,43 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos; + +/** + * Fallback interface. + * + *

There is no thread-safety guarantee. + * + * @param Type of input + * @since 1.0 + */ +public interface Fallback extends Func { + + /** + * Calculate level of support of the given exception. + * @param exception Exception + * @return Level of support: greater or equals to 0 if the target + * is supported and {@link Integer#MIN_VALUE} otherwise + */ + int support(Throwable exception); +} diff --git a/src/main/java/org/cactoos/func/FallbackFrom.java b/src/main/java/org/cactoos/func/FallbackFrom.java index d9d492fd49..2091a6c9dc 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 target) { return new MinOf( new Mapped<>( - supported -> new InheritanceLevel(target, supported).value(), + supported -> new InheritanceLevel(target.getClass(), supported).value(), this.exceptions ) ).intValue();