lambda表达式可以让我们的代码更加简介,业务逻辑更加清晰,但是在lambda表达式中使用的Functional Interface并没有很好的处理异常,因为JDK提供的这些Functional Interface通常都是没有抛出异常的,这意味着需要我们自己手动来处理异常。
Lambda表达式能够自动抛出非检查异常,检查异常,必须catch,如果每个表达式都去处理,代码会非常臃肿,可以看一下如下代码:
private List<String> list = Arrays.asList("1", "2", "0", "4", "8");
@Test
public void test2() {
list.stream().forEach(i -> {
File file = new File(i);
try {
FileInputStream fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
});
}
改造一下上面的代码,编写一个统一抛出异常的处理函数,使得代码简洁
定义一个能够抛出检查异常的consumer:
package com.iscas.common.tools.exception;
@FunctionalInterface
public interface ThrowingConsumer<T> {
void accept(T t) throws Exception;
}
编写一个通用的处理类:
/**
* lambda consumer统一异常处理
* @version 1.0
* @since jdk1.8
* @date 2021/2/17
* @param consumer
* @throws
* @return java.util.function.Consumer<T>
*/
public static <T> Consumer<T> lambdaWrapper(ThrowingConsumer<T> consumer) {
return i -> {
try {
consumer.accept(i);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
测试:
private List<String> list = Arrays.asList("1", "2", "0", "4", "8");
@Test
public void test3() {
list.stream().forEach(LambdaExceptionUtils.lambdaWrapper(i -> {
File file = new File(i);
FileInputStream fileInputStream = new FileInputStream(file);
}));
}
可以发现代码又简洁了
其他lambda表达式相关的工具
/**
* lambda consumer统一异常处理
* @version 1.0
* @since jdk1.8
* @date 2021/2/17
* @param consumer
* @throws
* @return java.util.function.Consumer<T>
*/
public static <T> Consumer<T> lambdaWrapper(ThrowingConsumer<T> consumer) {
return i -> {
try {
consumer.accept(i);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
/**
* lambda BiConsumer统一异常处理
* @version 1.0
* @since jdk1.8
* @date 2021/2/17
* @param consumer
* @throws
* @return java.util.function.Consumer<T>
*/
public static <T, U> BiConsumer<T, U> lambdaWrapper(ThrowingBiConsumer<T, U> consumer) {
return (i, j) -> {
try {
consumer.accept(i, j);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
/**
* lambda function统一异常处理
* @version 1.0
* @since jdk1.8
* @date 2021/2/17
* @param function
* @throws
* @return java.util.function.Consumer<T>
*/
public static <T, R> Function<T, R> lambdaWrapper(ThrowingFunction<T, R> function) {
return i -> {
try {
return function.apply(i);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
/**
* lambda supplier统一异常处理
* @version 1.0
* @since jdk1.8
* @date 2021/2/17
* @param supplier
* @throws
* @return java.util.function.Consumer<T>
*/
public static <T> Supplier<T> lambdaWrapper(ThrowingSupplier<T> supplier) {
return () -> {
try {
return (T) supplier.get();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
/**
* lambda predicate统一异常处理
* @version 1.0
* @since jdk1.8
* @date 2021/2/17
* @param predicate
* @throws
* @return java.util.function.Consumer<T>
*/
public static <T> Predicate<T> lambdaPredicateWrapper(ThrowingPredicate<T> predicate) {
return i -> {
try {
return predicate.test(i);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
package com.iscas.common.tools.exception.lambda;
/**
* Lambda表达式能够抛出异常的BiConsumer
*
* @author zhuquanwen
* @vesion 1.0
* @date 2021/2/17 14:07
* @since jdk1.8
*/
@FunctionalInterface
public interface ThrowingBiConsumer<T, U> {
void accept(T t, U u) throws Exception;
}
package com.iscas.common.tools.exception.lambda;
/**
* Lambda表达式能够抛出异常的function
*
* @author zhuquanwen
* @vesion 1.0
* @date 2021/2/17 14:07
* @since jdk1.8
*/
@FunctionalInterface
public interface ThrowingFunction<T, R> {
R apply(T t) throws Exception;
}
package com.iscas.common.tools.exception.lambda;
import java.util.Objects;
import java.util.function.Predicate;
/**
* Lambda表达式能够抛出异常的Predicate
*
* @author zhuquanwen
* @vesion 1.0
* @date 2021/2/17 14:07
* @since jdk1.8
*/
@FunctionalInterface
public interface ThrowingPredicate<T>{
boolean test(T t) throws Exception;
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> {
try {
return test(t) && other.test(t);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
default Predicate<T> negate() {
return (t) -> {
try {
return !test(t);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> {
try {
return test(t) || other.test(t);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
@SuppressWarnings("unchecked")
static <T> Predicate<T> not(Predicate<? super T> target) {
Objects.requireNonNull(target);
return (Predicate<T>)target.negate();
}
}
package com.iscas.common.tools.exception.lambda;
/**
* Lambda表达式能够抛出异常的supplier
*
* @author zhuquanwen
* @vesion 1.0
* @date 2021/2/17 14:07
* @since jdk1.8
*/
@FunctionalInterface
public interface ThrowingSupplier<T> {
T get() throws Exception;
}