package com.ele.util; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.Supplier; public interface IThrowableUtil { IThrowableUtil build = new IThrowableUtil() {}; defaultvoid throwof(Consumer before, RE re, boolean rule) { if (rule) { before.accept(re); throw re; } } default void throwof(RE re, boolean rule) { throwof(e -> {}, re, rule); } default void throwof(String errMessage, T object, Predicate rule) { throwof(new RuntimeException(errMessage), object, rule); } default void throwof(Consumer before, String errMessage, T object, Predicate rule) { throwof(before, new RuntimeException(errMessage), object, rule); } default void throwof(RE re, T object, Predicate rule) { throwof(re, rule.test(object)); } default void throwof(Consumer before, RE re, T object, Predicate rule) { throwof(before, re, rule.test(object)); } default void throwofNon(String errMessage, T object, Predicate rule) { throwofNon(new RuntimeException(errMessage), object, rule); } default void throwofNon(Consumer before, String errMessage, T object, Predicate rule) { throwofNon(before, new RuntimeException(errMessage), object, rule); } default void throwofNon(RE re, T object, Predicate rule) { throwof(re, !rule.test(object)); } default void throwofNon(Consumer before, RE re, T object, Predicate rule) { throwof(before, re, !rule.test(object)); } default void throwof(String errMessage, boolean rule) { throwof(new RuntimeException(errMessage), rule); } default void throwof(Consumer before, String errMessage, boolean rule) { throwof(before, new RuntimeException(errMessage), rule); } default void throwof(String errMessage, Supplier rule) { throwof(new RuntimeException(errMessage), rule.get()); } default void throwof(Consumer before, String errMessage, Supplier rule) { throwof(before, new RuntimeException(errMessage), rule.get()); } default void throwof(RE re, Supplier rule) { throwof(re, rule.get()); } default void throwof(Consumer before, RE re, Supplier rule) { throwof(before, re, rule.get()); } }
package com.ele.util; import java.util.Objects; import java.util.function.Consumer; public interface IThrowObjectUtil extends IThrowableUtil { IThrowObjectUtil build = new IThrowObjectUtil() {}; defaultvoid throwofIsNull(RE re, T object) { throwof(re, Objects.isNull(object)); } default void throwofIsNull(Consumer before, RE re, T object) { throwof(before, re, Objects.isNull(object)); } default void throwofIsNull(String errMessage, T object) { throwofIsNull(new RuntimeException(errMessage), object); } default void throwofIsNull(Consumer before, String errMessage, T object) { throwofIsNull(before, new RuntimeException(errMessage), object); } default void throwofNonNull(RE re, T object) { throwof(re, Objects.nonNull(object)); } default void throwofNonNull(Consumer before, RE re, T object) { throwof(before, re, Objects.nonNull(object)); } default void throwofNonNull(String errMessage, T object) { throwofNonNull(new RuntimeException(errMessage), object); } default void throwofNonNull(Consumer before, String errMessage, T object) { throwofNonNull(before, new RuntimeException(errMessage), object); } default void throwofEquals(String errMessage, T object, T competitor) { throwofEquals(new RuntimeException(errMessage), object, competitor); } default void throwofEquals(Consumer before, String errMessage, T object, T competitor) { throwofEquals(before, new RuntimeException(errMessage), object, competitor); } default void throwofNotEquals(String errMessage, T object, T competitor) { throwofNotEquals(new RuntimeException(errMessage), object, competitor); } default void throwofNotEquals(Consumer before, String errMessage, T object, T competitor) { throwofNotEquals(before, new RuntimeException(errMessage), object, competitor); } default void throwofEquals(RE re, T object, T competitor) { throwof(re, Objects.equals(object, competitor)); } default void throwofEquals(Consumer before, RE re, T object, T competitor) { throwof(before, re, Objects.equals(object, competitor)); } default void throwofNotEquals(RE re, T object, T competitor) { throwof(re, !Objects.equals(object, competitor)); } default void throwofNotEquals(Consumer before, RE re, T object, T competitor) { throwof(before, re, !Objects.equals(object, competitor)); } }
package com.ele.util; import java.util.function.Consumer; public interface IThrowStringUtil extends IThrowObjectUtil { IThrowStringUtil build = new IThrowStringUtil() {}; defaultvoid throwofStrIsEmpty(RE re, String str) { throwof(re, (str == null || str.length() == 0)); } default void throwofStrIsEmpty(Consumer 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 before, String errMessage, String str) { throwofStrIsEmpty(before, new RuntimeException(errMessage), str); } default void throwofStrNotEmpty(RE re, String str) { throwof(re, !(str == null || str.length() == 0)); } default void throwofStrNotEmpty(Consumer 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 before, String errMessage, String str) { throwofStrNotEmpty(before, new RuntimeException(errMessage), str); } }
package com.ele.util; 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"); // ... Consumerlog = 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); // ... } Comsumer log(String message) { return re -> logger.error(message + "" + re.getMessage(), re); } Comsumer log() { return re -> logger.error(re.getMessage(), re); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)