update at 2021-02-05 14:15:42 by ehlxr

dev
ehlxr 2021-02-05 14:15:42 +08:00
parent c52cc632fe
commit 4bc3587c59
1 changed files with 110 additions and 54 deletions

View File

@ -24,7 +24,11 @@
package io.github.ehlxr.utils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
@ -83,6 +87,7 @@ public interface Try {
abstract class Tryable<C> {
Consumer<? super Throwable> throwConsumer;
ThrowableRunnable finallyRunnable;
ThrowableConsumer<? super Object> finallyConsumer;
Consumer<? super Throwable> finallyThrowConsumer;
C c;
@ -99,6 +104,24 @@ public interface Try {
});
}
/**
* finally
*
* @param p
* @param <P>
*/
protected <P> void doFinally(P p) {
if (Objects.nonNull(finallyConsumer)) {
try {
finallyConsumer.accept(p);
} catch (final Throwable e) {
Optional.ofNullable(finallyThrowConsumer).ifPresent(tc -> tc.accept(e));
}
} else {
doFinally();
}
}
/**
*
*
@ -113,6 +136,8 @@ public interface Try {
/**
* finally
* <p>
* {@link TryConsumer}{@link TryFunction} {@link #andFinally(ThrowableConsumer)}
*
* @param finallyRunnable finally lambda
* @return {@link C}
@ -123,6 +148,20 @@ public interface Try {
return c;
}
/**
* finally
* <p>
* {@link TryConsumer}{@link TryFunction}
*
* @param finallyConsumer finally lambda
* @return {@link C}
*/
public C andFinally(ThrowableConsumer<? super Object> finallyConsumer) {
Objects.requireNonNull(finallyConsumer, "No finallyConsumer present");
this.finallyConsumer = finallyConsumer;
return c;
}
/**
* finally
*
@ -160,17 +199,6 @@ public interface Try {
}
}
@FunctionalInterface
interface ThrowableConsumer<P> {
/**
* Performs this operation on the given argument.
*
* @param p the input argument
* @throws Throwable throwable
*/
void accept(P p) throws Throwable;
}
class TrySupplier<R> extends Tryable<TrySupplier<R>> {
private final ThrowableSupplier<? extends R> supplier;
@ -215,39 +243,6 @@ public interface Try {
}
}
@FunctionalInterface
interface ThrowableSupplier<P> {
/**
* Gets a result.
*
* @return a result
* @throws Throwable throwable
*/
P get() throws Throwable;
}
@FunctionalInterface
interface ThrowableRunnable {
/**
* Performs this operation
*
* @throws Throwable throwable
*/
void run() throws Throwable;
}
@FunctionalInterface
interface ThrowableFunction<P, R> {
/**
* Applies this function to the given argument.
*
* @param p the function argument
* @return the function result
* @throws Throwable throwable
*/
R apply(P p) throws Throwable;
}
class TryConsumer<P> extends Tryable<TryConsumer<P>> {
private final ThrowableConsumer<? super P> consumer;
@ -271,7 +266,8 @@ public interface Try {
} catch (final Throwable e) {
Optional.ofNullable(throwConsumer).ifPresent(tc -> tc.accept(e));
} finally {
doFinally();
// doFinally();
doFinally(p);
}
}
}
@ -315,7 +311,8 @@ public interface Try {
Optional.ofNullable(throwConsumer).ifPresent(tc -> tc.accept(e));
return r;
} finally {
doFinally();
// doFinally();
doFinally(p);
}
}
@ -333,39 +330,98 @@ public interface Try {
Optional.ofNullable(throwConsumer).ifPresent(tc -> tc.accept(e));
return null;
} finally {
doFinally();
// doFinally();
doFinally(p);
}
}
}
@FunctionalInterface
interface ThrowableConsumer<P> {
/**
* Performs this operation on the given argument.
*
* @param p the input argument
* @throws Throwable throwable
*/
void accept(P p) throws Throwable;
}
@FunctionalInterface
interface ThrowableSupplier<R> {
/**
* Gets a result.
*
* @return a result
* @throws Throwable throwable
*/
R get() throws Throwable;
}
@FunctionalInterface
interface ThrowableRunnable {
/**
* Performs this operation
*
* @throws Throwable throwable
*/
void run() throws Throwable;
}
@FunctionalInterface
interface ThrowableFunction<P, R> {
/**
* Applies this function to the given argument.
*
* @param p the function argument
* @return the function result
* @throws Throwable throwable
*/
R apply(P p) throws Throwable;
}
@SuppressWarnings({"ConstantConditions", "Convert2MethodRef"})
static void main(String[] args) {
System.out.println("------------有返回值,无入参----------------");
// 有返回值,无入参
String param = "s";
String param = "hello";
Long result = Try.of(() -> Long.valueOf(param)).get(0L);
System.out.println("Long.valueOf 1: " + result);
result = Try.of(() -> Long.valueOf(param)).get();
System.out.println("Long.valueOf 2: " + result);
System.out.println("------------有返回值,有入参----------------");
// 有返回值,有入参
result = Try.<String, Long>of(s -> Long.valueOf(s))
.apply(param)
.trap((e) -> System.out.println("Long.valueOf exception: " + e.getMessage()))
.andFinally(() -> System.out.println("Long.valueOf finally run code."))
.finallyTrap((e) -> System.out.println("Long.valueOf finally exception: " + e.getMessage()))
result = Try.<Map<String, String>, Long>of(s -> Long.valueOf(s.get("k1")))
.apply(ImmutableMap.of("k1", param))
.trap(e -> System.out.println("Long.valueOf exception: " + e.getMessage()))
.andFinally(() -> System.out.println("This message will ignore."))
.andFinally(s -> {
Map<String, Object> returnMap = JsonUtils.string2Obj(JsonUtils.obj2String(s), new TypeReference<Map<String, Object>>() {
});
System.out.println("Long.valueOf finally run code." + s);
// 演示抛异常
String k2 = returnMap.get("k2").toString();
System.out.println(k2);
})
.finallyTrap(e -> System.out.println("Long.valueOf finally exception: " + e.getMessage()))
.get();
System.out.println("Long.valueOf 3: " + result);
ArrayList<String> list = null;
System.out.println("-----------无返回值,无入参-----------------");
// 无返回值,无入参
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
.run();
System.out.println("--------------无返回值,有入参--------------");
// 无返回值,有入参
Try.<String>of(v -> list.add(0, v)).accept("test");
Try.<String>of(v -> list.add(0, v))
.andFinally(s -> System.out.println(s))
.accept("hello");
}
}