Skip to content

Commit 34cab56

Browse files
多维数组
1 parent f619040 commit 34cab56

File tree

11 files changed

+191
-124
lines changed

11 files changed

+191
-124
lines changed

pom.xml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.github.developframework</groupId>
66
<artifactId>expression</artifactId>
7-
<version>1.5.2-SNAPSHOT</version>
7+
<version>1.6.0-SNAPSHOT</version>
88

99
<name>Expression Framework</name>
1010
<url>https://github.com/developframework/expression</url>
@@ -42,22 +42,24 @@
4242
<properties>
4343
<maven.compiler.source>11</maven.compiler.source>
4444
<maven.compiler.target>11</maven.compiler.target>
45-
46-
<version.lombok>1.18.24</version.lombok>
47-
<version.commons-lang3>3.12.0</version.commons-lang3>
4845
</properties>
4946

5047
<dependencies>
5148
<dependency>
5249
<groupId>org.projectlombok</groupId>
5350
<artifactId>lombok</artifactId>
54-
<version>${version.lombok}</version>
55-
<optional>true</optional>
51+
<version>1.18.24</version>
5652
</dependency>
5753
<dependency>
5854
<groupId>org.apache.commons</groupId>
5955
<artifactId>commons-lang3</artifactId>
60-
<version>${version.commons-lang3}</version>
56+
<version>3.12.0</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>junit</groupId>
60+
<artifactId>junit</artifactId>
61+
<version>4.13.2</version>
62+
<scope>test</scope>
6163
</dependency>
6264
</dependencies>
6365

@@ -71,7 +73,7 @@
7173
<path>
7274
<groupId>org.projectlombok</groupId>
7375
<artifactId>lombok</artifactId>
74-
<version>${version.lombok}</version>
76+
<version>1.18.24</version>
7577
</path>
7678
</annotationProcessorPaths>
7779
</configuration>

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
/**
1313
* 数组表达式
1414
* 示例: abc[i]
15+
* 支持多维数组 matrix[x][y][z]
1516
*
16-
* @author qiuzhenhao
17+
* @author qiushui
1718
*/
1819
@Getter
1920
public class ArrayExpression extends Expression {
@@ -27,41 +28,41 @@ public class ArrayExpression extends Expression {
2728
* @param expressionValue 表达式字符串
2829
*/
2930
protected ArrayExpression(String expressionValue) {
30-
super(expressionValue);
3131
if (!isArrayExpression(expressionValue)) {
3232
throw new ExpressionParseException("The expression \"%s\" is not a array type expression.", expressionValue);
3333
}
34+
this.expressionValue = expressionValue;
3435
final int bracketStart = expressionValue.indexOf("[");
35-
this.propertyName = expressionValue.substring(0, bracketStart);
36+
this.name = expressionValue.substring(0, bracketStart);
3637
StringBuilder sb = new StringBuilder();
37-
List<Integer> indexs = new LinkedList<>();
38+
List<Integer> indexList = new LinkedList<>();
3839
for (char c : expressionValue.substring(bracketStart).toCharArray()) {
3940
switch (c) {
4041
case '[':
4142
sb.setLength(0);
4243
break;
4344
case ']':
44-
indexs.add(Integer.parseInt(sb.toString()));
45+
indexList.add(Integer.parseInt(sb.toString()));
4546
break;
4647
default:
4748
sb.append(c);
4849
break;
4950
}
5051
}
51-
this.indexArray = indexs.stream().mapToInt(Integer::intValue).toArray();
52+
this.indexArray = indexList.stream().mapToInt(Integer::intValue).toArray();
5253
}
5354

5455
protected ArrayExpression(String propertyName, int[] indexArray) {
55-
super(propertyName + IntStream.of(indexArray).mapToObj(i -> "[" + i + "]").collect(Collectors.joining()));
56-
this.propertyName = propertyName;
56+
this.expressionValue = propertyName + IntStream.of(indexArray).mapToObj(i -> "[" + i + "]").collect(Collectors.joining());
57+
this.name = propertyName;
5758
this.indexArray = indexArray;
5859
}
5960

6061
/**
6162
* 判断是否有属性名称
6263
*/
6364
public boolean hasPropertyName() {
64-
return !propertyName.isEmpty();
65+
return !name.isEmpty();
6566
}
6667

6768
/**
@@ -71,7 +72,7 @@ public boolean hasPropertyName() {
7172
* @return 检测结果
7273
*/
7374
public static boolean isArrayExpression(String expressionValue) {
74-
return expressionValue.matches("^\\w*(\\[\\d+])+$");
75+
return expressionValue != null && expressionValue.matches("^\\w*(\\[\\d+])+$");
7576
}
7677

7778
/**
@@ -82,7 +83,7 @@ public static boolean isArrayExpression(String expressionValue) {
8283
* @return 数组表达式
8384
*/
8485
public static ArrayExpression fromObject(ObjectExpression objectExpression, int[] indexArray) {
85-
ArrayExpression arrayExpression = new ArrayExpression(objectExpression.getPropertyName(), indexArray);
86+
ArrayExpression arrayExpression = new ArrayExpression(objectExpression.getName(), indexArray);
8687
arrayExpression.setParentExpression(objectExpression.getParentExpression());
8788
return arrayExpression;
8889
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
package com.github.developframework.expression;
22

3+
import org.apache.commons.lang3.StringUtils;
4+
35
/**
46
* 空表达式
57
*
6-
* @author qiuzhenhao
8+
* @author qiushui
79
*/
810
public class EmptyExpression extends Expression {
911

1012
public static final EmptyExpression INSTANCE = new EmptyExpression();
1113

12-
public EmptyExpression() {
13-
super("");
14+
private EmptyExpression() {
15+
expressionValue = "";
16+
}
17+
18+
/**
19+
* 检测expressionValue是否是空表达式
20+
*
21+
* @param expressionValue 表达式字符串
22+
* @return 检测结果
23+
*/
24+
public static boolean isEmptyExpression(String expressionValue) {
25+
return StringUtils.isEmpty(expressionValue);
1426
}
1527
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.github.developframework.expression;
22

33
import com.github.developframework.expression.exception.ExpressionException;
4-
import lombok.AccessLevel;
54
import lombok.EqualsAndHashCode;
65
import lombok.Getter;
7-
import lombok.RequiredArgsConstructor;
86
import org.apache.commons.lang3.StringUtils;
97

108
import java.util.Collections;
@@ -14,24 +12,23 @@
1412
/**
1513
* 表达式抽象基类
1614
*
17-
* @author qiuzhenhao
15+
* @author qiushui
1816
*/
1917
@Getter
2018
@EqualsAndHashCode(of = {"parentExpression", "expressionValue"})
21-
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
2219
public abstract class Expression {
2320

24-
protected final String expressionValue;
21+
protected String expressionValue;
2522

2623
/* 父表达式对象 */
2724
protected Expression parentExpression = EmptyExpression.INSTANCE;
2825

29-
/* 属性名称 */
30-
protected String propertyName;
26+
/* 名称 */
27+
protected String name;
3128

3229
@Override
3330
public String toString() {
34-
return (parentExpression == EmptyExpression.INSTANCE ? "" : parentExpression + ".") + expressionValue;
31+
return (parentExpression == null || parentExpression == EmptyExpression.INSTANCE ? "" : parentExpression + ".") + expressionValue;
3532
}
3633

3734
public void setParentExpression(Expression parentExpression) {
@@ -153,10 +150,10 @@ private static Expression parseSingle(String singleExpressionValue) {
153150
public static Expression copy(Expression expression) {
154151
Expression newExpression;
155152
if (expression instanceof ObjectExpression) {
156-
newExpression = new ObjectExpression(((ObjectExpression) expression).getPropertyName());
153+
newExpression = new ObjectExpression(expression.getName());
157154
} else if (expression instanceof ArrayExpression) {
158155
ArrayExpression arrayExpression = (ArrayExpression) expression;
159-
newExpression = new ArrayExpression(arrayExpression.getPropertyName(), arrayExpression.getIndexArray());
156+
newExpression = new ArrayExpression(arrayExpression.getName(), arrayExpression.getIndexArray());
160157
} else {
161158
newExpression = EmptyExpression.INSTANCE;
162159
}

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

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* 表达式取值工具
1313
*
14-
* @author qiuzhenhao
14+
* @author qiushui
1515
*/
1616
@SuppressWarnings("unchecked")
1717
public final class ExpressionUtils {
@@ -65,25 +65,25 @@ public static <T> T getValue(Object instance, Expression expression, Class<T> ta
6565
* @return 值
6666
*/
6767
public static Object getValue(Object instance, Expression expression) {
68-
if (expression == null) {
69-
throw new ExpressionException("expression is null");
68+
if (instance == null) {
69+
return null;
7070
}
71-
if (expression == EmptyExpression.INSTANCE) {
71+
if (expression == null || expression == EmptyExpression.INSTANCE) {
7272
return instance;
7373
}
74-
Object tempObject = instance;
74+
Object value = instance;
7575
for (Expression singleExpression : expression.expressionTree()) {
76-
if (tempObject == null) {
77-
return null;
76+
if (value == null) {
77+
break;
7878
} else if (singleExpression instanceof ObjectExpression) {
79-
tempObject = getValueFromObjectOrMap(tempObject, ((ObjectExpression) singleExpression).getPropertyName());
79+
value = getValueFromObjectOrMap(value, singleExpression.getName());
8080
} else if (singleExpression instanceof ArrayExpression) {
81-
tempObject = getValueFromArray(tempObject, (ArrayExpression) singleExpression);
81+
value = getValueFromArray(value, (ArrayExpression) singleExpression);
8282
} else if (singleExpression instanceof MethodExpression) {
83-
tempObject = getValueFromMethod(instance, tempObject, (MethodExpression) singleExpression);
83+
value = getValueFromMethod(instance, value, (MethodExpression) singleExpression);
8484
}
8585
}
86-
return tempObject;
86+
return value;
8787
}
8888

8989
/**
@@ -113,23 +113,27 @@ private static Object getValueFromObjectOrMap(Object instance, String propertyNa
113113
private static Object getValueFromArray(Object instance, ArrayExpression arrayExpression) {
114114
Object arrayObject = instance;
115115
if (arrayExpression.hasPropertyName()) {
116-
arrayObject = getValueFromObjectOrMap(instance, arrayExpression.getPropertyName());
117-
}
118-
if (arrayObject == null) {
119-
return null;
116+
arrayObject = getValueFromObjectOrMap(instance, arrayExpression.getName());
120117
}
121-
Class<?> clazz = arrayObject.getClass();
122-
if (clazz.isArray()) {
123-
return ((Object[]) arrayObject)[arrayExpression.getIndex()];
124-
} else if (List.class.isAssignableFrom(clazz)) {
125-
return ((List) arrayObject).get(arrayExpression.getIndex());
126-
} else if (Set.class.isAssignableFrom(clazz)) {
127-
ArrayList arrayList = new ArrayList<>((Set) arrayObject);
128-
arrayList.sort(Comparator.comparingInt(Object::hashCode));
129-
return arrayList.get(arrayExpression.getIndex());
130-
} else {
131-
throw new ExpressionException("The instance \"%s\" type is not array or List/Set", instance.toString());
118+
for (int i : arrayExpression.getIndexArray()) {
119+
if (arrayObject == null) {
120+
break;
121+
} else {
122+
Class<?> clazz = arrayObject.getClass();
123+
if (clazz.isArray()) {
124+
arrayObject = ((Object[]) arrayObject)[i];
125+
} else if (List.class.isAssignableFrom(clazz)) {
126+
arrayObject = ((List) arrayObject).get(i);
127+
} else if (Set.class.isAssignableFrom(clazz)) {
128+
ArrayList arrayList = new ArrayList<>((Set) arrayObject);
129+
arrayList.sort(Comparator.comparingInt(Object::hashCode));
130+
arrayObject = arrayList.get(i);
131+
} else {
132+
throw new ExpressionException("The instance \"%s\" type \"%s\" is not array or List/Set", instance.toString(), clazz);
133+
}
134+
}
132135
}
136+
return arrayObject;
133137
}
134138

135139
private static Object getValueFromMethod(Object rootInstance, Object instance, MethodExpression methodExpression) {
@@ -138,9 +142,9 @@ private static Object getValueFromMethod(Object rootInstance, Object instance, M
138142
.map(argumentExpression -> getValue(rootInstance, argumentExpression))
139143
.toArray(Object[]::new);
140144
try {
141-
return MethodUtils.invokeMethod(instance, true, methodExpression.getMethodName(), arguments);
145+
return MethodUtils.invokeMethod(instance, true, methodExpression.getName(), arguments);
142146
} catch (Exception e) {
143-
throw new ExpressionException("%s invoke failed: %s", methodExpression.getMethodName(), e.getMessage());
147+
throw new ExpressionException("%s invoke failed: %s", methodExpression.getName(), e.getMessage());
144148
}
145149
}
146150

0 commit comments

Comments
 (0)