All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sinjar.common.ext.hub.SimpleCode Maven / Gradle / Ivy

There is a newer version: 1.3
Show newest version
package com.github.sinjar.common.ext.hub;

import com.github.sinjar.common.base.RunnableWithThrow;
import com.github.sinjar.common.base.SupplierWithThrow;
import com.github.sinjar.common.lang.ActionExecException;
import lombok.extern.slf4j.Slf4j;

/**
 * Description :  简化代码类
 * 

* runtimeException开头, 原来需要catch的运行时转换为runtimeException抛出 * ignoreException开头, 忽略发生的异常, 仅仅打印日志. * * @author CPF * Date: 2020/6/24 18:00 */ @Slf4j public class SimpleCode { private SimpleCode() { } /** * 忽略运行的异常 * * @param runnable 带有异常的运行接口 * @param message 发生异常报错信息 * @see SimpleCode#simpleException(com.github.sinjar.common.base.RunnableWithThrow, java.lang.String, boolean) */ public static void ignoreException(RunnableWithThrow runnable, String message) { simpleException(runnable, message, false); } /** * 忽略运行的异常 * * @param runnable 带有异常的运行接口 * @see SimpleCode#simpleException(com.github.sinjar.common.base.RunnableWithThrow, java.lang.String, boolean) */ public static void ignoreException(RunnableWithThrow runnable) { simpleException(runnable, null, false); } /** * 异常转换为 ActionExecException * * @param runnable 带有异常的运行接口 * @param message 发生异常报错信息 * @see SimpleCode#simpleException(com.github.sinjar.common.base.RunnableWithThrow, java.lang.String, boolean) */ public static void runtimeException(RunnableWithThrow runnable, String message) { simpleException(runnable, message, true); } /** * 异常转换为 ActionExecException * * @param runnable 带有异常的运行接口 * @see SimpleCode#simpleException(com.github.sinjar.common.base.RunnableWithThrow, java.lang.String, boolean) */ public static void runtimeException(RunnableWithThrow runnable) { simpleException(runnable, null, true); } /** * 忽略运行的异常 * * @param supplier 带有返回值和throw的函数接口 * @see SimpleCode#simpleException(com.github.sinjar.common.base.SupplierWithThrow, java.lang.Object, java.lang.String, boolean) */ public static T ignoreException(SupplierWithThrow supplier) { return simpleException(supplier, null, null, false); } /** * 忽略运行的异常 * * @param supplier 带有返回值和throw的函数接口 * @param defaultValue supplier 发生错误后的默认返回值 * @see SimpleCode#simpleException(com.github.sinjar.common.base.SupplierWithThrow, java.lang.Object, java.lang.String, boolean) */ public static T ignoreException(SupplierWithThrow supplier, T defaultValue) { return simpleException(supplier, defaultValue, null, false); } /** * 忽略运行的异常 * * @param supplier 带有返回值和throw的函数接口 * @param defaultValue supplier 发生错误后的默认返回值 * @see SimpleCode#simpleException(com.github.sinjar.common.base.SupplierWithThrow, java.lang.Object, java.lang.String, boolean) */ public static T ignoreException(SupplierWithThrow supplier, T defaultValue, String message) { return simpleException(supplier, defaultValue, message, false); } /** * 忽略运行的异常 * * @param supplier 带有返回值和throw的函数接口 * @see SimpleCode#simpleException(com.github.sinjar.common.base.SupplierWithThrow, java.lang.Object, java.lang.String, boolean) */ public static T runtimeException(SupplierWithThrow supplier) { return simpleException(supplier, null, null, true); } /** * 原来需要 catch 的运行时转换为 runtimeException 抛出 * * @param supplier 带有返回值和throw的函数接口 * @param message supplier 发生错误后的信息 * @see SimpleCode#simpleException(com.github.sinjar.common.base.SupplierWithThrow, java.lang.Object, java.lang.String, boolean) */ public static T runtimeException(SupplierWithThrow supplier, String message) { return simpleException(supplier, null, message, true); } /** * 忽略运行的异常 * * @param runnable 指定的函数接口 * @param message runnable 发生错误后的信息 * @param throwException true: runnable 发生错误后抛出运行时异常, false: 仅仅打印日志 */ public static void simpleException(RunnableWithThrow runnable, String message, boolean throwException) { try { runnable.run(); } catch (Exception e) { if (message == null) { message = "simpleException"; } if (throwException) { throw new ActionExecException(message, e); } else { log.error(message, e); } } } /** * 忽略运行的异常 * * @param supplier 带有返回值和throw的函数接口 * @param defaultValue throwException为true时, supplier 发生错误后的默认返回值 * @param message supplier 发生错误后的信息 * @param throwException true: supplier 发生错误后抛出运行时异常, false: 仅仅打印日志 * @param 返回值类型 * @return supplier 执行成功: supplier的返回值, supplier 执行失败: 返回 defaultValue */ public static T simpleException(SupplierWithThrow supplier, T defaultValue, String message, boolean throwException) { try { return supplier.get(); } catch (Exception e) { if (message == null) { message = "simpleException"; } if (throwException) { throw new ActionExecException(message, e); } else { log.error(message, e); } } return defaultValue; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy