com.github.yiuman.citrus.support.utils.LambdaUtils Maven / Gradle / Ivy
package com.github.yiuman.citrus.support.utils;
import com.github.yiuman.citrus.support.wrapper.*;
import java.util.function.*;
/**
* Lambda工具
*
* @author yiuman
* @date 2020/4/3
*/
public final class LambdaUtils {
private LambdaUtils() {
}
/**
* 用于处理lambda检查异常
*
* @param consumer 可抛异常consumer
* @param 类型
* @return Consumer
*/
public static Consumer consumerWrapper(ConsumerWrapper consumer) {
return t -> {
try {
consumer.accept(t);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
/**
* 用于处理lambda检查异常
*
* @param consumer 可抛异常consumer
* @param 类型
* @return Consumer
*/
public static BiConsumer biConsumerWrapper(BiConsumerWrapper consumer) {
return (t, u) -> {
try {
consumer.accept(t, u);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
public static Function functionWrapper(FunctionWrapper
functionWrapper) {
return t -> {
try {
return functionWrapper.apply(t);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
public static Predicate predicateWrapper(PredicateWrapper predicateWrapper) {
return t -> {
try {
return predicateWrapper.test(t);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
public static Supplier supplierWrapper(SupplierWrapper
supplierWrapper) {
return () -> {
try {
return supplierWrapper.get();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy