Skip to content

Commit 19243cb

Browse files
增加Expression比较的方法
1 parent cad9f77 commit 19243cb

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ target
33
.classpath
44
.project
55
.idea
6-
*.iml
6+
*.iml
7+
8+
*/src/test

src/main/java/com/github/developframework/expression/ArrayExpression.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,29 @@ public String toString() {
8484
}
8585
return parentExpression + "." + propertyName + "[" + index + "]";
8686
}
87+
88+
@Override
89+
public int hashCode() {
90+
int hash = 7;
91+
if(parentExpression != null) {
92+
hash = hash * 31 + parentExpression.hashCode();
93+
}
94+
hash = hash * 31 + propertyName.hashCode();
95+
hash = hash * 31 + index;
96+
return hash;
97+
}
98+
99+
@Override
100+
public boolean equals(Object obj) {
101+
if (obj instanceof ArrayExpression) {
102+
ArrayExpression otherExpression = (ArrayExpression) obj;
103+
if(propertyName.equals(otherExpression.getPropertyName()) && index == otherExpression.getIndex()) {
104+
if(this.hasParentExpression() && otherExpression.hasParentExpression()) {
105+
return parentExpression.equals(otherExpression.getParentExpression());
106+
}
107+
return !this.hasParentExpression() && !this.hasParentExpression();
108+
}
109+
}
110+
return false;
111+
}
87112
}

src/main/java/com/github/developframework/expression/EmptyExpression.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ public class EmptyExpression extends Expression{
1111
public String toString() {
1212
return "";
1313
}
14+
15+
@Override
16+
public boolean equals(Object obj) {
17+
return this == obj;
18+
}
1419
}

src/main/java/com/github/developframework/expression/ObjectExpression.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
/**
66
* 对象表达式
77
* 示例: abc
8+
*
89
* @author qiuzhenhao
910
* @date 2017/5/6
1011
*/
11-
public class ObjectExpression extends Expression{
12+
public class ObjectExpression extends Expression {
1213

1314
/* 属性名称 */
1415
@Getter
@@ -20,9 +21,33 @@ public ObjectExpression(String propertyName) {
2021

2122
@Override
2223
public String toString() {
23-
if(parentExpression == null) {
24+
if (parentExpression == null) {
2425
return propertyName;
2526
}
2627
return parentExpression + "." + propertyName;
2728
}
29+
30+
@Override
31+
public int hashCode() {
32+
int hash = 7;
33+
if(this.hasParentExpression()) {
34+
hash = hash * 31 + parentExpression.hashCode();
35+
}
36+
hash = hash * 31 + propertyName.hashCode();
37+
return hash;
38+
}
39+
40+
@Override
41+
public boolean equals(Object obj) {
42+
if (obj instanceof ObjectExpression) {
43+
ObjectExpression otherExpression = (ObjectExpression) obj;
44+
if(propertyName.equals(otherExpression.getPropertyName())) {
45+
if(this.hasParentExpression() && otherExpression.hasParentExpression()) {
46+
return parentExpression.equals(otherExpression.getParentExpression());
47+
}
48+
return !this.hasParentExpression() && !this.hasParentExpression();
49+
}
50+
}
51+
return false;
52+
}
2853
}

0 commit comments

Comments
 (0)