public Try run(Throwing.Consumer4<R1, R2, R3, R4> fn) { return Try.run(() -> { try (R1 r1 = this.r1.get(); R2 r2 = this.r2.get(); R3 r3 = this.r3.get(); R4 r4 = this.r4.get()) { fn.accept(r1, r2, r3, r4); } }); } }
/** * Execute the given action before throwing the exception. * * @param action Action to execute. * @param <TV1> Input type. * @param <TV2> Input type. * @param <TV3> Input type. * @param <TV4> Input type. * @return A new consumer with a listener action. */ default <TV1 extends V1, TV2 extends V2, TV3 extends V3, TV4 extends V4> Consumer4<TV1, TV2, TV3, TV4> onFailure( java.util.function.Consumer<Throwable> 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 <X> Exception type. * @param <TV1> Input type. * @param <TV2> Input type. * @param <TV3> Input type. * @param <TV4> Input type. * @return A new consumer with a listener action. */ default <TV1 extends V1, TV2 extends V2, TV3 extends V3, TV4 extends V4, X extends Throwable> Consumer4<TV1, TV2, TV3, TV4> onFailure( Class<? extends X> type, java.util.function.Consumer<X> action) { return (v1, v2, v3, v4) -> runOnFailure(() -> tryAccept(v1, v2, v3, v4), type, action); }
default void accept(V1 v1, V2 v2, V3 v3, V4 v4) { runAction(() -> tryAccept(v1, v2, v3, v4)); }
/** * Wrap an exception as new exception provided by the given wrap function. * * @param wrapper Wrap function. * @param <TV1> Input type. * @param <TV2> Input type. * @param <TV3> Input type. * @param <TV4> Input type. * @return A new consumer. */ default <TV1 extends V1, TV2 extends V2, TV3 extends V3, TV4 extends V4> Consumer4<TV1, TV2, TV3, TV4> wrap( java.util.function.Function<Throwable, Exception> 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 <TV1> Input type. * @param <TV2> Input type. * @param <TV3> Input type. * @param <TV4> Input type. * @param <X> Exception type. * @return A new consumer. */ default <TV1 extends V1, TV2 extends V2, TV3 extends V3, TV4 extends V4, X extends Throwable> Consumer4<TV1, TV2, TV3, TV4> unwrap( Class<X> type) { return (v1, v2, v3, v4) -> runUnwrap(() -> tryAccept(v1, v2, v3, v4), type); } }