欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

最无用的工具类封装——Equals条件封装 2

程序员文章站 2022-05-04 19:09:43
最无用的工具类封装——Equals条件封装 2昨天出了一篇最无用的工具类封装 EqualsUtils出这个工具类的目的是为了解决书写 if 语句比较多的时候造成的错误,结果搞出个什么鬼今天我针对昨天的 EqualsUtils 做了扩展,并使用了一个新名字——IfUtils社会我 T 哥,人狠话不多,上代码package cn.texous.test.demo.commons.utils;import lombok.Data;import java.util.ArrayList;imp...

最无用的工具类封装——Equals条件封装 2

昨天出了一篇最无用的工具类封装 EqualsUtils
出这个工具类的目的是为了解决书写 if 语句比较多的时候造成的错误,结果搞出个什么鬼
今天我针对昨天的 EqualsUtils 做了扩展,并使用了一个新名字——IfUtils

社会我 T 哥,人狠话不多,上代码
package cn.texous.test.demo.commons.utils;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;

import static cn.texous.test.demo.commons.utils.IfUtils.Condition.equalsCondition;
import static cn.texous.test.demo.commons.utils.IfUtils.Condition.newCondition;
import static cn.texous.test.demo.commons.utils.IfUtils.Condition.notEqualsCondition;

/**
 * if 条件构造工具类
 *
 * @author Showa.L
 * @since 2020/7/16 9:58
 */
public class IfUtils {

    /**
     * 判断连个对象是否 equals
     *
     * @param source 原对象
     * @param target 目标对象
     * @return
     */
    public static boolean equals(Object source, Object target) {
        if (source == target)
            return true;
        return Optional.ofNullable(source).map(s -> s.equals(target)).orElse(false);
    }

    /**
     * 判断连个对象是否 equals
     *
     * @param sourceSupplier 原对象提供者
     * @param targetSupplier 目标对象提供者
     * @return
     */
    public static boolean equals(Supplier<Object> sourceSupplier, Supplier<Object> targetSupplier) {
        return equals(sourceSupplier.get(), targetSupplier.get());
    }

    /**
     * 判断连个对象是否 notEquals
     *
     * @param source 原对象
     * @param target 目标对象
     * @return
     */
    public static boolean notEquals(Object source, Object target) {
        return !equals(source, target);
    }

    /**
     * 判断连个对象是否 notEquals
     *
     * @param sourceSupplier 原对象提供者
     * @param targetSupplier 目标对象提供者
     * @return
     */
    public static boolean notEquals(Supplier<Object> sourceSupplier, Supplier<Object> targetSupplier) {
        return notEquals(sourceSupplier.get(), targetSupplier.get());
    }

    /**
     * 判断对象是否为 true
     *
     * @param source 类型
     * @return
     */
    public static boolean isTrue(Boolean source) {
        return Optional.ofNullable(source).orElse(false);
    }

    /**
     * 判断对象是否为 false
     *
     * @param source 类型
     * @return
     */
    public static boolean isFalse(Boolean source) {
        return !isTrue(source);
    }

    /**
     * 构建对象关系
     *
     * @param condition 对象条件 {@link Condition}
     * @return
     */
    public static Relation build(Supplier<Boolean> condition) {
        return Relation.build(condition);
    }

    /**
     * 校验判断
     *
     * @param relation 条件语句
     * @return
     */
    public static Boolean validate(Relation relation) {
        return relation.validate();
    }

    /**
     * 校验成功后处理
     *
     * @param relation    校验条件
     * @param doSomething 处理方法
     */
    public static void validateThen(Relation relation, Consumer<Boolean> doSomething) {
        Optional.ofNullable(relation).map(Relation::validate).filter(IfUtils::isTrue).ifPresent(doSomething);
    }

    /**
     * 需要进行对比的对象,及对比条件
     *
     * @param <S> 对象1
     * @param <T> 对象2
     */
    @Data
    public static class Condition<S, T> implements Supplier<Boolean> {
        private S source;
        private T target;
        private List<CompareInfo<S, T>> compares = new ArrayList<>();

        private Condition(S source, T target, BiFunction<S, T, Boolean> compare) {
            this.source = source;
            this.target = target;
            this.compares.add(CompareInfo.build(compare));
        }

        @Override
        public Boolean get() {
            Boolean result = null;
            for (CompareInfo<S, T> compare : compares) {
                if (result != null) {
                    if (compare.isOr()) {
                        result = result || compare.getCompare().apply(source, target);
                    } else {
                        result = result && compare.getCompare().apply(source, target);
                    }
                } else {
                    result = compare.getCompare().apply(source, target);
                }
            }
            return result;
        }

        public Condition<S, T> and(BiFunction<S, T, Boolean> compare) {
            compares.add(CompareInfo.buildAnd(compare));
            return this;
        }

        public Condition<S, T> or(BiFunction<S, T, Boolean> compare) {
            compares.add(CompareInfo.buildOr(compare));
            return this;
        }

        public static <S, T> Condition<S, T> equalsCondition(S source, T target) {
            return new Condition<>(source, target, IfUtils::equals);
        }

        public static <S, T> Condition<S, T> equalsCondition(Supplier<S> sourceSupp, Supplier<T> targetSupp) {
            return new Condition<>(sourceSupp.get(), targetSupp.get(), IfUtils::equals);
        }

        public static <S, T> Condition<S, T> notEqualsCondition(S source, T target) {
            return new Condition<>(source, target, IfUtils::notEquals);
        }

        public static <S, T> Condition<S, T> notEqualsCondition(Supplier<S> sourceSupp, Supplier<T> targetSupp) {
            return new Condition<>(sourceSupp.get(), targetSupp.get(), IfUtils::notEquals);
        }

        public static <S, T> Condition<S, T> newCondition(S source, T target, BiFunction<S, T, Boolean> compare) {
            return new Condition<>(source, target, compare);
        }

    }

    /**
     * 兼容一组对象多个属性分别对比的情况
     *
     * @param <S> 对象1
     * @param <T> 对象2
     */
    @Data
    public static class CompareInfo<S, T> {
        private boolean isOr;
        private BiFunction<S, T, Boolean> compare;

        private CompareInfo() {
        }

        private CompareInfo(BiFunction<S, T, Boolean> compare) {
            this.compare = compare;
        }

        private CompareInfo(BiFunction<S, T, Boolean> compare, boolean isOr) {
            this.compare = compare;
            this.isOr = isOr;
        }

        public static <S, T> CompareInfo<S, T> build(BiFunction<S, T, Boolean> compare) {
            return new CompareInfo<>(compare);
        }

        public static <S, T> CompareInfo<S, T> buildAnd(BiFunction<S, T, Boolean> compare) {
            return new CompareInfo<>(compare, Boolean.FALSE);
        }

        public static <S, T> CompareInfo<S, T> buildOr(BiFunction<S, T, Boolean> compare) {
            return new CompareInfo<>(compare, Boolean.TRUE);
        }

    }

    /**
     * 节点之间的关系表示
     */
    @Data
    public static class Relation {
        private Supplier<Boolean> source;
        // 节点和上一个节点之间的关系
        private boolean isOr;
        private List<Relation> relations = new ArrayList<>();

        public Relation(Supplier<Boolean> condition) {
            this.source = condition;
        }

        public Relation(Supplier<Boolean> condition, boolean isOr) {
            this.source = condition;
            this.isOr = isOr;
        }

        public Relation andGroup(Relation relation) {
            relation.setOr(Boolean.FALSE);
            relations.add(relation);
            return this;
        }

        public Relation orGroup(Relation relation) {
            relation.setOr(Boolean.TRUE);
            relations.add(relation);
            return this;
        }

        public Relation and(Supplier<Boolean> condition) {
            Relation relation = new Relation(condition, Boolean.FALSE);
            relations.add(relation);
            return this;
        }

        public Relation or(Supplier<Boolean> condition) {
            Relation relation = new Relation(condition, Boolean.TRUE);
            relations.add(relation);
            return this;
        }

        public boolean isAnd() {
            return !isOr;
        }

        public boolean validate() {
            boolean validate = source.get();
            for (Relation relation : relations) {
                if (relation.isAnd())
                    validate = validate && relation.validate();
                else {
                    validate = validate || relation.validate();
                }
            }
            return validate;
        }

        public String toString() {
            StringBuilder sb = new StringBuilder()
                    .append("(")
                    .append(source.get());
            relations.forEach(c -> sb.append(c.isOr() ? " || " : " && ").append(c.toString()));
            sb.append(")");
            return sb.toString();
        }

        public static Relation buildAnd(Supplier<Boolean> condition) {
            return new Relation(condition, Boolean.FALSE);
        }

        public static Relation buildOr(Supplier<Boolean> condition) {
            return new Relation(condition, Boolean.TRUE);
        }

        public static Relation build(Supplier<Boolean> condition) {
            return new Relation(condition);
        }

    }

    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        boolean c = true;
        boolean d = false;
        Relation or = IfUtils.build(() -> a).and(() -> b).andGroup(IfUtils.build(() -> c).and(() -> d).orGroup(IfUtils.build(() -> a).or(() -> b))).and(() -> b).or(() -> c);
        System.out.println(or.toString());
        System.out.println(or.validate());

        Relation and = IfUtils.build(equalsCondition(a, b)).and(notEqualsCondition(c, d));
        System.out.println(and.toString());
        System.out.println(and.validate());

        String e = "e";
        String f = "f";
        String g = "g";
        System.out.println("(e == e || e == f) && (e==f || (e == e && f == f))");
        Relation relation = IfUtils.build(equalsCondition(e, e)).or(equalsCondition(e, f))
                .andGroup(IfUtils.build(equalsCondition(e, f))
                        .or(equalsCondition(e, e))
                        .and(equalsCondition(f, f)));
        validateThen(relation, System.out::println);

        System.out.println("(e == e || e == f) && (e == f || (e == e && f == g))");
        relation = IfUtils.build(equalsCondition(e, e))
                .or(equalsCondition(e, f))
                .andGroup(IfUtils.build(equalsCondition(e, f))
                        .orGroup(IfUtils.build(equalsCondition(e, e))
                                .and(equalsCondition(f, g))));
        validateThen(relation, System.out::println);
        System.out.println(relation.validate());

        User user = new User();
        user.setAge(12);
        user.setName("user 1");
        user.setNick("张三");

        User user1 = new User();
        user1.setAge(12);
        user1.setName("user 1");
        user1.setNick("里斯");

//        relation = build(equalsCondition(user::getName, user1::getName))
//                .and(equalsCondition(user::getAge, user1::getAge))
//                .and(equalsCondition(user::getAge, user1::getAge));

        relation = IfUtils.build(newCondition(user, user1, (u, u1) -> equals(u::getName, u1::getName))
                .and((u, u1) -> equals(u::getAge, u1::getAge)))
                .and(equalsCondition(user::getAge, user1::getAge));
        System.out.println(relation.toString());
        System.out.println(relation.validate());

        relation = IfUtils.build(equalsCondition(user::getName, user1::getName))
                .and(equalsCondition(user::getAge, user1::getAge));
        System.out.println(relation.toString());
        System.out.println(relation.validate());

        // relation = build(equalsCondition(user::getNick, user1::getNick));
        relation = IfUtils.build(newCondition(user, user1, (u, u1) -> equals(u::getNick, u1::getNick)));
        System.out.println(relation.toString());
        System.out.println(relation.validate());

        System.out.println("简洁性对比1-------------------------");
        if (equals(user::getName, user1::getName)
                && equals(user::getAge, user1::getAge)
                && equals(user::getAge, user::getAge)) {
            System.out.println(true);
        }

        validateThen(build(equalsCondition(user::getName, user1::getName))
                        .and(equalsCondition(user::getAge, user1::getAge))
                        .and(equalsCondition(user::getAge, user1::getAge)),
                System.out::println);

        System.out.println("可观看性对比-----------------------");
        if (equals(user::getName, user1::getName)
                && (notEquals(user::getNick, user1::getNick)
                || notEquals(user::getAge, user1::getAge)))
            System.out.println(true);

        validateThen(build(equalsCondition(user::getName, user1::getName))
                        .andGroup(build(notEqualsCondition(user::getNick, user1::getNick))
                                .or(equalsCondition(user::getAge, user1::getAge))),
                System.out::println);

        // 要他有何用
    }

    @Data
    private static final class User {
        private String name;
        private Integer age;
        private String nick;
    }

}

经过一系列的测试对比后发现,真的是一点用都没有

加粗的一群乌鸦飞过。

乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦乌鸦

我要这代码有何用。

本文地址:https://blog.csdn.net/qq_28038487/article/details/107390305