diff --git a/pom.xml b/pom.xml index 68a5004..4422328 100644 --- a/pom.xml +++ b/pom.xml @@ -269,16 +269,16 @@ jodd-props 3.6.1 - - - - - - - - - - + + + + + + + + + + org.jetbrains.kotlin kotlin-stdlib-jdk8 @@ -321,6 +321,11 @@ 3.14.7 + + io.vavr + vavr + 0.10.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 60e4dae..c828e6e 100644 --- a/src/main/java/io/github/ehlxr/utils/Try.java +++ b/src/main/java/io/github/ehlxr/utils/Try.java @@ -37,62 +37,78 @@ import java.util.function.Supplier; * @author ehlxr * @since 2020-12-03 10:37. */ -public class Try { - public static Try.C of(Consumer consumer) { +public interface Try { + static Try.C of(Consumer consumer) { return new Try.C<>(consumer); } - public static Try.S of(Supplier supplier) { + static Try.S of(Supplier supplier) { return new Try.S<>(supplier); } - public static Try.F of(Function function) { + static Try.F of(Function function) { return new Try.F<>(function); } - public static Try.V of(Void v) { + static Try.V of(CheckedRunnable0 v) { return new Try.V(v); } - public static class V { - private final Void v; - private Consumer th; + class V { + private final CheckedRunnable0 checkedRunnable0; + private Consumer throwableConsumer; + private CheckedRunnable1 finallyRunnable; - V(Void v) { - Objects.requireNonNull(v, "No value present"); - this.v = v; + V(CheckedRunnable0 checkedRunnable0) { + Objects.requireNonNull(checkedRunnable0, "No checkedRunnable0 present"); + this.checkedRunnable0 = checkedRunnable0; } /** * 计算结果 */ - public void exec() { + public void run() { try { - v.exec(); + checkedRunnable0.run(); } catch (Throwable e) { - Optional.ofNullable(th).ifPresent(c -> c.accept(e)); + Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); + } finally { + Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); } } /** * 如果有异常,调用自定义异常处理表达式 * - * @param th 自定义异常处理 lambda 表达式 + * @param throwableConsumer 自定义异常处理 lambda 表达式 * @return {@link V} */ - public V trap(Consumer th) { - Objects.requireNonNull(th, "No value present"); - this.th = th; + 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; } } - public static class C { + class C { private final Consumer consumer; - private Consumer th; + private Consumer throwableConsumer; + private CheckedRunnable1 finallyRunnable; C(Consumer consumer) { - Objects.requireNonNull(consumer, "No value present"); + Objects.requireNonNull(consumer, "No consumer present"); this.consumer = consumer; } @@ -105,29 +121,44 @@ public class Try { try { consumer.accept(t); } catch (Throwable e) { - Optional.ofNullable(th).ifPresent(c -> c.accept(e)); + Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); + } finally { + Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); } } /** * 如果有异常,调用自定义异常处理表达式 * - * @param th 自定义异常处理 lambda 表达式 + * @param throwableConsumer 自定义异常处理 lambda 表达式 * @return {@link C} */ - public C trap(Consumer th) { - Objects.requireNonNull(th, "No value present"); - this.th = th; + 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; } } - public static class S { + class S { private final Supplier supplier; - private Consumer th; + private Consumer throwableConsumer; + private CheckedRunnable1 finallyRunnable; S(Supplier supplier) { - Objects.requireNonNull(supplier, "No value present"); + Objects.requireNonNull(supplier, "No supplier present"); this.supplier = supplier; } @@ -141,8 +172,10 @@ public class Try { try { return supplier.get(); } catch (Throwable e) { - Optional.ofNullable(th).ifPresent(c -> c.accept(e)); + Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); return r; + } finally { + Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); } } @@ -150,30 +183,45 @@ public class Try { try { return supplier.get(); } catch (Throwable e) { - Optional.ofNullable(th).ifPresent(c -> c.accept(e)); + Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); return null; + } finally { + Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); } } /** * 如果有异常,调用自定义异常处理表达式 * - * @param th 自定义异常处理 lambda 表达式 + * @param throwableConsumer 自定义异常处理 lambda 表达式 * @return {@link S} */ - public S trap(Consumer th) { - Objects.requireNonNull(th, "No value present"); - this.th = th; + 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; } } - public static class F { + class F { private final Function function; - private Consumer th; + private Consumer throwableConsumer; + private CheckedRunnable1 finallyRunnable; F(Function function) { - Objects.requireNonNull(function, "No value present"); + Objects.requireNonNull(function, "No function present"); this.function = function; } @@ -188,8 +236,10 @@ public class Try { try { return function.apply(t); } catch (Throwable e) { - Optional.ofNullable(th).ifPresent(c -> c.accept(e)); + Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); return r; + } finally { + Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); } } @@ -197,31 +247,50 @@ public class Try { try { return function.apply(t); } catch (Throwable e) { - Optional.ofNullable(th).ifPresent(c -> c.accept(e)); + Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e)); return null; + } finally { + Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run); } } /** * 如果有异常,调用自定义异常处理表达式 * - * @param th 自定义异常处理 lambda 表达式 + * @param throwableConsumer 自定义异常处理 lambda 表达式 * @return {@link F} */ - public F trap(Consumer th) { - Objects.requireNonNull(th, "No value present"); - this.th = th; + 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 - public interface Void { - void exec() throws Throwable; + interface CheckedRunnable0 { + void run() throws Throwable; + } + + @FunctionalInterface + interface CheckedRunnable1 { + void run(); } @SuppressWarnings("ConstantConditions") - public static void main(String[] args) { + static void main(String[] args) { // 有返回值,无入参 System.out.println(Try.of(() -> Long.valueOf("s")).trap(System.out::println).get(0L)); System.out.println(Try.of(() -> Long.valueOf("2w1")).get()); @@ -232,7 +301,7 @@ public class Try { ArrayList list = null; // 无返回值,无入参 - Try.of(() -> Thread.sleep(-1L)).exec(); + Try.of(() -> Thread.sleep(-1L)).andFinally(() -> System.out.println("ddf")).trap(System.out::println).run(); // 无返回值,有入参 Try.