org.jooby.funzy.Throwing Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hello-world-plugin Show documentation
Show all versions of hello-world-plugin Show documentation
Kill Bill Hello World plugin
The newest version!
package org.jooby.funzy;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
/**
* Collection of throwable interfaces to simplify exception handling, specially on lambdas.
*
* We do provide throwable and 100% compatible implementation of {@link java.util.function.Function},
* {@link java.util.function.Consumer}, {@link java.lang.Runnable},
* {@link java.util.function.Supplier}, {@link java.util.function.Predicate} and
* {@link java.util.function.BiPredicate}.
*
* Examples:
*
* {@code
*
* interface Query {
* Item findById(String id) throws IOException;
* }
*
* Query query = ...
*
* List- items = Arrays.asList("1", "2", "3")
* .stream()
* .map(throwingFunction(query::findById))
* .collect(Collectors.toList());
*
* }
*
*
* @author edgar
* @since 0.1.0
*/
public class Throwing {
private interface Memoized {
}
/**
* Throwable version of {@link Predicate}.
*
* @param Input type.
*/
public interface Predicate extends java.util.function.Predicate {
boolean tryTest(V v) throws Throwable;
@Override default boolean test(V v) {
try {
return tryTest(v);
} catch (Throwable x) {
throw sneakyThrow(x);
}
}
}
/**
* Throwable version of {@link Predicate}.
*
* @param Input type.
* @param Input type.
*/
public interface Predicate2 extends java.util.function.BiPredicate {
boolean tryTest(V1 v1, V2 v2) throws Throwable;
@Override default boolean test(V1 v1, V2 v2) {
try {
return tryTest(v1, v2);
} catch (Throwable x) {
throw sneakyThrow(x);
}
}
}
/**
* Throwable version of {@link java.lang.Runnable}.
*/
@FunctionalInterface
public interface Runnable extends java.lang.Runnable {
void tryRun() throws Throwable;
@Override default void run() {
runAction(this);
}
/**
* Execute the given action before throwing the exception.
*
* @param action Action to execute.
* @return A new consumer with a listener action.
*/
default Runnable onFailure(java.util.function.Consumer action) {
return onFailure(Throwable.class, action);
}
/**
* Execute the given action before throwing the exception.
*
* @param type Exception type filter.
* @param action Action to execute.
* @param Exception type.
* @return A new consumer with a listener action.
*/
default Runnable onFailure(Class extends X> type,
java.util.function.Consumer action) {
return () -> runOnFailure(() -> tryRun(), type, action);
}
/**
* Wrap an exception as new exception provided by the given wrap function.
*
* @param wrapper Wrap function.
* @return A new consumer.
*/
default Runnable wrap(java.util.function.Function wrapper) {
return () -> runWrap(() -> tryRun(), wrapper);
}
/**
* Unwrap an exception and rethrow. Useful to produce clean/shorter stacktraces.
*
* @param type Type to unwrap.
* @param Exception type.
* @return A new consumer.
*/
default Runnable unwrap(Class extends X> type) {
return () -> runUnwrap(() -> tryRun(), type);
}
}
/**
* Throwable version of {@link java.util.function.Supplier}.
*
* @param Result type.
*/
@FunctionalInterface
public interface Supplier extends java.util.function.Supplier {
V tryGet() throws Throwable;
@Override default V get() {
return fn(this);
}
/**
* Apply this function and run the given action in case of exception.
*
* @param action Action to run when exception occurs.
* @return A new function.
*/
default Supplier onFailure(java.util.function.Consumer action) {
return onFailure(Throwable.class, action);
}
/**
*
* Apply this function and run the given action in case of exception.
*
* @param type Exception filter.
* @param action Action to run when exception occurs.
* @param Exception type.
* @return A new function.
*/
default Supplier onFailure(Class type,
java.util.function.Consumer action) {
return () -> fnOnFailure(() -> tryGet(), type, action);
}
/**
* Apply this function and wrap any resulting exception.
*
* @param wrapper Exception wrapper.
* @return A new function.
*/
default Supplier wrap(java.util.function.Function wrapper) {
return () -> fnWrap(() -> tryGet(), wrapper);
}
/**
* Apply this function and unwrap any resulting exception. Useful to get clean/shorter stacktrace.
*
* @param type Exception to unwrap.
* @param Exception type.
* @return A new function.
*/
default Supplier unwrap(Class extends X> type) {
return () -> fnUnwrap(() -> tryGet(), type);
}
/**
* Apply this function and returns the given default value in case of exception.
*
* @param defaultValue Exceptional default value.
* @return A new function.
*/
default Supplier orElse(V defaultValue) {
return orElse(() -> defaultValue);
}
/**
* Apply this function and returns the given default value in case of exception.
*
* @param defaultValue Exceptional default value.
* @return A new function.
*/
default Supplier orElse(Supplier defaultValue) {
return () -> fn(() -> tryGet(), defaultValue);
}
/**
* Apply this function or recover from it in case of exception.
*
* @param fn Exception recover.
* @return A new function.
*/
default Supplier recover(java.util.function.Function fn) {
return recover(Throwable.class, fn);
}
/**
* Apply this function or recover from a specific exception in case of exception.
*
* @param type Exception filter.
* @param fn Exception recover.
* @param Exception type.
* @return A new function.
*/
default Supplier recover(Class extends X> type,
java.util.function.Function fn) {
return () -> fnRecover(() -> tryGet(), type, fn);
}
/**
* Singleton version of this supplier.
*
* @return A memo function.
*/
default Supplier singleton() {
if (this instanceof Memoized) {
return this;
}
AtomicReference ref = new AtomicReference<>();
return (Supplier & Memoized) () -> {
if (ref.get() == null) {
ref.set(tryGet());
}
return ref.get();
};
}
}
/**
* Throwable version of {@link java.util.function.Consumer}.
*
* This class rethrow any exception using the {@link #sneakyThrow(Throwable)} technique.
*
* @param Input type.
*/
@FunctionalInterface
public interface Consumer extends java.util.function.Consumer {
/**
* Performs this operation on the given argument.
*
* @param value Argument.
* @throws Throwable If something goes wrong.
*/
void tryAccept(V value) throws Throwable;
@Override default void accept(V v) {
runAction(() -> tryAccept(v));
}
/**
* Execute the given action before throwing the exception.
*
* @param action Action to execute.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer onFailure(java.util.function.Consumer action) {
return onFailure(Throwable.class, action);
}
/**
* Execute the given action before throwing the exception.
*
* @param type Exception type filter.
* @param action Action to execute.
* @param Exception type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer onFailure(Class extends X> type,
java.util.function.Consumer action) {
return value -> runOnFailure(() -> tryAccept(value), type, action);
}
/**
* Wrap an exception as new exception provided by the given wrap function.
*
* @param wrapper Wrap function.
* @param Input type
* @return A new consumer.
*/
default Consumer wrap(
java.util.function.Function wrapper) {
return value -> runWrap(() -> tryAccept(value), wrapper);
}
/**
* Unwrap an exception and rethrow. Useful to produce clean/shorter stacktraces.
*
* @param type Type to unwrap.
* @param Input type
* @param Exception type.
* @return A new consumer.
*/
default Consumer unwrap(Class extends X> type) {
return value -> runUnwrap(() -> tryAccept(value), type);
}
}
/**
* Two argument version of {@link Consumer}.
*
* This class rethrow any exception using the {@link #sneakyThrow(Throwable)} technique.
*
* @param Input type.
* @param Input type.
*/
@FunctionalInterface
public interface Consumer2 {
/**
* Performs this operation on the given argument.
*
* @param v1 Argument.
* @param v2 Argument.
* @throws Throwable If something goes wrong.
*/
void tryAccept(V1 v1, V2 v2) throws Throwable;
default void accept(V1 v1, V2 v2) {
runAction(() -> tryAccept(v1, v2));
}
/**
* Execute the given action before throwing the exception.
*
* @param action Action to execute.
* @param Input type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer2 onFailure(
java.util.function.Consumer action) {
return onFailure(Throwable.class, action);
}
/**
* Execute the given action before throwing the exception.
*
* @param type Exception type filter.
* @param action Action to execute.
* @param Exception type.
* @param Input type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer2 onFailure(
Class extends X> type, java.util.function.Consumer action) {
return (v1, v2) -> runOnFailure(() -> tryAccept(v1, v2), type, action);
}
/**
* Wrap an exception as new exception provided by the given wrap function.
*
* @param wrapper Wrap function.
* @param Input type.
* @param Input type.
* @return A new consumer.
*/
default Consumer2 wrap(
java.util.function.Function wrapper) {
return (v1, v2) -> runWrap(() -> tryAccept(v1, v2), wrapper);
}
/**
* Unwrap an exception and rethrow. Useful to produce clean/shorter stacktraces.
*
* @param type Type to unwrap.
* @param Input type.
* @param Input type.
* @param Exception type.
* @return A new consumer.
*/
default Consumer2 unwrap(
Class type) {
return (v1, v2) -> runUnwrap(() -> tryAccept(v1, v2), type);
}
}
/**
* Three argument version of {@link Consumer}.
*
* This class rethrow any exception using the {@link #sneakyThrow(Throwable)} technique.
*
* @param Input type.
* @param Input type.
* @param Input type.
*/
@FunctionalInterface
public interface Consumer3 {
/**
* Performs this operation on the given argument.
*
* @param v1 Argument.
* @param v2 Argument.
* @param v3 Argument.
* @throws Throwable If something goes wrong.
*/
void tryAccept(V1 v1, V2 v2, V3 v3) throws Throwable;
default void accept(V1 v1, V2 v2, V3 v3) {
runAction(() -> tryAccept(v1, v2, v3));
}
/**
* Execute the given action before throwing the exception.
*
* @param action Action to execute.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer3 onFailure(
java.util.function.Consumer action) {
return onFailure(Throwable.class, action);
}
/**
* Execute the given action before throwing the exception.
*
* @param type Exception type filter.
* @param action Action to execute.
* @param Exception type.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer3 onFailure(
Class extends X> type, java.util.function.Consumer action) {
return (v1, v2, v3) -> runOnFailure(() -> tryAccept(v1, v2, v3), type, action);
}
/**
* Wrap an exception as new exception provided by the given wrap function.
*
* @param wrapper Wrap function.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer.
*/
default Consumer3 wrap(
java.util.function.Function wrapper) {
return (v1, v2, v3) -> runWrap(() -> tryAccept(v1, v2, v3), wrapper);
}
/**
* Unwrap an exception and rethrow. Useful to produce clean/shorter stacktraces.
*
* @param type Type to unwrap.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Exception type.
* @return A new consumer.
*/
default Consumer3 unwrap(
Class type) {
return (v1, v2, v3) -> runUnwrap(() -> tryAccept(v1, v2, v3), type);
}
}
/**
* Four argument version of {@link Consumer}.
*
* This class rethrow any exception using the {@link #sneakyThrow(Throwable)} technique.
*
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
*/
@FunctionalInterface
public interface Consumer4 {
/**
* Performs this operation on the given arguments.
*
* @param v1 Argument.
* @param v2 Argument.
* @param v3 Argument.
* @param v4 Argument.
* @throws Throwable If something goes wrong.
*/
void tryAccept(V1 v1, V2 v2, V3 v3, V4 v4) throws Throwable;
default void accept(V1 v1, V2 v2, V3 v3, V4 v4) {
runAction(() -> tryAccept(v1, v2, v3, v4));
}
/**
* Execute the given action before throwing the exception.
*
* @param action Action to execute.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer4 onFailure(
java.util.function.Consumer action) {
return onFailure(Throwable.class, action);
}
/**
* Execute the given action before throwing the exception.
*
* @param type Exception type filter.
* @param action Action to execute.
* @param Exception type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer4 onFailure(
Class extends X> type, java.util.function.Consumer action) {
return (v1, v2, v3, v4) -> runOnFailure(() -> tryAccept(v1, v2, v3, v4), type, action);
}
/**
* Wrap an exception as new exception provided by the given wrap function.
*
* @param wrapper Wrap function.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer.
*/
default Consumer4 wrap(
java.util.function.Function wrapper) {
return (v1, v2, v3, v4) -> runWrap(() -> tryAccept(v1, v2, v3, v4), wrapper);
}
/**
* Unwrap an exception and rethrow. Useful to produce clean/shorter stacktraces.
*
* @param type Type to unwrap.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Exception type.
* @return A new consumer.
*/
default Consumer4 unwrap(
Class type) {
return (v1, v2, v3, v4) -> runUnwrap(() -> tryAccept(v1, v2, v3, v4), type);
}
}
/**
* Five argument version of {@link Consumer}.
*
* This class rethrow any exception using the {@link #sneakyThrow(Throwable)} technique.
*
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
*/
@FunctionalInterface
public interface Consumer5 {
/**
* Performs this operation on the given arguments.
*
* @param v1 Argument.
* @param v2 Argument.
* @param v3 Argument.
* @param v4 Argument.
* @param v5 Argument.
* @throws Throwable If something goes wrong.
*/
void tryAccept(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5) throws Throwable;
default void accept(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5) {
runAction(() -> tryAccept(v1, v2, v3, v4, v5));
}
/**
* Execute the given action before throwing the exception.
*
* @param action Action to execute.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer5 onFailure(
java.util.function.Consumer action) {
return onFailure(Throwable.class, action);
}
/**
* Execute the given action before throwing the exception.
*
* @param type Exception type filter.
* @param action Action to execute.
* @param Exception type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer5 onFailure(
Class extends X> type, java.util.function.Consumer action) {
return (v1, v2, v3, v4, v5) -> runOnFailure(() -> tryAccept(v1, v2, v3, v4, v5), type,
action);
}
/**
* Wrap an exception as new exception provided by the given wrap function.
*
* @param wrapper Wrap function.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer.
*/
default Consumer5 wrap(
java.util.function.Function wrapper) {
return (v1, v2, v3, v4, v5) -> runWrap(() -> tryAccept(v1, v2, v3, v4, v5), wrapper);
}
/**
* Unwrap an exception and rethrow. Useful to produce clean/shorter stacktraces.
*
* @param type Type to unwrap.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Exception type.
* @return A new consumer.
*/
default Consumer5 unwrap(
Class type) {
return (v1, v2, v3, v4, v5) -> runUnwrap(() -> tryAccept(v1, v2, v3, v4, v5), type);
}
}
/**
* Six argument version of {@link Consumer}.
*
* This class rethrow any exception using the {@link #sneakyThrow(Throwable)} technique.
*
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
*/
@FunctionalInterface
public interface Consumer6 {
/**
* Performs this operation on the given arguments.
*
* @param v1 Argument.
* @param v2 Argument.
* @param v3 Argument.
* @param v4 Argument.
* @param v5 Argument.
* @param v6 Argument.
* @throws Throwable If something goes wrong.
*/
void tryAccept(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6) throws Throwable;
/**
* Performs this operation on the given arguments and throw any exception using {@link #sneakyThrow(Throwable)} method.
*
* @param v1 Argument.
* @param v2 Argument.
* @param v3 Argument.
* @param v4 Argument.
* @param v5 Argument.
* @param v6 Argument.
*/
default void accept(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6) {
runAction(() -> tryAccept(v1, v2, v3, v4, v5, v6));
}
/**
* Execute the given action before throwing the exception.
*
* @param action Action to execute.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer6 onFailure(
java.util.function.Consumer action) {
return onFailure(Throwable.class, action);
}
/**
* Execute the given action before throwing the exception.
*
* @param type Exception type filter.
* @param action Action to execute.
* @param Exception type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default Consumer6 onFailure(
Class extends X> type, java.util.function.Consumer action) {
return (v1, v2, v3, v4, v5, v6) -> runOnFailure(() -> tryAccept(v1, v2, v3, v4, v5, v6), type,
action);
}
/**
* Wrap an exception as new exception provided by the given wrap function.
*
* @param wrapper Wrap function.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer.
*/
default Consumer6 wrap(
java.util.function.Function wrapper) {
return (v1, v2, v3, v4, v5, v6) -> runWrap(() -> tryAccept(v1, v2, v3, v4, v5, v6), wrapper);
}
/**
* Unwrap an exception and rethrow. Useful to produce clean/shorter stacktraces.
*
* @param type Type to unwrap.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Exception type.
* @return A new consumer.
*/
default Consumer6 unwrap(
Class type) {
return (v1, v2, v3, v4, v5, v6) -> runUnwrap(() -> tryAccept(v1, v2, v3, v4, v5, v6), type);
}
}
/**
* Seven argument version of {@link Consumer}.
*
* This class rethrow any exception using the {@link #sneakyThrow(Throwable)} technique.
*
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
*/
@FunctionalInterface
public interface Consumer7 {
/**
* Performs this operation on the given arguments.
*
* @param v1 Argument.
* @param v2 Argument.
* @param v3 Argument.
* @param v4 Argument.
* @param v5 Argument.
* @param v6 Argument.
* @param v7 Argument.
* @throws Throwable If something goes wrong.
*/
void tryAccept(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6, V7 v7) throws Throwable;
/**
* Performs this operation on the given arguments and throw any exception using {@link #sneakyThrow(Throwable)} method.
*
* @param v1 Argument.
* @param v2 Argument.
* @param v3 Argument.
* @param v4 Argument.
* @param v5 Argument.
* @param v6 Argument.
* @param v7 Argument.
*/
default void accept(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6, V7 v7) {
runAction(() -> tryAccept(v1, v2, v3, v4, v5, v6, v7));
}
/**
* Execute the given action before throwing the exception.
*
* @param action Action to execute.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @param Input type.
* @return A new consumer with a listener action.
*/
default