Java逻辑运算解析器

版本1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.example.criteria;

// 定义 Criteria 接口
interface Criteria {
boolean match(); // 判断是否满足条件的方法
}

// 创建具体的 Criteria 实现类
class AgeCriteria implements Criteria {
private int age;

public AgeCriteria(int age) {
this.age = age;
}

@Override
public boolean match() {
// 实际的判断逻辑
// 这里只是一个简单的示例,你可以根据实际情况修改
return age >= 18;
}
}

class NameCriteria implements Criteria {
private String name;

public NameCriteria(String name) {
this.name = name;
}

@Override
public boolean match() {
// 实际的判断逻辑
// 这里只是一个简单的示例,你可以根据实际情况修改
return name.startsWith("A");
}
}

// 创建逻辑关系类
class AndCriteria implements Criteria {
private Criteria criteria1;
private Criteria criteria2;

public AndCriteria(Criteria criteria1, Criteria criteria2) {
this.criteria1 = criteria1;
this.criteria2 = criteria2;
}

@Override
public boolean match() {
// AND 关系的判断
return criteria1.match() && criteria2.match();
}
}

class OrCriteria implements Criteria {
private Criteria criteria1;
private Criteria criteria2;

public OrCriteria(Criteria criteria1, Criteria criteria2) {
this.criteria1 = criteria1;
this.criteria2 = criteria2;
}

@Override
public boolean match() {
// OR 关系的判断
return criteria1.match() || criteria2.match();
}
}

// 测试
class Main {
public static void main(String[] args) {
Criteria ageCriteria = new AgeCriteria(20);
Criteria nameCriteria = new NameCriteria("Alice");

// 使用 AND 关系
Criteria andCriteria = new AndCriteria(ageCriteria, nameCriteria);
System.out.println("AND 结果:" + andCriteria.match());

// 使用 OR 关系
Criteria orCriteria = new OrCriteria(ageCriteria, nameCriteria);
System.out.println("OR 结果:" + orCriteria.match());
}
}

版本2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.example.criteria;

import java.util.List;

// 抽象 Criteria 接口
interface Criteria {
boolean match();

static Criteria and(Criteria criteria1, Criteria criteria2) {
// AND 关系的实现
return new AndCriteria(List.of(criteria1, criteria2));
}

static Criteria or(Criteria criteria1, Criteria criteria2) {
// OR 关系的实现
return new OrCriteria(List.of(criteria1, criteria2));
}
}

// 具体的 Criteria 实现类
class SimpleCriteria implements Criteria {
private boolean result;

public SimpleCriteria(boolean result) {
this.result = result;
}

@Override
public boolean match() {
return result;
}
}

class AndCriteria implements Criteria {
private List<Criteria> criteriaList;

public AndCriteria(List<Criteria> criteriaList) {
this.criteriaList = criteriaList;
}

@Override
public boolean match() {
// AND 关系的判断
return criteriaList.stream().allMatch(Criteria::match);
}
}

class OrCriteria implements Criteria {
private List<Criteria> criteriaList;

public OrCriteria(List<Criteria> criteriaList) {
this.criteriaList = criteriaList;
}

@Override
public boolean match() {
// OR 关系的判断
return criteriaList.stream().anyMatch(Criteria::match);
}
}

// 测试
class Main {
public static void main(String[] args) {
// 使用静态方法构建 AND 和 OR 关系
Criteria criteriaAnd = Criteria.and(new SimpleCriteria(true), new SimpleCriteria(true));
Criteria criteriaOr = Criteria.or(new SimpleCriteria(true), new SimpleCriteria(false));

System.out.println("AND 结果:" + criteriaAnd.match());
System.out.println("OR 结果:" + criteriaOr.match());
}
}

版本3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.example.criteria;

// 抽象 Criteria 接口
interface Criteria {
boolean match();

static Criteria and(Criteria... criteriaArray) {
// AND 关系的实现
return () -> {
for (Criteria criteria : criteriaArray) {
if (!criteria.match()) {
return false;
}
}
return true;
};
}

static Criteria or(Criteria... criteriaArray) {
// OR 关系的实现
return () -> {
for (Criteria criteria : criteriaArray) {
if (criteria.match()) {
return true;
}
}
return false;
};
}
}

// 测试
class Main {
public static void main(String[] args) {

// 使用静态方法构建 AND 关系,支持多个条件
Criteria criteriaMultipleAnd = Criteria.and(() -> false, () -> true, () -> true);

System.out.println("多个条件 AND 结果:" + criteriaMultipleAnd.match());
}
}

版本4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.example.criteria;

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

public class Criteria {
private String field;
private Object value;
private Operator operator;
private List<Criteria> chain;

private Criteria() {
// 私有构造函数,只允许使用静态方法创建 Criteria 实例
}

public static Criteria where(String field, Operator operator, Object value) {
Criteria criteria = new Criteria();
criteria.field = field;
criteria.operator = operator;
criteria.value = value;
return criteria;
}

public Criteria and(Criteria criteria) {
this.operator = Operator.AND;
addCriteriaToChain(criteria);
return this;
}

public Criteria or(Criteria criteria) {
this.operator = Operator.OR;
addCriteriaToChain(criteria);
return this;
}

public Criteria eq(String field, Object value) {
addCriteriaToChain(Criteria.where(field, Operator.EQUALS, value));
return this;
}

public Criteria gt(String field, Object value) {
addCriteriaToChain(Criteria.where(field, Operator.GREATER_THAN, value));
return this;
}

private void addCriteriaToChain(Criteria criteria) {
if (chain == null) {
chain = new ArrayList<>();
}
chain.add(criteria);
}

public String build() {
StringBuilder result = new StringBuilder();

if (chain != null) {
result.append("(").append(field).append(" ").append(operator).append(" ");
for (Criteria criteria : chain) {
result.append(criteria.build()).append(" ");
}
result.append(")");
} else {
result.append(field).append(" ").append(operator).append(" ").append(value);
}

return result.toString();
}

public static void main(String[] args) {
Criteria criteria = Criteria.where("age", Operator.GREATER_THAN, 18)
.and(Criteria.where("gender", Operator.EQUALS, "female"))
.or(Criteria.where("city", Operator.EQUALS, "New York"))
.and(Criteria.where("score", Operator.GREATER_THAN, 90));

String query = criteria.build();
System.out.println("Query: " + query);
}

public enum Operator {
EQUALS("="),
GREATER_THAN(">"),
LESS_THAN("<"),
AND("AND"),
OR("OR");

private final String symbol;

Operator(String symbol) {
this.symbol = symbol;
}

@Override
public String toString() {
return symbol;
}
}
}