提交 40516d95 编写于 作者: 如梦技术's avatar 如梦技术 🐛

mica-core R 添加 throwOn 系列方法。

上级 575a568b
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
- :sparkles: mica-ip2region、mica-captcha 添加对 spring-native 的支持 #38 #I3PX2N - :sparkles: mica-ip2region、mica-captcha 添加对 spring-native 的支持 #38 #I3PX2N
- :sparkles: mica-jetcache 完善 metrics 待续 #37 #I3PX2K - :sparkles: mica-jetcache 完善 metrics 待续 #37 #I3PX2K
- :sparkles: mica-caffeine 添加不支持自定义 Caffeine bean 提示。 - :sparkles: mica-caffeine 添加不支持自定义 Caffeine bean 提示。
- :sparkles: mica-core R 添加 throwOn 系列方法。
- :bug: mica-logging 修复 LoggingInitializer Spring boot 2.4.x 失效的问题。 - :bug: mica-logging 修复 LoggingInitializer Spring boot 2.4.x 失效的问题。
- :arrow_up: 升级 druid 到 1.2.6 - :arrow_up: 升级 druid 到 1.2.6
......
...@@ -27,6 +27,8 @@ import org.springframework.lang.Nullable; ...@@ -27,6 +27,8 @@ import org.springframework.lang.Nullable;
import java.io.Serializable; import java.io.Serializable;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
/** /**
* 响应信息主体 * 响应信息主体
...@@ -187,6 +189,49 @@ public class R<T> implements Serializable { ...@@ -187,6 +189,49 @@ public class R<T> implements Serializable {
return new R<>(rCode, msg); return new R<>(rCode, msg);
} }
/**
* 断言 成功时:直接抛出失败异常,返回传入的 result。
*
* @param predicate 断言函数
* @param object 模型
* @param result 响应对象
* @param <T> 泛型
*/
public static <T> void throwOn(Predicate<T> predicate, T object, R<?> result) {
if (predicate.test(object)) {
throw new ServiceException(result);
}
}
/**
* 断言 成功时:直接抛出失败异常,返回传入的 result。
*
* @param predicate 断言函数
* @param object 模型
* @param rCode 响应 code
* @param <T> 泛型
*/
public static <T> void throwOn(Predicate<T> predicate, T object, IResultCode rCode) {
if (predicate.test(object)) {
throw new ServiceException(rCode);
}
}
/**
* 断言 成功时:直接抛出失败异常,返回传入的 result。
*
* @param predicate 断言函数
* @param object 模型
* @param rCode 响应 code
* @param msg 自定义消息
* @param <T> 泛型
*/
public static <T> void throwOn(Predicate<T> predicate, T object, IResultCode rCode, String msg) {
if (predicate.test(object)) {
throw new ServiceException(rCode, msg);
}
}
/** /**
* 当 result 不成功时:直接抛出失败异常,返回传入的 result。 * 当 result 不成功时:直接抛出失败异常,返回传入的 result。
* *
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册