public class LambdaUtil extends Object
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
LambdaUtil.BiConsumerWithExceptions<T,U,E extends Exception> |
static interface |
LambdaUtil.BiFunctionWithExceptions<T,U,R,E extends Exception> |
static interface |
LambdaUtil.BiPredicateWithExceptions<T,U,E extends Exception> |
static interface |
LambdaUtil.ConsumerWithExceptions<T,E extends Exception> |
static interface |
LambdaUtil.FunctionWithExceptions<T,R,E extends Exception> |
static interface |
LambdaUtil.PredicateWithExceptions<T,E extends Exception> |
static interface |
LambdaUtil.RunnableWithExceptions<E extends Exception> |
static interface |
LambdaUtil.SupplierWithExceptions<T,E extends Exception> |
private static class |
LambdaUtil.UnreachableException |
| 构造器和说明 |
|---|
LambdaUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static <E extends Exception> |
sure(LambdaUtil.RunnableWithExceptions<E> runner)
同上
|
static <R,E extends Exception> |
sure(LambdaUtil.SupplierWithExceptions<R,E> supplier)
如果一段代码确保不会抛出所申明的异常,可以使用该方法进行包装
如:new String(byteArr, "UTF-8")申明了UnsupportedEncodingException,
但编码"UTF-8"是必定不会抛异常的,使用sure进行包装
String text = sure(() -> new String(byteArr, "UTF-8"))
注: sure方法有一定的风险,因为它隐藏了可能的异常申明,所以请谨慎使用,确保(sure)不会抛出异常才可以使用
|
private static <E extends Exception> |
throwAsUnchecked(Exception exception) |
static <T,U,E extends Exception> |
wrapBiConsumer(LambdaUtil.BiConsumerWithExceptions<T,U,E> biConsumer)
包装双重消费函数(BiConsumer)
|
static <T,U,R,E extends Exception> |
wrapBiFunction(LambdaUtil.BiFunctionWithExceptions<T,U,R,E> biFunction)
包装双入参普通函数(BiFunction)
|
static <T,U,E extends Exception> |
wrapBiPredicate(LambdaUtil.BiPredicateWithExceptions<T,U,E> predicate)
包装双入参条件函数(BiPredicate)
|
static <T,E extends Exception> |
wrapConsumer(LambdaUtil.ConsumerWithExceptions<T,E> consumer)
包装消费函数(Consumer)
|
static <T,R,E extends Exception> |
wrapFunction(LambdaUtil.FunctionWithExceptions<T,R,E> function)
包装普通函数(Function)
注:方法签名中的" throws E "编译器会提示多余,但其实是为了将实际的异常向外传递,如果不这么做:
1.外层代码中编译器将无法提示有异常需要处理
2.也无法主动在外层捕获具体的异常,如果尝试try一个具体的异常,编译器将提示:
在try语句体中永远不会抛出相应异常(Exception 'XXX' is never thrown in the corresponding try block)
|
static <T,E extends Exception> |
wrapPredicate(LambdaUtil.PredicateWithExceptions<T,E> predicate)
包装条件函数(Predicate)
|
static <E extends Exception> |
wrapRunnable(LambdaUtil.RunnableWithExceptions<E> runnable)
包装纯执行函数(Runnable)
|
static <T,E extends Exception> |
wrapSupplier(LambdaUtil.SupplierWithExceptions<T,E> function)
包装生产函数(Supplier)
|
public static <T,R,E extends Exception> java.util.function.Function<T,R> wrapFunction(LambdaUtil.FunctionWithExceptions<T,R,E> function) throws E extends Exception
注:方法签名中的" throws E "编译器会提示多余,但其实是为了将实际的异常向外传递,如果不这么做: 1.外层代码中编译器将无法提示有异常需要处理 2.也无法主动在外层捕获具体的异常,如果尝试try一个具体的异常,编译器将提示: 在try语句体中永远不会抛出相应异常(Exception 'XXX' is never thrown in the corresponding try block)
E extends Exceptionpublic static <R,E extends Exception> R sure(LambdaUtil.SupplierWithExceptions<R,E> supplier)
注: sure方法有一定的风险,因为它隐藏了可能的异常申明,所以请谨慎使用,确保(sure)不会抛出异常才可以使用
public static <E extends Exception> void sure(LambdaUtil.RunnableWithExceptions<E> runner)
E - runner - private static <E extends Exception> void throwAsUnchecked(Exception exception) throws E extends Exception
E extends Exceptionpublic static <T,U,R,E extends Exception> java.util.function.BiFunction<T,U,R> wrapBiFunction(LambdaUtil.BiFunctionWithExceptions<T,U,R,E> biFunction) throws E extends Exception
E extends Exceptionpublic static <T,E extends Exception> java.util.function.Consumer<T> wrapConsumer(LambdaUtil.ConsumerWithExceptions<T,E> consumer) throws E extends Exception
E extends Exceptionpublic static <T,U,E extends Exception> java.util.function.BiConsumer<T,U> wrapBiConsumer(LambdaUtil.BiConsumerWithExceptions<T,U,E> biConsumer) throws E extends Exception
E extends Exceptionpublic static <T,E extends Exception> java.util.function.Supplier<T> wrapSupplier(LambdaUtil.SupplierWithExceptions<T,E> function) throws E extends Exception
E extends Exceptionpublic static <T,E extends Exception> java.util.function.Predicate<T> wrapPredicate(LambdaUtil.PredicateWithExceptions<T,E> predicate) throws E extends Exception
E extends Exceptionpublic static <T,U,E extends Exception> java.util.function.BiPredicate<T,U> wrapBiPredicate(LambdaUtil.BiPredicateWithExceptions<T,U,E> predicate) throws E extends Exception
E extends Exceptionpublic static <E extends Exception> void wrapRunnable(LambdaUtil.RunnableWithExceptions<E> runnable) throws E extends Exception
E extends ExceptionCopyright © 2022. All rights reserved.