diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/RetOps.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/RetOps.java index e2908b78b..4929db68d 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/RetOps.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/RetOps.java @@ -106,6 +106,15 @@ public class RetOps { return Optional.of(original.getData()); } + /** + * 有条件地读取{@code data}的值 + * @param predicate 断言函数 + * @return 返回 Optional 包装的data,如果断言失败返回empty + */ + public Optional getDataIf(Predicate> predicate) { + return predicate.test(original) ? getData() : Optional.empty(); + } + /** * 读取{@code msg}的值 * @return 返回Optional包装的 msg @@ -218,6 +227,22 @@ public class RetOps { return of(result); } + /** + * 对业务数据(data)转换 + * @param predicate 断言函数 + * @param mapper 业务数据转换函数 + * @param 数据类型 + * @return 返回新实例,以便于继续进行链式操作 + * @see RetOps#CODE_SUCCESS + * @see RetOps#HAS_DATA + * @see RetOps#HAS_ELEMENT + * @see RetOps#DATA_AVAILABLE + */ + public RetOps mapIf(Predicate> predicate,Function mapper) { + R result = R.restResult(mapper.apply(original.getData()), original.getCode(), original.getMsg()); + return of(result);?? + } + // ~ 数据消费 // ===================================================================================================