From f75d44f4e4f9dcae9c7a830cca182948e80c6289 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Mon, 21 Dec 2020 15:40:34 +0800 Subject: [PATCH] upgrade Try.java --- src/main/java/io/github/ehlxr/utils/Try.java | 50 +++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/main/java/io/github/ehlxr/utils/Try.java b/src/main/java/io/github/ehlxr/utils/Try.java index e7a8515..b1c9f36 100644 --- a/src/main/java/io/github/ehlxr/utils/Try.java +++ b/src/main/java/io/github/ehlxr/utils/Try.java @@ -32,7 +32,7 @@ public class Try { public static class V { private final Void v; - private Consumer ec; + private Consumer th; V(Void v) { Objects.requireNonNull(v, "No value present"); @@ -46,25 +46,26 @@ public class Try { try { v.exec(); } catch (Throwable e) { - Optional.ofNullable(ec).ifPresent(c -> c.accept(e)); + Optional.ofNullable(th).ifPresent(c -> c.accept(e)); } } /** * 如果有异常,调用自定义异常处理表达式 * - * @param c 自定义异常处理 lambda 表达式 + * @param th 自定义异常处理 lambda 表达式 * @return {@link V} */ - public V trap(Consumer c) { - ec = c; + public V trap(Consumer th) { + Objects.requireNonNull(th, "No value present"); + this.th = th; return this; } } public static class C { private final Consumer consumer; - private Consumer ec; + private Consumer th; C(Consumer consumer) { Objects.requireNonNull(consumer, "No value present"); @@ -80,25 +81,26 @@ public class Try { try { consumer.accept(t); } catch (Throwable e) { - Optional.ofNullable(ec).ifPresent(c -> c.accept(e)); + Optional.ofNullable(th).ifPresent(c -> c.accept(e)); } } /** - * 如果有异常,调用自定义异常处理表达式 + * 如果有异常,调用自定义异常处理表达式 * - * @param c 自定义异常处理 lambda 表达式 + * @param th 自定义异常处理 lambda 表达式 * @return {@link C} */ - public C trap(Consumer c) { - ec = c; + public C trap(Consumer th) { + Objects.requireNonNull(th, "No value present"); + this.th = th; return this; } } public static class S { private final Supplier supplier; - private Consumer ec; + private Consumer th; S(Supplier supplier) { Objects.requireNonNull(supplier, "No value present"); @@ -115,7 +117,7 @@ public class Try { try { return supplier.get(); } catch (Throwable e) { - Optional.ofNullable(ec).ifPresent(c -> c.accept(e)); + Optional.ofNullable(th).ifPresent(c -> c.accept(e)); return r; } } @@ -124,7 +126,7 @@ public class Try { try { return supplier.get(); } catch (Throwable e) { - Optional.ofNullable(ec).ifPresent(c -> c.accept(e)); + Optional.ofNullable(th).ifPresent(c -> c.accept(e)); return null; } } @@ -132,18 +134,19 @@ public class Try { /** * 如果有异常,调用自定义异常处理表达式 * - * @param c 自定义异常处理 lambda 表达式 + * @param th 自定义异常处理 lambda 表达式 * @return {@link S} */ - public S trap(Consumer c) { - ec = c; + public S trap(Consumer th) { + Objects.requireNonNull(th, "No value present"); + this.th = th; return this; } } public static class F { private final Function function; - private Consumer ec; + private Consumer th; F(Function function) { Objects.requireNonNull(function, "No value present"); @@ -161,7 +164,7 @@ public class Try { try { return function.apply(t); } catch (Throwable e) { - Optional.ofNullable(ec).ifPresent(c -> c.accept(e)); + Optional.ofNullable(th).ifPresent(c -> c.accept(e)); return r; } } @@ -170,7 +173,7 @@ public class Try { try { return function.apply(t); } catch (Throwable e) { - Optional.ofNullable(ec).ifPresent(c -> c.accept(e)); + Optional.ofNullable(th).ifPresent(c -> c.accept(e)); return null; } } @@ -178,11 +181,12 @@ public class Try { /** * 如果有异常,调用自定义异常处理表达式 * - * @param c 自定义异常处理 lambda 表达式 + * @param th 自定义异常处理 lambda 表达式 * @return {@link F} */ - public F trap(Consumer c) { - ec = c; + public F trap(Consumer th) { + Objects.requireNonNull(th, "No value present"); + this.th = th; return this; } }