最无用的工具类封装——Equals条件封装
程序员文章站
2022-11-15 20:30:41
最无用的工具类封装——Equals条件封装主要实现了 equals 条件判断构造例如:(a==b || b != c) || ((a == c && b != c) || a ==b)package cn.texous.test.demo.commons.utils;import lombok.Data;import java.util.ArrayList;import java.util.List;/** * insert description here *...
最无用的工具类封装——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 函数
就是这么没用,就是这么自信
至于运用到别的场景,请自行探索
本文地址:https://blog.csdn.net/qq_28038487/article/details/107368179
下一篇: 好好发泄发泄