add doc comment
This commit is contained in:
parent
c7c1cc0648
commit
d2ffda2813
@ -36,18 +36,46 @@ import java.util.function.Consumer;
|
|||||||
* @since 2020-12-03 10:37.
|
* @since 2020-12-03 10:37.
|
||||||
*/
|
*/
|
||||||
public interface Try {
|
public interface Try {
|
||||||
|
/**
|
||||||
|
* 构建消费型(有入参,无返回)Tryable 对象
|
||||||
|
*
|
||||||
|
* @param consumer {@link ThrowableConsumer} 类型函数式接口
|
||||||
|
* @param <T> 入参类型
|
||||||
|
* @return {@link TryConsumer}
|
||||||
|
*/
|
||||||
static <T> TryConsumer<T> of(ThrowableConsumer<? super T> consumer) {
|
static <T> TryConsumer<T> of(ThrowableConsumer<? super T> consumer) {
|
||||||
return new TryConsumer<>(consumer);
|
return new TryConsumer<>(consumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建供给型(无入参,有返回)Tryable 对象
|
||||||
|
*
|
||||||
|
* @param supplier {@link ThrowableSupplier} 类型函数式接口
|
||||||
|
* @param <R> 返回值类型
|
||||||
|
* @return {@link TrySupplier}
|
||||||
|
*/
|
||||||
static <R> TrySupplier<R> of(ThrowableSupplier<? extends R> supplier) {
|
static <R> TrySupplier<R> of(ThrowableSupplier<? extends R> supplier) {
|
||||||
return new TrySupplier<>(supplier);
|
return new TrySupplier<>(supplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建功能型(有入参,有返回)Tryable 对象
|
||||||
|
*
|
||||||
|
* @param function {@link ThrowableFunction} 类型函数式接口
|
||||||
|
* @param <T> 入参类型
|
||||||
|
* @param <R> 返回值类型
|
||||||
|
* @return {@link TryFunction}
|
||||||
|
*/
|
||||||
static <T, R> TryFunction<T, R> of(ThrowableFunction<? super T, ? extends R> function) {
|
static <T, R> TryFunction<T, R> of(ThrowableFunction<? super T, ? extends R> function) {
|
||||||
return new TryFunction<>(function);
|
return new TryFunction<>(function);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建运行型(无入参,无返回)Tryable 对象
|
||||||
|
*
|
||||||
|
* @param runnable {@link ThrowableRunnable} 类型函数式接口
|
||||||
|
* @return {@link TryRunnable}
|
||||||
|
*/
|
||||||
static TryRunnable of(ThrowableRunnable runnable) {
|
static TryRunnable of(ThrowableRunnable runnable) {
|
||||||
return new TryRunnable(runnable);
|
return new TryRunnable(runnable);
|
||||||
}
|
}
|
||||||
@ -61,7 +89,7 @@ public interface Try {
|
|||||||
/**
|
/**
|
||||||
* 处理 finally
|
* 处理 finally
|
||||||
*/
|
*/
|
||||||
protected void _finally() {
|
protected void doFinally() {
|
||||||
Optional.ofNullable(finallyRunnable).ifPresent(r -> {
|
Optional.ofNullable(finallyRunnable).ifPresent(r -> {
|
||||||
try {
|
try {
|
||||||
r.run();
|
r.run();
|
||||||
@ -127,7 +155,7 @@ public interface Try {
|
|||||||
} catch (final Throwable e) {
|
} catch (final Throwable e) {
|
||||||
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
||||||
} finally {
|
} finally {
|
||||||
_finally();
|
doFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -155,7 +183,7 @@ public interface Try {
|
|||||||
} catch (final Throwable e) {
|
} catch (final Throwable e) {
|
||||||
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
||||||
} finally {
|
} finally {
|
||||||
_finally();
|
doFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -183,7 +211,7 @@ public interface Try {
|
|||||||
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
||||||
return r;
|
return r;
|
||||||
} finally {
|
} finally {
|
||||||
_finally();
|
doFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,7 +227,7 @@ public interface Try {
|
|||||||
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
_finally();
|
doFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -222,7 +250,7 @@ public interface Try {
|
|||||||
* @return {@link TryFunction}
|
* @return {@link TryFunction}
|
||||||
*/
|
*/
|
||||||
public TryFunction<T, R> apply(T t) {
|
public TryFunction<T, R> apply(T t) {
|
||||||
Objects.requireNonNull(t, "Apply t shoud not null");
|
Objects.requireNonNull(t, "Apply t should not null");
|
||||||
|
|
||||||
this.t = t;
|
this.t = t;
|
||||||
return this;
|
return this;
|
||||||
@ -243,7 +271,7 @@ public interface Try {
|
|||||||
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
||||||
return r;
|
return r;
|
||||||
} finally {
|
} finally {
|
||||||
_finally();
|
doFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,28 +289,52 @@ public interface Try {
|
|||||||
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e));
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
_finally();
|
doFinally();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
interface ThrowableRunnable {
|
interface ThrowableRunnable {
|
||||||
|
/**
|
||||||
|
* Performs this operation
|
||||||
|
*
|
||||||
|
* @throws Throwable throwable
|
||||||
|
*/
|
||||||
void run() throws Throwable;
|
void run() throws Throwable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
interface ThrowableConsumer<T> {
|
interface ThrowableConsumer<T> {
|
||||||
|
/**
|
||||||
|
* Performs this operation on the given argument.
|
||||||
|
*
|
||||||
|
* @param t the input argument
|
||||||
|
* @throws Throwable throwable
|
||||||
|
*/
|
||||||
void accept(T t) throws Throwable;
|
void accept(T t) throws Throwable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
interface ThrowableSupplier<T> {
|
interface ThrowableSupplier<T> {
|
||||||
|
/**
|
||||||
|
* Gets a result.
|
||||||
|
*
|
||||||
|
* @return a result
|
||||||
|
* @throws Throwable throwable
|
||||||
|
*/
|
||||||
T get() throws Throwable;
|
T get() throws Throwable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
interface ThrowableFunction<T, R> {
|
interface ThrowableFunction<T, R> {
|
||||||
|
/**
|
||||||
|
* Applies this function to the given argument.
|
||||||
|
*
|
||||||
|
* @param t the function argument
|
||||||
|
* @return the function result
|
||||||
|
* @throws Throwable throwable
|
||||||
|
*/
|
||||||
R apply(T t) throws Throwable;
|
R apply(T t) throws Throwable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user