update Try
This commit is contained in:
parent
d4b44d2335
commit
5e02c22865
6
pom.xml
6
pom.xml
@ -326,6 +326,12 @@
|
|||||||
<artifactId>vavr</artifactId>
|
<artifactId>vavr</artifactId>
|
||||||
<version>0.10.3</version>
|
<version>0.10.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>pl.touk</groupId>
|
||||||
|
<artifactId>throwing-function</artifactId>
|
||||||
|
<version>1.3</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<finalName>budd</finalName>
|
<finalName>budd</finalName>
|
||||||
|
@ -38,30 +38,86 @@ import java.util.function.Supplier;
|
|||||||
* @since 2020-12-03 10:37.
|
* @since 2020-12-03 10:37.
|
||||||
*/
|
*/
|
||||||
public interface Try {
|
public interface Try {
|
||||||
static <T> Try.C<T> of(Consumer<? super T> consumer) {
|
static <T> TryConsumer<T> of(Consumer<? super T> consumer) {
|
||||||
return new Try.C<>(consumer);
|
return new TryConsumer<>(consumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static <R> Try.S<R> of(Supplier<? extends R> supplier) {
|
static <R> TrySupplier<R> of(Supplier<? extends R> supplier) {
|
||||||
return new Try.S<>(supplier);
|
return new TrySupplier<>(supplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
static <T, R> Try.F<T, R> of(Function<? super T, ? extends R> function) {
|
static <T, R> TryFunction<T, R> of(Function<? super T, ? extends R> function) {
|
||||||
return new Try.F<>(function);
|
return new TryFunction<>(function);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Try.V of(CheckedRunnable0 v) {
|
static TryRunnable of(TryRunnableFunc tryRunnableFunc) {
|
||||||
return new Try.V(v);
|
return new TryRunnable(tryRunnableFunc);
|
||||||
}
|
}
|
||||||
|
|
||||||
class V {
|
class Tryable<C> {
|
||||||
private final CheckedRunnable0 checkedRunnable0;
|
Consumer<? super Throwable> throwableConsumer;
|
||||||
private Consumer<? super Throwable> throwableConsumer;
|
TryRunnableFunc finallyRunnable;
|
||||||
private CheckedRunnable1 finallyRunnable;
|
Consumer<? super Throwable> finallyThrowableConsumer;
|
||||||
|
C c;
|
||||||
|
|
||||||
V(CheckedRunnable0 checkedRunnable0) {
|
/**
|
||||||
Objects.requireNonNull(checkedRunnable0, "No checkedRunnable0 present");
|
* 处理 finally
|
||||||
this.checkedRunnable0 = checkedRunnable0;
|
*/
|
||||||
|
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<? super Throwable> 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<? super Throwable> finallyThrowableConsumer) {
|
||||||
|
Objects.requireNonNull(finallyThrowableConsumer, "No finallyThrowableConsumer present");
|
||||||
|
this.finallyThrowableConsumer = finallyThrowableConsumer;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TryRunnable extends Tryable<TryRunnable> {
|
||||||
|
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() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
checkedRunnable0.run();
|
tryRunnableFunc.run();
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
||||||
} finally {
|
} finally {
|
||||||
Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run);
|
dealFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 如果有异常,调用自定义异常处理表达式
|
|
||||||
*
|
|
||||||
* @param throwableConsumer 自定义异常处理 lambda 表达式
|
|
||||||
* @return {@link V}
|
|
||||||
*/
|
|
||||||
public V trap(Consumer<? super Throwable> 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<T> {
|
class TryConsumer<T> extends Tryable<TryConsumer<T>> {
|
||||||
private final Consumer<? super T> consumer;
|
private final Consumer<? super T> consumer;
|
||||||
private Consumer<? super Throwable> throwableConsumer;
|
|
||||||
private CheckedRunnable1 finallyRunnable;
|
|
||||||
|
|
||||||
C(Consumer<? super T> consumer) {
|
TryConsumer(Consumer<? super T> consumer) {
|
||||||
Objects.requireNonNull(consumer, "No consumer present");
|
Objects.requireNonNull(consumer, "No consumer present");
|
||||||
this.consumer = consumer;
|
this.consumer = consumer;
|
||||||
|
|
||||||
|
super.c = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,43 +155,19 @@ public interface Try {
|
|||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
||||||
} finally {
|
} finally {
|
||||||
Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run);
|
dealFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 如果有异常,调用自定义异常处理表达式
|
|
||||||
*
|
|
||||||
* @param throwableConsumer 自定义异常处理 lambda 表达式
|
|
||||||
* @return {@link C}
|
|
||||||
*/
|
|
||||||
public C<T> trap(Consumer<? super Throwable> throwableConsumer) {
|
|
||||||
Objects.requireNonNull(throwableConsumer, "No throwableConsumer present");
|
|
||||||
this.throwableConsumer = throwableConsumer;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 自定义 finally 处理表达式
|
|
||||||
*
|
|
||||||
* @param finallyRunnable finally 处理 lambda 表达式
|
|
||||||
* @return {@link C}
|
|
||||||
*/
|
|
||||||
public C<T> andFinally(CheckedRunnable1 finallyRunnable) {
|
|
||||||
Objects.requireNonNull(finallyRunnable, "No finallyRunnable present");
|
|
||||||
this.finallyRunnable = finallyRunnable;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class S<R> {
|
class TrySupplier<R> extends Tryable<TrySupplier<R>> {
|
||||||
private final Supplier<? extends R> supplier;
|
private final Supplier<? extends R> supplier;
|
||||||
private Consumer<? super Throwable> throwableConsumer;
|
|
||||||
private CheckedRunnable1 finallyRunnable;
|
|
||||||
|
|
||||||
S(Supplier<? extends R> supplier) {
|
TrySupplier(Supplier<? extends R> supplier) {
|
||||||
Objects.requireNonNull(supplier, "No supplier present");
|
Objects.requireNonNull(supplier, "No supplier present");
|
||||||
this.supplier = supplier;
|
this.supplier = supplier;
|
||||||
|
|
||||||
|
super.c = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -175,7 +183,7 @@ public interface Try {
|
|||||||
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
||||||
return r;
|
return r;
|
||||||
} finally {
|
} finally {
|
||||||
Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run);
|
dealFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,43 +194,19 @@ public interface Try {
|
|||||||
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run);
|
dealFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 如果有异常,调用自定义异常处理表达式
|
|
||||||
*
|
|
||||||
* @param throwableConsumer 自定义异常处理 lambda 表达式
|
|
||||||
* @return {@link S}
|
|
||||||
*/
|
|
||||||
public S<R> trap(Consumer<? super Throwable> throwableConsumer) {
|
|
||||||
Objects.requireNonNull(throwableConsumer, "No throwableConsumer present");
|
|
||||||
this.throwableConsumer = throwableConsumer;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 自定义 finally 处理表达式
|
|
||||||
*
|
|
||||||
* @param finallyRunnable finally 处理 lambda 表达式
|
|
||||||
* @return {@link S}
|
|
||||||
*/
|
|
||||||
public S<R> andFinally(CheckedRunnable1 finallyRunnable) {
|
|
||||||
Objects.requireNonNull(finallyRunnable, "No finallyRunnable present");
|
|
||||||
this.finallyRunnable = finallyRunnable;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class F<T, R> {
|
class TryFunction<T, R> extends Tryable<TryFunction<T, R>> {
|
||||||
private final Function<? super T, ? extends R> function;
|
private final Function<? super T, ? extends R> function;
|
||||||
private Consumer<? super Throwable> throwableConsumer;
|
|
||||||
private CheckedRunnable1 finallyRunnable;
|
|
||||||
|
|
||||||
F(Function<? super T, ? extends R> function) {
|
TryFunction(Function<? super T, ? extends R> function) {
|
||||||
Objects.requireNonNull(function, "No function present");
|
Objects.requireNonNull(function, "No function present");
|
||||||
this.function = function;
|
this.function = function;
|
||||||
|
|
||||||
|
super.c = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -239,7 +223,7 @@ public interface Try {
|
|||||||
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
||||||
return r;
|
return r;
|
||||||
} finally {
|
} finally {
|
||||||
Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run);
|
dealFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,45 +234,16 @@ public interface Try {
|
|||||||
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwableConsumer).ifPresent(c -> c.accept(e));
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
Optional.ofNullable(finallyRunnable).ifPresent(CheckedRunnable1::run);
|
dealFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 如果有异常,调用自定义异常处理表达式
|
|
||||||
*
|
|
||||||
* @param throwableConsumer 自定义异常处理 lambda 表达式
|
|
||||||
* @return {@link F}
|
|
||||||
*/
|
|
||||||
public F<T, R> trap(Consumer<? super Throwable> throwableConsumer) {
|
|
||||||
Objects.requireNonNull(throwableConsumer, "No throwableConsumer present");
|
|
||||||
this.throwableConsumer = throwableConsumer;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 自定义 finally 处理表达式
|
|
||||||
*
|
|
||||||
* @param finallyRunnable finally 处理 lambda 表达式
|
|
||||||
* @return {@link F}
|
|
||||||
*/
|
|
||||||
public F<T, R> andFinally(CheckedRunnable1 finallyRunnable) {
|
|
||||||
Objects.requireNonNull(finallyRunnable, "No finallyRunnable present");
|
|
||||||
this.finallyRunnable = finallyRunnable;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
interface CheckedRunnable0 {
|
interface TryRunnableFunc {
|
||||||
void run() throws Throwable;
|
void run() throws Throwable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
|
||||||
interface CheckedRunnable1 {
|
|
||||||
void run();
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
@SuppressWarnings("ConstantConditions")
|
||||||
static void main(String[] args) {
|
static void main(String[] args) {
|
||||||
// 有返回值,无入参
|
// 有返回值,无入参
|
||||||
@ -301,12 +256,19 @@ public interface Try {
|
|||||||
ArrayList<String> list = null;
|
ArrayList<String> 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.<String>
|
Try.<String>
|
||||||
of(v -> list.add(0, v))
|
of(v -> list.add(0, v))
|
||||||
.trap(e -> System.out.println("222222" + e.getMessage()))
|
.trap(e -> System.out.println("222222" + e.getMessage()))
|
||||||
|
.andFinally(() -> System.out.println("finally"))
|
||||||
|
.finallyTrap(e -> System.out.println(e.getMessage()))
|
||||||
.accept("test");
|
.accept("test");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user