diff --git a/pom.xml b/pom.xml index 4422328..60a2de2 100644 --- a/pom.xml +++ b/pom.xml @@ -326,6 +326,12 @@ vavr 0.10.3 + + + pl.touk + throwing-function + 1.3 + budd diff --git a/src/main/java/io/github/ehlxr/utils/Try.java b/src/main/java/io/github/ehlxr/utils/Try.java index c828e6e..b0df90c 100644 --- a/src/main/java/io/github/ehlxr/utils/Try.java +++ b/src/main/java/io/github/ehlxr/utils/Try.java @@ -38,30 +38,86 @@ import java.util.function.Supplier; * @since 2020-12-03 10:37. */ public interface Try { - static Try.C of(Consumer consumer) { - return new Try.C<>(consumer); + static TryConsumer of(Consumer consumer) { + return new TryConsumer<>(consumer); } - static Try.S of(Supplier supplier) { - return new Try.S<>(supplier); + static TrySupplier of(Supplier supplier) { + return new TrySupplier<>(supplier); } - static Try.F of(Function function) { - return new Try.F<>(function); + static TryFunction of(Function function) { + return new TryFunction<>(function); } - static Try.V of(CheckedRunnable0 v) { - return new Try.V(v); + static TryRunnable of(TryRunnableFunc tryRunnableFunc) { + return new TryRunnable(tryRunnableFunc); } - class V { - private final CheckedRunnable0 checkedRunnable0; - private Consumer throwableConsumer; - private CheckedRunnable1 finallyRunnable; + class Tryable { + Consumer throwableConsumer; + TryRunnableFunc finallyRunnable; + Consumer finallyThrowableConsumer; + C c; - V(CheckedRunnable0 checkedRunnable0) { - Objects.requireNonNull(checkedRunnable0, "No checkedRunnable0 present"); - this.checkedRunnable0 = checkedRunnable0; + /** + * 处理 finally + */ + public void dealFinally() { + Optional.ofNullable(finallyRunnable).ifPresent(tryRunnableFunc1 -> { + try { + tryRunnableFunc1.run(); + } catch (final Throwable t) { + Optional.ofNullable(finallyThrowableConsumer).ifPresent(c -> c.accept(t)); + } + }); + } + + /** + * 如果有异常,调用自定义异常处理表达式 + * + * @param throwableConsumer 自定义异常处理 lambda 表达式 + * @return {@link C} + */ + public C trap(Consumer throwableConsumer) { + Objects.requireNonNull(throwableConsumer, "No throwableConsumer present"); + this.throwableConsumer = throwableConsumer; + return c; + } + + /** + * 自定义 finally 处理表达式 + * + * @param finallyRunnable finally 处理 lambda 表达式 + * @return {@link C} + */ + public C andFinally(TryRunnableFunc finallyRunnable) { + Objects.requireNonNull(finallyRunnable, "No finallyRunnable present"); + this.finallyRunnable = finallyRunnable; + return c; + } + + /** + * 如果 finally 有异常,调用自定义异常处理表达式 + * + * @param finallyThrowableConsumer 自定义异常处理 lambda 表达式 + * @return {@link C} + */ + public C finallyTrap(Consumer finallyThrowableConsumer) { + Objects.requireNonNull(finallyThrowableConsumer, "No finallyThrowableConsumer present"); + this.finallyThrowableConsumer = finallyThrowableConsumer; + return c; + } + } + + class TryRunnable extends Tryable { + private final TryRunnableFunc tryRunnableFunc; + + TryRunnable(TryRunnableFunc tryRunnableFunc) { + Objects.requireNonNull(tryRunnableFunc, "No checkedRunnable present"); + this.tryRunnableFunc = tryRunnableFunc; + + super.c = this; } /** @@ -69,47 +125,23 @@ public interface Try { */ public void run() { try { - checkedRunnable0.run(); + tryRunnableFunc.run(); } catch (Throwable e) { Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); } finally { - Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); + dealFinally(); } } - - /** - * 如果有异常,调用自定义异常处理表达式 - * - * @param throwableConsumer 自定义异常处理 lambda 表达式 - * @return {@link V} - */ - public V trap(Consumer throwableConsumer) { - Objects.requireNonNull(throwableConsumer, "No throwableConsumer present"); - this.throwableConsumer = throwableConsumer; - return this; - } - - /** - * 自定义 finally 处理表达式 - * - * @param finallyRunnable finally 处理 lambda 表达式 - * @return {@link V} - */ - public V andFinally(CheckedRunnable1 finallyRunnable) { - Objects.requireNonNull(finallyRunnable, "No finallyRunnable present"); - this.finallyRunnable = finallyRunnable; - return this; - } } - class C { + class TryConsumer extends Tryable> { private final Consumer consumer; - private Consumer throwableConsumer; - private CheckedRunnable1 finallyRunnable; - C(Consumer consumer) { + TryConsumer(Consumer consumer) { Objects.requireNonNull(consumer, "No consumer present"); this.consumer = consumer; + + super.c = this; } /** @@ -123,43 +155,19 @@ public interface Try { } catch (Throwable e) { Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); } finally { - Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); + dealFinally(); } } - - /** - * 如果有异常,调用自定义异常处理表达式 - * - * @param throwableConsumer 自定义异常处理 lambda 表达式 - * @return {@link C} - */ - public C trap(Consumer throwableConsumer) { - Objects.requireNonNull(throwableConsumer, "No throwableConsumer present"); - this.throwableConsumer = throwableConsumer; - return this; - } - - /** - * 自定义 finally 处理表达式 - * - * @param finallyRunnable finally 处理 lambda 表达式 - * @return {@link C} - */ - public C andFinally(CheckedRunnable1 finallyRunnable) { - Objects.requireNonNull(finallyRunnable, "No finallyRunnable present"); - this.finallyRunnable = finallyRunnable; - return this; - } } - class S { + class TrySupplier extends Tryable> { private final Supplier supplier; - private Consumer throwableConsumer; - private CheckedRunnable1 finallyRunnable; - S(Supplier supplier) { + TrySupplier(Supplier supplier) { Objects.requireNonNull(supplier, "No supplier present"); this.supplier = supplier; + + super.c = this; } /** @@ -175,7 +183,7 @@ public interface Try { Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); return r; } finally { - Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); + dealFinally(); } } @@ -186,43 +194,19 @@ public interface Try { Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); return null; } finally { - Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); + dealFinally(); } } - - /** - * 如果有异常,调用自定义异常处理表达式 - * - * @param throwableConsumer 自定义异常处理 lambda 表达式 - * @return {@link S} - */ - public S trap(Consumer throwableConsumer) { - Objects.requireNonNull(throwableConsumer, "No throwableConsumer present"); - this.throwableConsumer = throwableConsumer; - return this; - } - - /** - * 自定义 finally 处理表达式 - * - * @param finallyRunnable finally 处理 lambda 表达式 - * @return {@link S} - */ - public S andFinally(CheckedRunnable1 finallyRunnable) { - Objects.requireNonNull(finallyRunnable, "No finallyRunnable present"); - this.finallyRunnable = finallyRunnable; - return this; - } } - class F { + class TryFunction extends Tryable> { private final Function function; - private Consumer throwableConsumer; - private CheckedRunnable1 finallyRunnable; - F(Function function) { + TryFunction(Function function) { Objects.requireNonNull(function, "No function present"); this.function = function; + + super.c = this; } /** @@ -239,7 +223,7 @@ public interface Try { Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); return r; } finally { - Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); + dealFinally(); } } @@ -250,45 +234,16 @@ public interface Try { Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); return null; } finally { - Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); + dealFinally(); } } - - /** - * 如果有异常,调用自定义异常处理表达式 - * - * @param throwableConsumer 自定义异常处理 lambda 表达式 - * @return {@link F} - */ - public F trap(Consumer throwableConsumer) { - Objects.requireNonNull(throwableConsumer, "No throwableConsumer present"); - this.throwableConsumer = throwableConsumer; - return this; - } - - /** - * 自定义 finally 处理表达式 - * - * @param finallyRunnable finally 处理 lambda 表达式 - * @return {@link F} - */ - public F andFinally(CheckedRunnable1 finallyRunnable) { - Objects.requireNonNull(finallyRunnable, "No finallyRunnable present"); - this.finallyRunnable = finallyRunnable; - return this; - } } @FunctionalInterface - interface CheckedRunnable0 { + interface TryRunnableFunc { void run() throws Throwable; } - @FunctionalInterface - interface CheckedRunnable1 { - void run(); - } - @SuppressWarnings("ConstantConditions") static void main(String[] args) { // 有返回值,无入参 @@ -301,12 +256,19 @@ public interface Try { ArrayList list = null; // 无返回值,无入参 - Try.of(() -> Thread.sleep(-1L)).andFinally(() -> System.out.println("ddf")).trap(System.out::println).run(); + Try.of(() -> Thread.sleep(-1L)) + .andFinally(() -> list.clear()) + // .andFinally(list::clear) //https://stackoverflow.com/questions/37413106/java-lang-nullpointerexception-is-thrown-using-a-method-reference-but-not-a-lamb + .finallyTrap(e -> System.out.println("list::clear " + e.getMessage())) + .trap(e -> System.out.println(e.getMessage())) + .run(); // 无返回值,有入参 Try. of(v -> list.add(0, v)) .trap(e -> System.out.println("222222" + e.getMessage())) + .andFinally(() -> System.out.println("finally")) + .finallyTrap(e -> System.out.println(e.getMessage())) .accept("test"); } }