Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#895, #1170) Refactor FallbackFrom and ScalarWithFallback #1525

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions src/main/java/org/cactoos/Fallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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;

import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.scalar.InheritanceLevel;
import org.cactoos.scalar.MinOf;

/**
* Fallback from a {@link Throwable}.
*
* <p>There is no thread-safety guarantee.
*
* @param <X> Type of input
* @since 1.0
*/
public interface Fallback<X> extends Func<Throwable, X> {

/**
* Calculate level of support of the given exception type.
* @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);

/**
* Fallback from exception.
*
* <p>There is no thread-safety guarantee.
*
* @param <T> Type of result
* @since 1.0
*/
final class From<T> implements Fallback<T> {

/**
* The list of exceptions supported by this instance.
*/
private final Iterable<Class<? extends Throwable>> exceptions;

/**
* Function that converts exceptions to the required type.
*/
private final Func<Throwable, T> func;

/**
* Ctor.
* @param exp Supported exception type
* @param func Function that converts the given exception into required one
*/
@SuppressWarnings("unchecked")
public From(final Class<? extends Throwable> exp,
final Func<Throwable, T> func) {
this(new IterableOf<>(exp), func);
}

/**
* Ctor.
* @param exps Supported exceptions types
* @param func Function that converts the given exception into required one
*/
public From(
final Iterable<Class<? extends Throwable>> exps,
final Func<Throwable, T> func) {
this.exceptions = exps;
this.func = func;
}

@Override
public T apply(final Throwable exp) throws Exception {
return this.func.apply(exp);
}

@Override
public int support(final Throwable exception) {
return new MinOf(
new Mapped<>(
supported -> new InheritanceLevel(exception.getClass(), supported).value(),
this.exceptions
)
).intValue();
}
}
}
95 changes: 0 additions & 95 deletions src/main/java/org/cactoos/func/FallbackFrom.java

This file was deleted.

19 changes: 10 additions & 9 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 All @@ -47,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 @@ -69,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 All @@ -99,16 +100,16 @@ 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
* @param fbks The fallbacks
*/
@SuppressWarnings("unchecked")
public FuncWithFallback(final Func<X, Y> fnc, final FallbackFrom<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 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
4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/iterable/IterableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
package org.cactoos.iterable;

import java.util.Iterator;
import org.cactoos.Fallback;
import org.cactoos.Scalar;
import org.cactoos.func.FallbackFrom;
import org.cactoos.iterator.IteratorOf;
import org.cactoos.scalar.And;
import org.cactoos.scalar.Folded;
Expand Down Expand Up @@ -105,7 +105,7 @@ public boolean equals(final Object other) {
)
),
new IterableOf<>(
new FallbackFrom<>(
new Fallback.From<>(
IllegalStateException.class,
ex -> false
)
Expand Down
41 changes: 9 additions & 32 deletions src/main/java/org/cactoos/scalar/ScalarWithFallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@

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;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterator.Filtered;
Expand All @@ -53,41 +52,19 @@ public final class ScalarWithFallback<T> implements Scalar<T> {
/**
* The fallback.
*/
private final Iterable<FallbackFrom<T>> fallbacks;
private final Iterable<Fallback<T>> fallbacks;

/**
* 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 Func<Throwable, 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 Func<Throwable, T> fallback
) {
this(
origin,
new IterableOf<FallbackFrom<T>>(
new FallbackFrom<>(
exceptions, fallback
)
)
);
this(origin, new IterableOf<>(fbks));
}

/**
Expand All @@ -96,7 +73,7 @@ public ScalarWithFallback(
* @param fbks Fallbacks
*/
public ScalarWithFallback(final Scalar<T> origin,
final Iterable<FallbackFrom<T>> fbks) {
final Iterable<Fallback<T>> fbks) {
baudoliver7 marked this conversation as resolved.
Show resolved Hide resolved
this.origin = origin;
this.fallbacks = fbks;
}
Expand Down Expand Up @@ -126,7 +103,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 +115,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
Loading