com.developcollect.dcinfra.utils.LambdaUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dc-infra Show documentation
Show all versions of dc-infra Show documentation
这是一个dc系列工具的基础包, 包含一些基础的类及依赖
package com.developcollect.dcinfra.utils;
/**
* lambda工具类
*
* @author zak
* @since 1.0.0
*/
public class LambdaUtil {
/**
* 什么都不做
* @param objs
* @return R
* @author zak
* @date 2020/8/24 10:55
*/
public static R doNothing(Object... objs) {
return null;
}
/**
* 同{@link #doNothing(Object...)}
*/
public static R nop(Object... objs) {
// nop
return null;
}
/**
* 编写一个泛型方法对异常进行包装
* 从而绕过受检异常在编译时编译不通过的错误
*
* @param e 异常
* @param 返回值类型
* @param 异常类型
* @throws E 异常
* @author zak
* @since 1.0.0
*/
public static R doThrow(Throwable e) throws E {
throw (E) e;
}
/**
* 同{@link #doThrow(Throwable)}
*/
public static R raise(Throwable e) throws E {
throw (E) e;
}
/**
* 通过函数式接口包装原有含受检异常的代码, 将受检异常通过泛型绕过编译错误
*
* @param doThrowWrapper 异常包装
* @param 返回值类型
* @return 返回值
* @author zak
* @since 1.0.0
*/
public static T doThrow(DoThrowWrapper doThrowWrapper) {
try {
return doThrowWrapper.get();
} catch (Throwable throwable) {
return doThrow(throwable);
}
}
/**
* 同{@link #doThrow(DoThrowWrapper)}
*/
public static T raise(DoThrowWrapper doThrowWrapper) {
return doThrow(doThrowWrapper);
}
/**
* 异常代码包装
*
* @author zak
* @since 1.0.0
*/
@FunctionalInterface
public interface DoThrowWrapper {
T get() throws Throwable;
}
}