update Try

dev
ehlxr 2020-12-22 15:07:34 +08:00
parent d4b44d2335
commit 5e02c22865
2 changed files with 105 additions and 137 deletions

View File

@ -326,6 +326,12 @@
<artifactId>vavr</artifactId>
<version>0.10.3</version>
</dependency>
<dependency>
<groupId>pl.touk</groupId>
<artifactId>throwing-function</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<build>
<finalName>budd</finalName>

View File

@ -38,30 +38,86 @@ import java.util.function.Supplier;
* @since 2020-12-03 10:37.
*/
public interface Try {
static <T> Try.C<T> of(Consumer<? super T> consumer) {
return new Try.C<>(consumer);
static <T> TryConsumer<T> of(Consumer<? super T> consumer) {
return new TryConsumer<>(consumer);
}
static <R> Try.S<R> of(Supplier<? extends R> supplier) {
return new Try.S<>(supplier);
static <R> TrySupplier<R> of(Supplier<? extends R> supplier) {
return new TrySupplier<>(supplier);
}
static <T, R> Try.F<T, R> of(Function<? super T, ? extends R> function) {
return new Try.F<>(function);
static <T, R> TryFunction<T, R> of(Function<? super T, ? extends R> 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<? super Throwable> throwableConsumer;
private CheckedRunnable1 finallyRunnable;
class Tryable<C> {
Consumer<? super Throwable> throwableConsumer;
TryRunnableFunc finallyRunnable;
Consumer<? super Throwable> 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<? 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() {
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<? 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 Consumer<? super Throwable> throwableConsumer;
private CheckedRunnable1 finallyRunnable;
C(Consumer<? super T> consumer) {
TryConsumer(Consumer<? super T> 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<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 Consumer<? super Throwable> throwableConsumer;
private CheckedRunnable1 finallyRunnable;
S(Supplier<? extends R> supplier) {
TrySupplier(Supplier<? extends R> 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<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 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");
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<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
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<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>
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");
}
}