依次校验参数列表中传入的参数(方法的值)是否非空,如果是则返回第一个符合条件的参数(方法的值)

import org.springframework.util.ObjectUtils;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @author <a href="https://blog.csdn.net/bnwbkiu?spm=1010.2135.3001.5343">瞎琢磨先生</a>
 * @date 2023-4-15 22:40:47
 */
public class CheckUtil {

    /**
     * 依次校验参数列表,如果存在参数符合不为 null(同时不为空),则返回第一个符合的参数的值
     */
    public static <T> T checkFindFirstNotEmpty(T... args) {
        return checkFindFirst(Objects::nonNull, args);
    }

    /**
     * 依次校验参数列表中传入的方法执行的结果是否符合不为 null(同时不为空),如果存在方法执行的结果符合不为 null(同时不为空),则返回第一个符合的方法的结果
     */
    public static <T> T checkFindFirstNotEmpty(Supplier<T>... suppliers) {
        return checkFindFirstLazy(Objects::nonNull, suppliers);
    }

    /**
     * 依次校验参数列表,如果参数符合某种规则,则返回第一个符合的参数的值
     *
     * @param args 参数列表
     * @return 第一个非空参数的值,如果所有参数都为空则返回null
     */

    public static <T> T checkFindFirst(Predicate<T> predicate, T... args) {

        return Optional.ofNullable(args)

                .flatMap(arr -> Stream.of(arr).filter(predicate::test).findFirst())

                .orElse(null);
    }

    /**
     * 依次校验参数列表中传入的方法执行的结果是否符合某种规则,如果存在方则返回第一个符合的方法的结果
     *
     * @param suppliers 参数列表中的方法
     */
    public static <T> T checkFindFirstLazy(Predicate<T> predicate, Supplier<T>... suppliers) {

        return Stream.of(suppliers)
                .map(Supplier::get)
                .filter(predicate::test)
                .findFirst()
                .orElse(null);
    }

}

class Test {

    public static void main(String[] args) {

        // 检查参数
        String result1 = CheckUtil.checkFindFirstNotEmpty(null, "", "hello", "world"); // 返回"hello"
        System.out.println("result1 = " + result1);
        Integer result2 = CheckUtil.checkFindFirstNotEmpty(null, 0, 1, 2); // 返回0
        System.out.println("result2 = " + result2);

        // 检查方法返回值
        String result3 = CheckUtil.checkFindFirstNotEmpty(() -> null, () -> "", () -> "hello", () -> "world"); // 返回"hello"
        System.out.println("result3 = " + result3);
        Integer result4 = CheckUtil.checkFindFirstNotEmpty(() -> null, () -> 0, () -> 1, () -> 2); // 返回0
        System.out.println("result4 = " + result4);

        String result5 = CheckUtil.checkFindFirst(x -> !ObjectUtils.isEmpty(x), "", null, "111", null, new Test().getStr("s11", "s22"));
        System.out.println("result5 = " + result5);

        String result6 = CheckUtil.checkFindFirstLazy(x -> !ObjectUtils.isEmpty(x), () -> "", () -> null, () -> "111", () -> null, () -> new Test().getStr("s11", "s22"));
        System.out.println("result6 = " + result6);

    }

    public String getStr(String s1, String s2) {
        System.out.println("Test.getStr");
        return s1 + "," + s2;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值