【面向对象编程】参考 vavr 自己写的一个简化 if...else 的对象封装, 有问题请指正

public class ChoiceUtil<T, K, U> {
    private final T t;
    private U u;
    private final AtomicBoolean isStop = new AtomicBoolean(false);

    private ChoiceUtil(T t) {
        this.t = t;
    }

    public static <T, K, U> ChoiceUtil<T, K, U> of(T t) {
        return new ChoiceUtil<>(t);
    }

    public ChoiceUtil<T, K, U> iF(Predicate<? super T> predicate, Function<? super T, ? extends U> f) {
        if (!isStop.get()) {
            isStop.set(predicate.test(t));
            u = isStop.get() ? f.apply(t) : null;
        }
        return this;
    }

    public ChoiceUtil<T, K, U> iF(Predicate<? super T> predicate, K k, Function<? super K, ? extends U> f) {
        if (!isStop.get()) {
            isStop.set(predicate.test(t));
            u = isStop.get() ? f.apply(k) : null;
        }
        return this;
    }

    public ChoiceUtil<T, K, U> finallyElse(Function<? super T, ? extends U> f) {
        if (!isStop.get()) {
            u = f.apply(t);
            isStop.set(true);
        }
        return this;
    }

    public ChoiceUtil<T, K, U> finallyElse(K k, Function<? super K, ? extends U> f) {
        if (!isStop.get()) {
            u = f.apply(k);
            isStop.set(true);
        }
        return this;
    }

    public void peekFinallyElse(Consumer<? super T> a) {
        if (!isStop.get()) {
            a.accept(t);
            isStop.set(true);
        }
    }

    public void peekFinallyElse(K k, Consumer<? super K> a) {
        if (!isStop.get()) {
            a.accept(k);
            isStop.set(true);
        }
    }

    public ChoiceUtil<T, K, U> peekIf(Predicate<? super T> predicate, Consumer<? super T> a) {
        if (!isStop.get()) {
            isStop.set(predicate.test(t));
            if (isStop.get()) {
                a.accept(t);
            }
        }
        return this;
    }

    public ChoiceUtil<T, K, U> peekIf(Predicate<? super T> predicate, K k, Consumer<? super K> a) {
        if (!isStop.get()) {
            isStop.set(predicate.test(t));
            if (isStop.get()) {
                a.accept(k);
            }
        }
        return this;
    }

    public U result() {
        return u;
    }


    public static void main(String[] args) {
        String a = "555";
        String b = "333";

        /*Object result = ChoiceUtil.of(a)
                .iF("1"::equals, r -> "if 1")
                .iF("2"::equals, r -> "else if 2")
                .iF("3"::equals, r -> "else if 3")
                .iF("4"::equals, b, "222"::equals)
                .finallyElse(b, "333"::equals)
                .finallyElse(r -> "else 4")
                .result();
        System.out.println(result);*/

        ChoiceUtil.of(a)
                .peekIf("1"::equals, r -> System.out.println("peek if 1"))
                .peekIf("4"::equals, b, System.out::println)
                .peekIf("2"::equals, r -> System.out.println("peek else if 2"))
                .peekIf("3"::equals, r -> System.out.println("peek else if 3"))
                .peekFinallyElse(b, System.out::println);
                //.peekFinallyElse(r -> System.out.println("peek else 4"));

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值