All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jooby.funzy.Throwing Maven / Gradle / Ivy

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 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 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 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 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 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 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 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 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 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 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 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 Consumer7 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. * @param Input type. * @return A new consumer with a listener action. */ default Consumer7 onFailure( Class type, java.util.function.Consumer action) { return (v1, v2, v3, v4, v5, v6, v7) -> runOnFailure( () -> tryAccept(v1, v2, v3, v4, v5, v6, v7), 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. * @param Input type. * @return A new consumer. */ default Consumer7 wrap( java.util.function.Function wrapper) { return (v1, v2, v3, v4, v5, v6, v7) -> runWrap(() -> tryAccept(v1, v2, v3, v4, v5, v6, v7), 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 Input type. * @param Exception type. * @return A new consumer. */ default Consumer7 unwrap( Class type) { return (v1, v2, v3, v4, v5, v6, v7) -> runUnwrap(() -> tryAccept(v1, v2, v3, v4, v5, v6, v7), 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. * @param Input type. */ @FunctionalInterface public interface Consumer8 { /** * 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. * @param v8 Argument. * @throws Throwable If something goes wrong. */ void tryAccept(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6, V7 v7, V8 v8) 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. * @param v8 Argument. */ default void accept(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6, V7 v7, V8 v8) { runAction(() -> tryAccept(v1, v2, v3, v4, v5, v6, v7, v8)); } /** * 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. * @param Input type. * @return A new consumer with a listener action. */ default Consumer8 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. * @param Input type. * @param Input type. * @return A new consumer with a listener action. */ default Consumer8 onFailure( Class type, java.util.function.Consumer action) { return (v1, v2, v3, v4, v5, v6, v7, v8) -> runOnFailure( () -> tryAccept(v1, v2, v3, v4, v5, v6, v7, v8), 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. * @param Input type. * @param Input type. * @return A new consumer. */ default Consumer8 wrap( java.util.function.Function wrapper) { return (v1, v2, v3, v4, v5, v6, v7, v8) -> runWrap( () -> tryAccept(v1, v2, v3, v4, v5, v6, v7, v8), 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 Input type. * @param Input type. * @param Exception type. * @return A new consumer. */ default Consumer8 unwrap( Class type) { return (v1, v2, v3, v4, v5, v6, v7, v8) -> runUnwrap( () -> tryAccept(v1, v2, v3, v4, v5, v6, v7, v8), type); } } /** * Throwable version of {@link java.util.function.Function}. * * The {@link #apply(Object)} method throws checked exceptions using {@link #sneakyThrow(Throwable)} method. * * @param Input type. * @param Output type. */ @FunctionalInterface public interface Function extends java.util.function.Function { /** * Apply this function to the given argument and produces a result. * * @param value Input argument. * @return Result. * @throws Throwable If something goes wrong. */ R tryApply(V value) throws Throwable; /** * Apply this function to the given argument and produces a result. * * @param v Input argument. * @return Result. */ @Override default R apply(V v) { return fn(() -> tryApply(v)); } /** * 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 Function 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 Function onFailure(Class type, java.util.function.Consumer action) { return value -> fnOnFailure(() -> tryApply(value), type, action); } /** * Apply this function and wrap any resulting exception. * * @param wrapper Exception wrapper. * @return A new function. */ default Function wrap(java.util.function.Function wrapper) { return value -> fnWrap(() -> tryApply(value), 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 Function unwrap(Class type) { return value -> fnUnwrap(() -> tryApply(value), type); } /** * Apply this function and returns the given default value in case of exception. * * @param defaultValue Exceptional default value. * @return A new function. */ default Function orElse(R 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 Function orElse(Supplier defaultValue) { return value -> fn(() -> tryApply(value), defaultValue); } /** * Apply this function or recover from it in case of exception. * * @param fn Exception recover. * @return A new function. */ default Function 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 Function recover(Class type, java.util.function.Function fn) { return value -> fnRecover(() -> tryApply(value), type, fn); } /** * A function that remember/cache previous executions. * * @return A memo function. */ default Function memoized() { if (this instanceof Memoized) { return this; } Map cache = new HashMap<>(); return (Function & Memoized) value -> memo(cache, Arrays.asList(value), () -> tryApply(value)); } } /** * Throwable version of {@link java.util.function.BiFunction}. * * The {@link #apply(Object, Object)} method throws checked exceptions using {@link #sneakyThrow(Throwable)} method. * * @param Input type. * @param Input type. * @param Output type. */ @FunctionalInterface public interface Function2 extends java.util.function.BiFunction { /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @return Result. * @throws Throwable If something goes wrong. */ R tryApply(V1 v1, V2 v2) throws Throwable; /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @return Result. */ @Override default R apply(V1 v1, V2 v2) { return fn(() -> tryApply(v1, v2)); } /** * 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 Function2 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 Function2 onFailure(Class type, java.util.function.Consumer action) { return (v1, v2) -> fnOnFailure(() -> tryApply(v1, v2), type, action); } /** * Apply this function and wrap any resulting exception. * * @param wrapper Exception wrapper. * @return A new function. */ default Function2 wrap(java.util.function.Function wrapper) { return (v1, v2) -> fnWrap(() -> tryApply(v1, v2), 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 Function2 unwrap(Class type) { return (v1, v2) -> fnUnwrap(() -> tryApply(v1, v2), type); } /** * Apply this function and returns the given default value in case of exception. * * @param defaultValue Exceptional default value. * @return A new function. */ default Function2 orElse(R 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 Function2 orElse(Supplier defaultValue) { return (v1, v2) -> fn(() -> tryApply(v1, v2), defaultValue); } /** * Apply this function or recover from it in case of exception. * * @param fn Exception recover. * @return A new function. */ default Function2 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 Function2 recover(Class type, java.util.function.Function fn) { return (v1, v2) -> fnRecover(() -> tryApply(v1, v2), type, fn); } /** * A function that remember/cache previous executions. * * @return A memo function. */ default Function2 memoized() { if (this instanceof Memoized) { return this; } Map cache = new HashMap<>(); return (Function2 & Memoized) (v1, v2) -> memo(cache, Arrays.asList(v1, v2), () -> tryApply(v1, v2)); } } /** * Function with three arguments. * * The {@link #apply(Object, Object, Object)} method throws checked exceptions using {@link #sneakyThrow(Throwable)} method. * * @param Input type. * @param Input type. * @param Input type. * @param Output type. */ @FunctionalInterface public interface Function3 { /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @return Result. * @throws Throwable If something goes wrong. */ R tryApply(V1 v1, V2 v2, V3 v3) throws Throwable; /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @return Result. */ default R apply(V1 v1, V2 v2, V3 v3) { return fn(() -> tryApply(v1, v2, v3)); } /** * 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 Function3 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 Function3 onFailure(Class type, java.util.function.Consumer action) { return (v1, v2, v3) -> fnOnFailure(() -> tryApply(v1, v2, v3), type, action); } /** * Apply this function and wrap any resulting exception. * * @param wrapper Exception wrapper. * @return A new function. */ default Function3 wrap( java.util.function.Function wrapper) { return (v1, v2, v3) -> fnWrap(() -> tryApply(v1, v2, v3), 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 Function3 unwrap(Class type) { return (v1, v2, v3) -> fnUnwrap(() -> tryApply(v1, v2, v3), type); } /** * Apply this function and returns the given default value in case of exception. * * @param defaultValue Exceptional default value. * @return A new function. */ default Function3 orElse(R 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 Function3 orElse(Supplier defaultValue) { return (v1, v2, v3) -> fn(() -> tryApply(v1, v2, v3), defaultValue); } /** * Apply this function or recover from it in case of exception. * * @param fn Exception recover. * @return A new function. */ default Function3 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 Function3 recover(Class type, java.util.function.Function fn) { return (v1, v2, v3) -> fnRecover(() -> tryApply(v1, v2, v3), type, fn); } /** * A function that remember/cache previous executions. * * @return A memo function. */ default Function3 memoized() { if (this instanceof Memoized) { return this; } Map cache = new HashMap<>(); return (Function3 & Memoized) (v1, v2, v3) -> memo(cache, Arrays.asList(v1, v2, v3), () -> tryApply(v1, v2, v3)); } } /** * Function with four arguments. * * The {@link #apply(Object, Object, Object, Object)} method throws checked exceptions using {@link #sneakyThrow(Throwable)} method. * * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Output type. */ @FunctionalInterface public interface Function4 { /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @param v4 Input argument. * @return Result. * @throws Throwable If something goes wrong. */ R tryApply(V1 v1, V2 v2, V3 v3, V4 v4) throws Throwable; /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @param v4 Input argument. * @return Result. */ default R apply(V1 v1, V2 v2, V3 v3, V4 v4) { return fn(() -> tryApply(v1, v2, v3, v4)); } /** * 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 Function4 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 Function4 onFailure(Class type, java.util.function.Consumer action) { return (v1, v2, v3, v4) -> fnOnFailure(() -> tryApply(v1, v2, v3, v4), type, action); } /** * Apply this function and wrap any resulting exception. * * @param wrapper Exception wrapper. * @return A new function. */ default Function4 wrap( java.util.function.Function wrapper) { return (v1, v2, v3, v4) -> fnWrap(() -> tryApply(v1, v2, v3, v4), 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 Function4 unwrap(Class type) { return (v1, v2, v3, v4) -> fnUnwrap(() -> tryApply(v1, v2, v3, v4), type); } /** * Apply this function and returns the given default value in case of exception. * * @param defaultValue Exceptional default value. * @return A new function. */ default Function4 orElse(R 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 Function4 orElse(Supplier defaultValue) { return (v1, v2, v3, v4) -> fn(() -> tryApply(v1, v2, v3, v4), defaultValue); } /** * Apply this function or recover from it in case of exception. * * @param fn Exception recover. * @return A new function. */ default Function4 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 Function4 recover(Class type, java.util.function.Function fn) { return (v1, v2, v3, v4) -> fnRecover(() -> tryApply(v1, v2, v3, v4), type, fn); } /** * A function that remember/cache previous executions. * * @return A memo function. */ default Function4 memoized() { if (this instanceof Memoized) { return this; } Map cache = new HashMap<>(); return (Function4 & Memoized) (v1, v2, v3, v4) -> memo(cache, Arrays.asList(v1, v2, v3, v4), () -> tryApply(v1, v2, v3, v4)); } } /** * Function with five arguments. * * The {@link #apply(Object, Object, Object, Object, Object)} method throws checked exceptions using {@link #sneakyThrow(Throwable)} method. * * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Output type. */ @FunctionalInterface public interface Function5 { /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @param v4 Input argument. * @param v5 Input argument. * @return Result. * @throws Throwable If something goes wrong. */ R tryApply(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5) throws Throwable; /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @param v4 Input argument. * @param v5 Input argument. * @return Result. */ default R apply(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5) { return fn(() -> tryApply(v1, v2, v3, v4, v5)); } /** * 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 Function5 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 Function5 onFailure(Class type, java.util.function.Consumer action) { return (v1, v2, v3, v4, v5) -> fnOnFailure(() -> tryApply(v1, v2, v3, v4, v5), type, action); } /** * Apply this function and wrap any resulting exception. * * @param wrapper Exception wrapper. * @return A new function. */ default Function5 wrap( java.util.function.Function wrapper) { return (v1, v2, v3, v4, v5) -> fnWrap(() -> tryApply(v1, v2, v3, v4, v5), 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 Function5 unwrap(Class type) { return (v1, v2, v3, v4, v5) -> fnUnwrap(() -> tryApply(v1, v2, v3, v4, v5), type); } /** * Apply this function and returns the given default value in case of exception. * * @param defaultValue Exceptional default value. * @return A new function. */ default Function5 orElse(R 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 Function5 orElse(Supplier defaultValue) { return (v1, v2, v3, v4, v5) -> fn(() -> tryApply(v1, v2, v3, v4, v5), defaultValue); } /** * Apply this function or recover from it in case of exception. * * @param fn Exception recover. * @return A new function. */ default Function5 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 Function5 recover(Class type, java.util.function.Function fn) { return (v1, v2, v3, v4, v5) -> fnRecover(() -> tryApply(v1, v2, v3, v4, v5), type, fn); } /** * A function that remember/cache previous executions. * * @return A memo function. */ default Function5 memoized() { if (this instanceof Memoized) { return this; } Map cache = new HashMap<>(); return (Function5 & Memoized) (v1, v2, v3, v4, v5) -> memo(cache, Arrays.asList(v1, v2, v3, v4, v5), () -> tryApply(v1, v2, v3, v4, v5)); } } /** * Function with six arguments. * * The {@link #apply(Object, Object, Object, Object, Object, Object)} method throws checked exceptions using {@link #sneakyThrow(Throwable)} method. * * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Output type. */ @FunctionalInterface public interface Function6 { /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @param v4 Input argument. * @param v5 Input argument. * @param v6 Input argument. * @return Result. * @throws Throwable If something goes wrong. */ R tryApply(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6) throws Throwable; /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @param v4 Input argument. * @param v5 Input argument. * @param v6 Input argument. * @return Result. */ default R apply(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6) { return fn(() -> tryApply(v1, v2, v3, v4, v5, v6)); } /** * 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 Function6 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 Function6 onFailure(Class type, java.util.function.Consumer action) { return (v1, v2, v3, v4, v5, v6) -> fnOnFailure(() -> tryApply(v1, v2, v3, v4, v5, v6), type, action); } /** * Apply this function and wrap any resulting exception. * * @param wrapper Exception wrapper. * @return A new function. */ default Function6 wrap( java.util.function.Function wrapper) { return (v1, v2, v3, v4, v5, v6) -> fnWrap(() -> tryApply(v1, v2, v3, v4, v5, v6), 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 Function6 unwrap( Class type) { return (v1, v2, v3, v4, v5, v6) -> fnUnwrap(() -> tryApply(v1, v2, v3, v4, v5, v6), type); } /** * Apply this function and returns the given default value in case of exception. * * @param defaultValue Exceptional default value. * @return A new function. */ default Function6 orElse(R 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 Function6 orElse(Supplier defaultValue) { return (v1, v2, v3, v4, v5, v6) -> fn(() -> tryApply(v1, v2, v3, v4, v5, v6), defaultValue); } /** * Apply this function or recover from it in case of exception. * * @param fn Exception recover. * @return A new function. */ default Function6 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 Function6 recover( Class type, java.util.function.Function fn) { return (v1, v2, v3, v4, v5, v6) -> fnRecover(() -> tryApply(v1, v2, v3, v4, v5, v6), type, fn); } /** * A function that remember/cache previous executions. * * @return A memo function. */ default Function6 memoized() { if (this instanceof Memoized) { return this; } Map cache = new HashMap<>(); return (Function6 & Memoized) (v1, v2, v3, v4, v5, v6) -> memo( cache, Arrays.asList(v1, v2, v3, v4, v5, v6), () -> tryApply(v1, v2, v3, v4, v5, v6)); } } /** * Function with seven arguments. * * The {@link #apply(Object, Object, Object, Object, Object, Object, Object)} method throws checked exceptions using {@link #sneakyThrow(Throwable)} method. * * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Output type. */ @FunctionalInterface public interface Function7 { /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @param v4 Input argument. * @param v5 Input argument. * @param v6 Input argument. * @param v7 Input argument. * @return Result. * @throws Throwable If something goes wrong. */ R tryApply(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6, V7 v7) throws Throwable; /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @param v4 Input argument. * @param v5 Input argument. * @param v6 Input argument. * @param v7 Input argument. * @return Result. */ default R apply(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6, V7 v7) { return fn(() -> tryApply(v1, v2, v3, v4, v5, v6, v7)); } /** * 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 Function7 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 Function7 onFailure(Class type, java.util.function.Consumer action) { return (v1, v2, v3, v4, v5, v6, v7) -> fnOnFailure(() -> tryApply(v1, v2, v3, v4, v5, v6, v7), type, action); } /** * Apply this function and wrap any resulting exception. * * @param wrapper Exception wrapper. * @return A new function. */ default Function7 wrap( java.util.function.Function wrapper) { return (v1, v2, v3, v4, v5, v6, v7) -> fnWrap(() -> tryApply(v1, v2, v3, v4, v5, v6, v7), 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 Function7 unwrap( Class type) { return (v1, v2, v3, v4, v5, v6, v7) -> fnUnwrap(() -> tryApply(v1, v2, v3, v4, v5, v6, v7), type); } /** * Apply this function and returns the given default value in case of exception. * * @param defaultValue Exceptional default value. * @return A new function. */ default Function7 orElse(R 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 Function7 orElse(Supplier defaultValue) { return (v1, v2, v3, v4, v5, v6, v7) -> fn(() -> tryApply(v1, v2, v3, v4, v5, v6, v7), defaultValue); } /** * Apply this function or recover from it in case of exception. * * @param fn Exception recover. * @return A new function. */ default Function7 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 Function7 recover( Class type, java.util.function.Function fn) { return (v1, v2, v3, v4, v5, v6, v7) -> fnRecover(() -> tryApply(v1, v2, v3, v4, v5, v6, v7), type, fn); } /** * A function that remember/cache previous executions. * * @return A memo function. */ default Function7 memoized() { if (this instanceof Memoized) { return this; } Map cache = new HashMap<>(); return (Function7 & Memoized) (v1, v2, v3, v4, v5, v6, v7) -> memo( cache, Arrays.asList(v1, v2, v3, v4, v5, v6, v7), () -> tryApply(v1, v2, v3, v4, v5, v6, v7)); } } /** * Function with seven arguments. * * The {@link #apply(Object, Object, Object, Object, Object, Object, Object, Object)} method throws checked exceptions using {@link #sneakyThrow(Throwable)} method. * * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Input type. * @param Output type. */ @FunctionalInterface public interface Function8 { /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @param v4 Input argument. * @param v5 Input argument. * @param v6 Input argument. * @param v7 Input argument. * @param v8 Input argument. * @return Result. * @throws Throwable If something goes wrong. */ R tryApply(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6, V7 v7, V8 v8) throws Throwable; /** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @param v3 Input argument. * @param v4 Input argument. * @param v5 Input argument. * @param v6 Input argument. * @param v7 Input argument. * @param v8 Input argument. * @return Result. */ default R apply(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5, V6 v6, V7 v7, V8 v8) { return fn(() -> tryApply(v1, v2, v3, v4, v5, v6, v7, v8)); } /** * 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 Function8 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 Function8 onFailure( Class type, java.util.function.Consumer action) { return (v1, v2, v3, v4, v5, v6, v7, v8) -> fnOnFailure( () -> tryApply(v1, v2, v3, v4, v5, v6, v7, v8), type, action); } /** * Apply this function and wrap any resulting exception. * * @param wrapper Exception wrapper. * @return A new function. */ default Function8 wrap( java.util.function.Function wrapper) { return (v1, v2, v3, v4, v5, v6, v7, v8) -> fnWrap( () -> tryApply(v1, v2, v3, v4, v5, v6, v7, v8), 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 Function8 unwrap( Class type) { return (v1, v2, v3, v4, v5, v6, v7, v8) -> fnUnwrap( () -> tryApply(v1, v2, v3, v4, v5, v6, v7, v8), type); } /** * Apply this function and returns the given default value in case of exception. * * @param defaultValue Exceptional default value. * @return A new function. */ default Function8 orElse(R 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 Function8 orElse(Supplier defaultValue) { return (v1, v2, v3, v4, v5, v6, v7, v8) -> fn(() -> tryApply(v1, v2, v3, v4, v5, v6, v7, v8), defaultValue); } /** * Apply this function or recover from it in case of exception. * * @param fn Exception recover. * @return A new function. */ default Function8 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 Function8 recover( Class type, java.util.function.Function fn) { return (v1, v2, v3, v4, v5, v6, v7, v8) -> fnRecover( () -> tryApply(v1, v2, v3, v4, v5, v6, v7, v8), type, fn); } /** * A function that remember/cache previous executions. * * @return A memo function. */ default Function8 memoized() { if (this instanceof Memoized) { return this; } Map cache = new HashMap<>(); return (Function8 & Memoized) (v1, v2, v3, v4, v5, v6, v7, v8) -> memo( cache, Arrays.asList(v1, v2, v3, v4, v5, v6, v7, v8), () -> tryApply(v1, v2, v3, v4, v5, v6, v7, v8)); } } public final static Predicate throwingPredicate(Predicate predicate) { return predicate; } public final static Predicate2 throwingPredicate(Predicate2 predicate) { return predicate; } /** * Factory method for {@link Runnable}. * * @param action Runnable. * @return Same runnable. */ public final static Runnable throwingRunnable(Runnable action) { return action; } /** * Factory method for {@link Supplier}. * * @param fn Supplier. * @param Resulting value. * @return Same supplier. */ public final static Supplier throwingSupplier(Supplier fn) { return fn; } /** * Factory method for {@link Function} and {@link java.util.function.Function}. * * @param fn Function. * @param Input value. * @param Result value. * @return Same supplier. */ public final static Function throwingFunction(Function fn) { return fn; } /** * Factory method for {@link Function2} and {@link java.util.function.BiFunction}. * * @param fn Function. * @param Input value. * @param Input value. * @param Result value. * @return Same supplier. */ public final static Function2 throwingFunction(Function2 fn) { return fn; } public final static Function3 throwingFunction( Function3 fn) { return fn; } public final static Function4 throwingFunction( Function4 fn) { return fn; } public final static Function5 throwingFunction( Function5 fn) { return fn; } public final static Function6 throwingFunction( Function6 fn) { return fn; } public final static Function7 throwingFunction( Function7 fn) { return fn; } public final static Function8 throwingFunction( Function8 fn) { return fn; } public final static Consumer throwingConsumer(Consumer action) { return action; } public final static Consumer2 throwingConsumer(Consumer2 action) { return action; } public final static Consumer3 throwingConsumer( Consumer3 action) { return action; } public final static Consumer4 throwingConsumer( Consumer4 action) { return action; } public final static Consumer5 throwingConsumer( Consumer5 action) { return action; } public final static Consumer6 throwingConsumer( Consumer6 action) { return action; } public final static Consumer7 throwingConsumer( Consumer7 action) { return action; } public final static Consumer8 throwingConsumer( Consumer8 action) { return action; } /** * Throws any throwable 'sneakily' - you don't need to catch it, nor declare that you throw it * onwards. * The exception is still thrown - javac will just stop whining about it. *

* Example usage: *

public void run() {
   *     throw sneakyThrow(new IOException("You don't need to catch me!"));
   * }
*

* NB: The exception is not wrapped, ignored, swallowed, or redefined. The JVM actually does not * know or care * about the concept of a 'checked exception'. All this method does is hide the act of throwing a * checked exception from the java compiler. *

* Note that this method has a return type of {@code RuntimeException}; it is advised you always * call this * method as argument to the {@code throw} statement to avoid compiler errors regarding no return * statement and similar problems. This method won't of course return an actual * {@code RuntimeException} - * it never returns, it always throws the provided exception. * * @param x The throwable to throw without requiring you to catch its type. * @return A dummy RuntimeException; this method never returns normally, it always throws * an exception! */ public static RuntimeException sneakyThrow(final Throwable x) { if (x == null) { throw new NullPointerException("x"); } sneakyThrow0(x); return null; } /** * True if the given exception is one of {@link InterruptedException}, {@link LinkageError}, * {@link ThreadDeath}, {@link VirtualMachineError}. * * @param x Exception to test. * @return True if the given exception is one of {@link InterruptedException}, {@link LinkageError}, * {@link ThreadDeath}, {@link VirtualMachineError}. */ public static boolean isFatal(Throwable x) { return x instanceof InterruptedException || x instanceof LinkageError || x instanceof ThreadDeath || x instanceof VirtualMachineError; } /** * Make a checked exception un-checked and rethrow it. * * @param x Exception to throw. * @param Exception type. * @throws E Exception to throw. */ @SuppressWarnings("unchecked") private static void sneakyThrow0(final Throwable x) throws E { throw (E) x; } private static void runAction(Runnable action) { try { action.tryRun(); } catch (Throwable x) { throw sneakyThrow(x); } } private static V fn(Supplier fn) { try { return fn.tryGet(); } catch (Throwable x) { throw sneakyThrow(x); } } private static R fn(Supplier fn, Supplier orElse) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } return orElse.get(); } } private static R fnRecover(Supplier fn, Class type, java.util.function.Function recover) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } if (type.isInstance(x)) { return recover.apply(type.cast(x)); } throw sneakyThrow(x); } } private static V fnOnFailure(Supplier fn, Class type, java.util.function.Consumer consumer) { try { return fn.tryGet(); } catch (Throwable x) { if (type.isInstance(x)) { consumer.accept(type.cast(x)); } throw sneakyThrow(x); } } private static V fnWrap(Supplier fn, java.util.function.Function wrapper) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } throw sneakyThrow(wrapper.apply(x)); } } private static V fnUnwrap(Supplier fn, Class type) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } Throwable t = x; if (type.isInstance(x)) { t = Optional.ofNullable(x.getCause()).orElse(x); } throw sneakyThrow(t); } } private static void runOnFailure(Runnable action, Class type, java.util.function.Consumer consumer) { try { action.tryRun(); } catch (Throwable x) { if (type.isInstance(x)) { consumer.accept(type.cast(x)); } throw sneakyThrow(x); } } private static void runWrap(Runnable action, java.util.function.Function wrapper) { try { action.tryRun(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } throw sneakyThrow(wrapper.apply(x)); } } private static void runUnwrap(Runnable action, Class type) { try { action.tryRun(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } Throwable t = x; if (type.isInstance(x)) { t = Optional.ofNullable(x.getCause()).orElse(x); } throw sneakyThrow(t); } } private final static R memo(Map cache, List key, Supplier fn) { synchronized (cache) { R value = cache.get(key); if (value == null) { value = fn.get(); cache.put(key, value); } return value; } } }