Java函数式抛异常

package com.ele.util;

import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * ele
 */
public interface IThrowableUtil {

    IThrowableUtil build = new IThrowableUtil() {};

    default <RE extends RuntimeException> void throwof(Consumer<RE> before, RE re, boolean rule) {
        if (rule) {
            before.accept(re);
            throw re;
        }
    }

    default <RE extends RuntimeException> void throwof(RE re, boolean rule) {
        throwof(e -> {}, re, rule);
    }

    default <T> void throwof(String errMessage, T object, Predicate<T> rule) {
        throwof(new RuntimeException(errMessage), object, rule);
    }

    default <T> void throwof(Consumer<RuntimeException> before, String errMessage, T object, Predicate<T> rule) {
        throwof(before, new RuntimeException(errMessage), object, rule);
    }

    default <RE extends RuntimeException, T> void throwof(RE re, T object, Predicate<T> rule) {
        throwof(re, rule.test(object));
    }

    default <RE extends RuntimeException, T> void throwof(Consumer<RE> before, RE re, T object, Predicate<T> rule) {
        throwof(before, re, rule.test(object));
    }

    default <T> void throwofNon(String errMessage, T object, Predicate<T> rule) {
        throwofNon(new RuntimeException(errMessage), object, rule);
    }

    default <T> void throwofNon(Consumer<RuntimeException> before, String errMessage, T object, Predicate<T> rule) {
        throwofNon(before, new RuntimeException(errMessage), object, rule);
    }

    default <RE extends RuntimeException, T> void throwofNon(RE re, T object, Predicate<T> rule) {
        throwof(re, !rule.test(object));
    }

    default <RE extends RuntimeException, T> void throwofNon(Consumer<RE> before, RE re, T object, Predicate<T> rule) {
        throwof(before, re, !rule.test(object));
    }

    default void throwof(String errMessage, boolean rule) {
        throwof(new RuntimeException(errMessage), rule);
    }

    default void throwof(Consumer<RuntimeException> before, String errMessage, boolean rule) {
        throwof(before, new RuntimeException(errMessage), rule);
    }

    default void throwof(String errMessage, Supplier<Boolean> rule) {
        throwof(new RuntimeException(errMessage), rule.get());
    }

    default void throwof(Consumer<RuntimeException> before, String errMessage, Supplier<Boolean> rule) {
        throwof(before, new RuntimeException(errMessage), rule.get());
    }

    default <RE extends RuntimeException> void throwof(RE re, Supplier<Boolean> rule) {
        throwof(re, rule.get());
    }

    default <RE extends RuntimeException> void throwof(Consumer<RE> before, RE re, Supplier<Boolean> rule) {
        throwof(before, re, rule.get());
    }

}
package com.ele.util;

import java.util.Objects;
import java.util.function.Consumer;

/**
 * ele
 */
public interface IThrowObjectUtil extends IThrowableUtil {

    IThrowObjectUtil build = new IThrowObjectUtil() {};

    default <RE extends RuntimeException, T> void throwofIsNull(RE re, T object) {
        throwof(re, Objects.isNull(object));
    }

    default <RE extends RuntimeException, T> void throwofIsNull(Consumer<RE> before, RE re, T object) {
        throwof(before, re, Objects.isNull(object));
    }

    default <T> void throwofIsNull(String errMessage, T object) {
        throwofIsNull(new RuntimeException(errMessage), object);
    }

    default <T> void throwofIsNull(Consumer<RuntimeException> before, String errMessage, T object) {
        throwofIsNull(before, new RuntimeException(errMessage), object);
    }

    default <RE extends RuntimeException, T> void throwofNonNull(RE re, T object) {
        throwof(re, Objects.nonNull(object));
    }

    default <RE extends RuntimeException, T> void throwofNonNull(Consumer<RE> before, RE re, T object) {
        throwof(before, re, Objects.nonNull(object));
    }

    default <T> void throwofNonNull(String errMessage, T object) {
        throwofNonNull(new RuntimeException(errMessage), object);
    }

    default <T> void throwofNonNull(Consumer<RuntimeException> before, String errMessage, T object) {
        throwofNonNull(before, new RuntimeException(errMessage), object);
    }

    default <T> void throwofEquals(String errMessage, T object, T competitor) {
        throwofEquals(new RuntimeException(errMessage), object, competitor);
    }

    default <T> void throwofEquals(Consumer<RuntimeException> before, String errMessage, T object, T competitor) {
        throwofEquals(before, new RuntimeException(errMessage), object, competitor);
    }

    default <T> void throwofNotEquals(String errMessage, T object, T competitor) {
        throwofNotEquals(new RuntimeException(errMessage), object, competitor);
    }

    default <T> void throwofNotEquals(Consumer<RuntimeException> before, String errMessage, T object, T competitor) {
        throwofNotEquals(before, new RuntimeException(errMessage), object, competitor);
    }

    default <RE extends RuntimeException, T> void throwofEquals(RE re, T object, T competitor) {
        throwof(re, Objects.equals(object, competitor));
    }

    default <RE extends RuntimeException, T> void throwofEquals(Consumer<RE> before, RE re, T object, T competitor) {
        throwof(before, re, Objects.equals(object, competitor));
    }

    default <RE extends RuntimeException, T> void throwofNotEquals(RE re, T object, T competitor) {
        throwof(re, !Objects.equals(object, competitor));
    }

    default <RE extends RuntimeException, T> void throwofNotEquals(Consumer<RE> before, RE re, T object, T competitor) {
        throwof(before, re, !Objects.equals(object, competitor));
    }
}
package com.ele.util;

import java.util.function.Consumer;

/**
 * ele
 */
public interface IThrowStringUtil extends IThrowObjectUtil {

    IThrowStringUtil build = new IThrowStringUtil() {};

    default <RE extends RuntimeException> void throwofStrIsEmpty(RE re, String str) {
        throwof(re, (str == null || str.length() == 0));
    }

    default <RE extends RuntimeException> void throwofStrIsEmpty(Consumer<RE> before, RE re, String str) {
        throwof(before, re, (str == null || str.length() == 0));
    }

    default void throwofStrIsEmpty(String errMessage, String str) {
        throwofStrIsEmpty(new RuntimeException(errMessage), str);
    }

    default void throwofStrIsEmpty(Consumer<RuntimeException> before, String errMessage, String str) {
        throwofStrIsEmpty(before, new RuntimeException(errMessage), str);
    }

    default <RE extends RuntimeException> void throwofStrNotEmpty(RE re, String str) {
        throwof(re, !(str == null || str.length() == 0));
    }

    default <RE extends RuntimeException> void throwofStrNotEmpty(Consumer<RE> before, RE re, String str) {
        throwof(before, re, !(str == null || str.length() == 0));
    }

    default void throwofStrNotEmpty(String errMessage, String str) {
        throwofStrNotEmpty(new RuntimeException(errMessage), str);
    }

    default void throwofStrNotEmpty(Consumer<RuntimeException> before, String errMessage, String str) {
        throwofStrNotEmpty(before, new RuntimeException(errMessage), str);
    }
}
package com.ele.util;

/**
 * ele
 */
public interface IThrowableUtils extends IThrowStringUtil {

    IThrowableUtils build = new IThrowableUtils() {};

}

使用案例:

class Test implements IThrowableUtils {

    Logger logger = LoggerFactory.getLogger(getClass());
    
    void test() {

        throwof(log("exception message"), "message", ture);
        throwof(log("exception message"), "message", 1, Objects::nonNull);
        throwof(log(), "message", ture);
        throwof(log(), "message", 1, Objects::nonNull);
        throwofNonNull(log(), "message", 1);
        throwofStrIsEmty(log(), "message", "string");
        // ...
        Consumer<RuntimeException> log = re -> logger.error(re.getMessage(), re);
        throwofNonNull(log, "message", 1);
        throwofNonNull(log, new RuntimeException("message"), 1);
        throwofNotEquals(log, new RuntimeException("message"), "a", "b");
        // ...
        IThrowableUtils.build.throwof(log, "message", ture);
        // ...
        IThrowableUtils throwUtil = IThrowableUtils.build;
        throwUtil.throwof(log, "message", ture);
        // ...
        
    }

    <RE extends RuntimeException> Comsumer<RE> log(String message) {
        return re -> logger.error(message + "" + re.getMessage(), re);
    }

    <RE extends RuntimeException> Comsumer<RE> log() {
        return re -> logger.error(re.getMessage(), re);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值