/** * Always run the given action, works like a finally clause. Exception and value might be null. * Exception will be null in case of success. * * @param action Finally action. * @return This try result. */ public Value<V> onComplete(final Throwing.Consumer2<V, Throwable> action) { try { V value = isSuccess() ? get() : null; action.accept(value, getCause().orElse(null)); return this; } catch (Throwable x) { return (Value<V>) failure(x); } }
/** * Unwrap an exception and rethrow. Useful to produce clean/shorter stacktraces. * * @param type Type to unwrap. * @param <TV1> Input type. * @param <TV2> Input type. * @param <X> Exception type. * @return A new consumer. */ default <TV1 extends V1, TV2 extends V2, X extends Throwable> Consumer2<TV1, TV2> unwrap( Class<X> type) { return (v1, v2) -> runUnwrap(() -> tryAccept(v1, v2), type); } }
default void accept(V1 v1, V2 v2) { runAction(() -> tryAccept(v1, v2)); }
/** * Execute the given action before throwing the exception. * * @param type Exception type filter. * @param action Action to execute. * @param <X> Exception type. * @param <TV1> Input type. * @param <TV2> Input type. * @return A new consumer with a listener action. */ default <TV1 extends V1, TV2 extends V2, X extends Throwable> Consumer2<TV1, TV2> onFailure( Class<? extends X> type, java.util.function.Consumer<X> action) { return (v1, v2) -> runOnFailure(() -> tryAccept(v1, v2), type, action); }
public Try run(Throwing.Consumer2<R1, R2> fn) { return Try.run(() -> { try (R1 r1 = this.r1.get(); R2 r2 = this.r2.get()) { fn.accept(r1, r2); } }); } }
/** * Wrap an exception as new exception provided by the given wrap function. * * @param wrapper Wrap function. * @param <TV1> Input type. * @param <TV2> Input type. * @return A new consumer. */ default <TV1 extends V1, TV2 extends V2> Consumer2<TV1, TV2> wrap( java.util.function.Function<Throwable, Exception> wrapper) { return (v1, v2) -> runWrap(() -> tryAccept(v1, v2), wrapper); }
/** * Execute the given action before throwing the exception. * * @param action Action to execute. * @param <TV1> Input type. * @param <TV2> Input type. * @return A new consumer with a listener action. */ default <TV1 extends V1, TV2 extends V2> Consumer2<TV1, TV2> onFailure( java.util.function.Consumer<Throwable> action) { return onFailure(Throwable.class, action); }