美文网首页
netty中实用util-判断抛出异常

netty中实用util-判断抛出异常

作者: 漫步无法人生 | 来源:发表于2020-01-12 11:26 被阅读0次

netty中的简化如下 希望带来一点点的思考

public final class ObjectUtil {

    private ObjectUtil() {
    }

    /**
     * Checks that the given argument is not null. If it is, throws {@link NullPointerException}.
     * Otherwise, returns the argument.
     */
    public static <T> T checkNotNull(T arg, String text) {
        if (arg == null) {
            throw new NullPointerException(text);
        }
        return arg;
    }

    /**
     * Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
     * Otherwise, returns the argument.
     */
    public static int checkPositive(int i, String name) {
        if (i <= 0) {
            throw new IllegalArgumentException(name + ": " + i + " (expected: > 0)");
        }
        return i;
    }
}

以下是自己的实现 利用1.8特性构建的util

public class Try {

    private static Logger logger = LoggerFactory.getLogger(Try.class);

    /**
     * 如果有异常抛出,绕过lambda
     * @param mapper
     * @param <R>
     * @return
     */
    public static <R> R ofThrow(UncheckedSupplier<R> mapper) {
        Objects.requireNonNull(mapper);
        try {
            return mapper.get();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static <R, E extends Throwable> R ofThrow(UncheckedSupplier<R> mapper, E e, String var1, Object var2) throws E {
        Objects.requireNonNull(mapper);
        try {
            return mapper.get();
        } catch (Throwable ex) {
            logger.error(var1, var2, ex);
            throw e;
        }
    }

    public static <R, E extends Throwable> R ofThrow(UncheckedSupplier<R> mapper, E e, String var1) throws E {
        Objects.requireNonNull(mapper);
        try {
            return mapper.get();
        } catch (Throwable ex) {
            logger.error(var1, ex);
            throw e;
        }
    }

    public static <R, E extends Throwable> R ofThrow(UncheckedSupplier<R> mapper, E e) throws E {
        Objects.requireNonNull(mapper);
        try {
            return mapper.get();
        } catch (Throwable ex) {
            throw e;
        }
    }

    /**
     * 如果有异常抛出,绕过lambda
     * @param mapper
     * @param <R>
     * @return
     */
    public static <T, R> R ofThrow(UncheckedFunction<T, R> mapper, T t) {
        Objects.requireNonNull(mapper);
        try {
            return mapper.apply(t);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 有异常直接包住
     * @param mapper
     * @param o
     * @param <R>
     * @param <O>
     * @return
     */
    public static <R, O> R of(UncheckedSupplier<R> mapper, O o) {
        Objects.requireNonNull(mapper);
        try {
            return mapper.get();
        } catch (Exception ex) {
            logger.error("exception msg = {}, error = {}", o, ex);
        }
        return null;
    }

    /**
     * 有异常直接包住
     *
     * @param mapper
     * @param <R>
     * @param <O>
     * @return
     */
    public static <R, O> R of(UncheckedSupplier<R> mapper, O o, R r) {
        Objects.requireNonNull(mapper);
        try {
            return mapper.get();
        } catch (Exception ex) {
            logger.error("exception msg = {}, error = {}", o, ex);
        }
        return r;
    }

    /**
     * 有异常直接包住,无返回值
     * @param mapper
     * @param o
     * @param <O>
     * @return
     */
    public static <O> void of(Unchecked mapper, O o) {
        Objects.requireNonNull(mapper);
        try {
           mapper.test();
        } catch (Exception ex) {
            logger.error("Unchecked exception msg = {}, error = {}", o, ex);
        }
    }

    @FunctionalInterface
    public static interface Unchecked {

        void test() throws Exception;

    }

    @FunctionalInterface
    public static interface UncheckedFunction<T, R> {

        R apply(T t) throws Exception;

    }

    @FunctionalInterface
    public static interface UncheckedSupplier<R> {

        R get() throws Exception;

    }

    /**
     * 如果参数为null,就抛出指定的异常
     * @param obj
     * @param e
     * @param <T>
     */
    public static <T, E extends Exception> void isNullThrow(T obj, E e) throws Exception {
        if (obj == null)
            throw e;
    }


    /**
     * 如果参数为true,就抛出指定的异常
     * @param obj
     * @param e
     * @param <T>
     */
    public static <E extends Exception> void isTrueThrow(Boolean bool, E e) throws Exception {
        if (Boolean.TRUE.equals(bool)) {
            throw e;
        }
    }

    /**
     * 如果 opt没有值就抛出
     *
     * @param opt
     * @param e
     * @param <T>
     * @param <E>
     * @return
     * @throws Exception
     */
    public static <T, E extends Exception> T isNullThrow(Optional<T> opt, E e) throws Exception {
        if (opt.isPresent()) {
            return opt.get();
        }
        throw e;
    }

}

相关文章

网友评论

      本文标题:netty中实用util-判断抛出异常

      本文链接:https://www.haomeiwen.com/subject/nqmiactx.html