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

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

主要实现了 equals 条件判断构造
例如:(a==b || b != c) || ((a == c && b != c) || a ==b)

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

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * insert description here
 *
 * @author Showa.L
 * @since 2020/7/15 10:01
 */
public class EqualsUtils {

    public static Condition equals(Object source, Object target) {
        return new Condition(source, target, PareType.EQ);
    }

    public static Condition notEquals(Object source, Object target) {
        return new Condition(source, target, PareType.NE);
    }

    @Data
    public static class Condition {
        private Object source;
        private Object target;
        private PareType pareType;
        private RelationType relationType;
        private List<Condition> conditions = new ArrayList<>();

        public Condition(Object source, Object target, PareType pareType) {
            this.source = source;
            this.target = target;
            this.pareType = pareType;
        }

        public Condition andGroup(Condition condition) {
            condition.setRelationType(RelationType.AND);
            conditions.add(condition);
            return this;
        }

        public Condition orGroup(Condition condition) {
            condition.setRelationType(RelationType.OR);
            conditions.add(condition);
            return this;
        }

        public Condition andEquals(Object source, Object target) {
            Condition condition = EqualsUtils.equals(source, target);
            condition.setRelationType(RelationType.AND);
            conditions.add(condition);
            return this;
        }

        public Condition andNotEquals(Object source, Object target) {
            Condition condition = EqualsUtils.notEquals(source, target);
            condition.setRelationType(RelationType.AND);
            conditions.add(condition);
            return this;
        }

        public Condition orEquals(Object source, Object target) {
            Condition condition = EqualsUtils.equals(source, target);
            condition.setRelationType(RelationType.OR);
            conditions.add(condition);
            return this;
        }

        public Condition orNotEquals(Object source, Object target) {
            Condition condition = EqualsUtils.notEquals(source, target);
            condition.setRelationType(RelationType.OR);
            conditions.add(condition);
            return this;
        }

        public boolean validate() {
            boolean validate = compare(this);
            // 只要有一个 or 为 true 则返回 true
            for (Condition condition : conditions) {
                if (RelationType.AND.equals(condition.getRelationType()))
                    validate = validate && condition.validate();
                else {
                    validate = validate || condition.validate();
                }
            }
            return validate;
        }

        public static final boolean compare(Condition condition) {
            return PareType.EQ.equals(condition.getPareType()) == condition.getSource().equals(condition.getTarget());
        }

        public String toString() {
            return toString(false);
        }

        public String toString(final boolean basic) {
            StringBuilder sb = new StringBuilder()
                    .append("(");
            if (basic) {
                sb.append(source).append(PareType.NE.equals(pareType) ? " != " : " == ")
                        .append(target);
            } else {
                if (PareType.NE.equals(pareType))
                    sb.append("!");
                sb.append(source).append(".equals(")
                        .append(target)
                        .append(")");
            }
            conditions.forEach(c -> sb.append(RelationType.AND.equals(c.getRelationType()) ? " && " : " || ").append(c.toString(basic)));
            sb.append(")");
            return sb.toString();
        }

    }

    public enum PareType {
        EQ,
        NE,
        ;
    }

    public enum RelationType {
        AND,
        OR
    }

    public static void main(String[] args) {
        // (a == a || a == b) && (a==b || (a == a && b == b))
        String a = "a";
        String b = "b";
        String c = "c";

        Condition validate = equals(a, a).orEquals(a, b).andGroup(equals(a, b).orGroup(equals(a, a).andEquals(b, b)));
        System.out.println(validate.toString(true));
        System.out.println(validate.validate() + "    (a == a || a == b) && (a==b || (a == a && b == b))");

        // (a == a || a == b) && (a==b || (a == a && b == c))
        validate = equals(a, a).orEquals(a, b).andGroup(equals(a, b).orGroup(equals(a, a).andEquals(b, c)));
        System.out.println(validate.toString(true));
        System.out.println(validate.validate() + "    (a == a || a == b) && (a==b || (a == a && b == c))");

        // (a == a || a == b) && (a==b || (a == a && b != c))
        validate = equals(a, a).orEquals(a, b).andGroup(equals(a, b).orGroup(equals(a, a).andNotEquals(b, c)));
        System.out.println(validate.toString(true));
        System.out.println(validate.validate() + "    (a == a || a == b) && (a==b || (a == a && b != c))");

        // (a == a && a == b) && (a==b || (a == a && b != c))
        validate = equals(a, a).andEquals(a, b).andGroup(equals(a, b).orGroup(equals(a, a).andNotEquals(b, c)));
        System.out.println(validate.toString(true));
        System.out.println(validate.validate() + "    (a == a || a == b) && (a==b || (a == a && b != c))");

        boolean b1 = (a.equals(a) && (a.equals(b)) && (a.equals(b) || (a.equals(a) && (!b.equals(c)))) || (!a.equals(b)));
        // (a == a && a == b) && (a==b || (a == a && b != c)) || a != b
        validate = equals(a, a).andEquals(a, b)
                .andGroup(equals(a, b).orGroup(equals(a, a)
                                .andNotEquals(b, c)
                        )
                )
                .orNotEquals(a, b);
        System.out.println(validate.toString(true));
        System.out.println(validate.validate() + "    (a == a && a == b) && (a==b || (a == a && b != c)) || a != b");

        System.out.println(equals(a, a).validate());
        System.out.println(equals(a, c).validate());
        //  (a == a || a != b) && (a==b || (a == a && c == c))
        validate = equals(a, a).orNotEquals(a, b).andGroup(equals(a, b).orGroup(equals(a, a).andEquals(b, c)));
        System.out.println(validate.toString(false));
        System.out.println(validate.validate() + "    (a == a || a != b) && (a==b || (a == a && b == c))");
    }

}

具体的使用方式就不说了,请直接看 main 函数

就是这么没用,就是这么自信

至于运用到别的场景,请自行探索

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值