@Override default void accept(V v) { runAction(() -> tryAccept(v)); }
/** * Execute the given action before throwing the exception. * * @param type Exception type filter. * @param action Action to execute. * @param <X> Exception type. * @param <V1> Input type. * @return A new consumer with a listener action. */ default <V1 extends V, X extends Throwable> Consumer<V1> onFailure(Class<? extends X> type, java.util.function.Consumer<X> 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 <V1> Input type * @return A new consumer. */ default <V1 extends V> Consumer<V1> wrap( java.util.function.Function<Throwable, Exception> wrapper) { return value -> runWrap(() -> tryAccept(value), wrapper); }
/** * Unwrap an exception and rethrow. Useful to produce clean/shorter stacktraces. * * @param type Type to unwrap. * @param <V1> Input type * @param <X> Exception type. * @return A new consumer. */ default <V1 extends V, X extends Throwable> Consumer<V1> unwrap(Class<? extends X> type) { return value -> runUnwrap(() -> tryAccept(value), type); } }