upgrade Try.java
This commit is contained in:
parent
1f9e1a91e9
commit
f75d44f4e4
@ -32,7 +32,7 @@ public class Try {
|
|||||||
|
|
||||||
public static class V {
|
public static class V {
|
||||||
private final Void v;
|
private final Void v;
|
||||||
private Consumer<? super Throwable> ec;
|
private Consumer<? super Throwable> th;
|
||||||
|
|
||||||
V(Void v) {
|
V(Void v) {
|
||||||
Objects.requireNonNull(v, "No value present");
|
Objects.requireNonNull(v, "No value present");
|
||||||
@ -46,25 +46,26 @@ public class Try {
|
|||||||
try {
|
try {
|
||||||
v.exec();
|
v.exec();
|
||||||
} catch (Throwable e) {
|
} 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}
|
* @return {@link V}
|
||||||
*/
|
*/
|
||||||
public V trap(Consumer<? super Throwable> c) {
|
public V trap(Consumer<? super Throwable> th) {
|
||||||
ec = c;
|
Objects.requireNonNull(th, "No value present");
|
||||||
|
this.th = th;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class C<T> {
|
public static class C<T> {
|
||||||
private final Consumer<? super T> consumer;
|
private final Consumer<? super T> consumer;
|
||||||
private Consumer<? super Throwable> ec;
|
private Consumer<? super Throwable> th;
|
||||||
|
|
||||||
C(Consumer<? super T> consumer) {
|
C(Consumer<? super T> consumer) {
|
||||||
Objects.requireNonNull(consumer, "No value present");
|
Objects.requireNonNull(consumer, "No value present");
|
||||||
@ -80,25 +81,26 @@ public class Try {
|
|||||||
try {
|
try {
|
||||||
consumer.accept(t);
|
consumer.accept(t);
|
||||||
} catch (Throwable e) {
|
} 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}
|
* @return {@link C}
|
||||||
*/
|
*/
|
||||||
public C<T> trap(Consumer<? super Throwable> c) {
|
public C<T> trap(Consumer<? super Throwable> th) {
|
||||||
ec = c;
|
Objects.requireNonNull(th, "No value present");
|
||||||
|
this.th = th;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class S<R> {
|
public static class S<R> {
|
||||||
private final Supplier<? extends R> supplier;
|
private final Supplier<? extends R> supplier;
|
||||||
private Consumer<? super Throwable> ec;
|
private Consumer<? super Throwable> th;
|
||||||
|
|
||||||
S(Supplier<? extends R> supplier) {
|
S(Supplier<? extends R> supplier) {
|
||||||
Objects.requireNonNull(supplier, "No value present");
|
Objects.requireNonNull(supplier, "No value present");
|
||||||
@ -115,7 +117,7 @@ public class Try {
|
|||||||
try {
|
try {
|
||||||
return supplier.get();
|
return supplier.get();
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
Optional.ofNullable(ec).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(th).ifPresent(c -> c.accept(e));
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,7 +126,7 @@ public class Try {
|
|||||||
try {
|
try {
|
||||||
return supplier.get();
|
return supplier.get();
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
Optional.ofNullable(ec).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(th).ifPresent(c -> c.accept(e));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -132,18 +134,19 @@ public class Try {
|
|||||||
/**
|
/**
|
||||||
* 如果有异常,调用自定义异常处理表达式
|
* 如果有异常,调用自定义异常处理表达式
|
||||||
*
|
*
|
||||||
* @param c 自定义异常处理 lambda 表达式
|
* @param th 自定义异常处理 lambda 表达式
|
||||||
* @return {@link S}
|
* @return {@link S}
|
||||||
*/
|
*/
|
||||||
public S<R> trap(Consumer<? super Throwable> c) {
|
public S<R> trap(Consumer<? super Throwable> th) {
|
||||||
ec = c;
|
Objects.requireNonNull(th, "No value present");
|
||||||
|
this.th = th;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class F<T, R> {
|
public static class F<T, R> {
|
||||||
private final Function<? super T, ? extends R> function;
|
private final Function<? super T, ? extends R> function;
|
||||||
private Consumer<? super Throwable> ec;
|
private Consumer<? super Throwable> th;
|
||||||
|
|
||||||
F(Function<? super T, ? extends R> function) {
|
F(Function<? super T, ? extends R> function) {
|
||||||
Objects.requireNonNull(function, "No value present");
|
Objects.requireNonNull(function, "No value present");
|
||||||
@ -161,7 +164,7 @@ public class Try {
|
|||||||
try {
|
try {
|
||||||
return function.apply(t);
|
return function.apply(t);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
Optional.ofNullable(ec).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(th).ifPresent(c -> c.accept(e));
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -170,7 +173,7 @@ public class Try {
|
|||||||
try {
|
try {
|
||||||
return function.apply(t);
|
return function.apply(t);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
Optional.ofNullable(ec).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(th).ifPresent(c -> c.accept(e));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -178,11 +181,12 @@ public class Try {
|
|||||||
/**
|
/**
|
||||||
* 如果有异常,调用自定义异常处理表达式
|
* 如果有异常,调用自定义异常处理表达式
|
||||||
*
|
*
|
||||||
* @param c 自定义异常处理 lambda 表达式
|
* @param th 自定义异常处理 lambda 表达式
|
||||||
* @return {@link F}
|
* @return {@link F}
|
||||||
*/
|
*/
|
||||||
public F<T, R> trap(Consumer<? super Throwable> c) {
|
public F<T, R> trap(Consumer<? super Throwable> th) {
|
||||||
ec = c;
|
Objects.requireNonNull(th, "No value present");
|
||||||
|
this.th = th;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user