1515 */
1616public abstract class Expression {
1717
18- public static final EmptyExpression EMPTY_EXPRESSION = new EmptyExpression ();
19-
2018 /* 父表达式对象 */
2119 @ Getter
22- protected Expression parentExpression = EMPTY_EXPRESSION ;
20+ protected Expression parentExpression = EmptyExpression . INSTANCE ;
2321
2422 public void setParentExpression (Expression parentExpression ) {
2523 if (parentExpression == null ) {
@@ -34,7 +32,7 @@ public void setParentExpression(Expression parentExpression) {
3432 * @return 判断结果
3533 */
3634 public boolean hasParentExpression () {
37- return parentExpression != EMPTY_EXPRESSION ;
35+ return parentExpression != EmptyExpression . INSTANCE ;
3836 }
3937
4038 /**
@@ -45,7 +43,7 @@ public boolean hasParentExpression() {
4543 public Expression [] expressionTree () {
4644 List <Expression > expressionTree = new LinkedList <>();
4745 Expression tempExpression = this ;
48- while (tempExpression != Expression . EMPTY_EXPRESSION ) {
46+ while (tempExpression != EmptyExpression . INSTANCE ) {
4947 expressionTree .add (tempExpression );
5048 tempExpression = tempExpression .parentExpression ;
5149 }
@@ -59,10 +57,10 @@ public Expression[] expressionTree() {
5957 * @param expressionValue 表达式字符串
6058 * @return 表达式对象
6159 */
62- public static final Expression parse (String expressionValue ) {
63- if (StringUtils .isNotBlank (expressionValue )) {
60+ public static Expression parse (String expressionValue ) {
61+ if (StringUtils .isNotEmpty (expressionValue )) {
6462 if (expressionValue .contains ("." )) {
65- String [] expressionFragments = expressionValue . split (" \\ ." );
63+ String [] expressionFragments = split (expressionValue );
6664 Expression rootExpression = parseSingle (expressionFragments [0 ]);
6765 for (int i = 1 ; i < expressionFragments .length ; i ++) {
6866 Expression childExpression = parseSingle (expressionFragments [i ]);
@@ -74,7 +72,48 @@ public static final Expression parse(String expressionValue) {
7472 return parseSingle (expressionValue );
7573 }
7674 }
77- return EMPTY_EXPRESSION ;
75+ return EmptyExpression .INSTANCE ;
76+ }
77+
78+ /**
79+ * 切分表达式
80+ *
81+ * @param expressionValue 表达式字符串
82+ * @return 切分结果
83+ */
84+ public static String [] split (String expressionValue ) {
85+ if (!expressionValue .contains ("(" )) {
86+ return expressionValue .split ("\\ ." );
87+ }
88+ List <String > parts = new LinkedList <>();
89+ StringBuilder sb = new StringBuilder ();
90+ int inBracketLevel = 0 ;
91+ for (int i = 0 ; i < expressionValue .length (); i ++) {
92+ final char ch = expressionValue .charAt (i );
93+ switch (ch ) {
94+ case '(' :
95+ inBracketLevel ++;
96+ break ;
97+ case ')' :
98+ inBracketLevel --;
99+ break ;
100+ case '.' : {
101+ if (inBracketLevel == 0 ) {
102+ parts .add (sb .toString ());
103+ sb .setLength (0 );
104+ inBracketLevel = 0 ;
105+ } else {
106+ sb .append (ch );
107+ }
108+ }
109+ continue ;
110+ }
111+ sb .append (ch );
112+ }
113+ if (sb .length () > 0 ) {
114+ parts .add (sb .toString ());
115+ }
116+ return parts .toArray (String []::new );
78117 }
79118
80119 /**
@@ -84,10 +123,12 @@ public static final Expression parse(String expressionValue) {
84123 * @return 单项表达式对象
85124 */
86125 private static Expression parseSingle (String singleExpressionValue ) {
87- if (StringUtils .isBlank (singleExpressionValue )) {
88- return EMPTY_EXPRESSION ;
126+ if (StringUtils .isEmpty (singleExpressionValue )) {
127+ return EmptyExpression . INSTANCE ;
89128 } else if (ArrayExpression .isArrayExpression (singleExpressionValue )) {
90129 return new ArrayExpression (singleExpressionValue );
130+ } else if (MethodExpression .isMethodExpression (singleExpressionValue )) {
131+ return new MethodExpression (singleExpressionValue );
91132 } else {
92133 return new ObjectExpression (singleExpressionValue );
93134 }
@@ -98,20 +139,20 @@ private static Expression parseSingle(String singleExpressionValue) {
98139 * @param expression 表达式对象
99140 * @return 新的表达式对象
100141 */
101- public static final Expression copy (Expression expression ) {
142+ public static Expression copy (Expression expression ) {
102143 Expression newExpression ;
103144 if (expression instanceof ObjectExpression ) {
104145 newExpression = new ObjectExpression (((ObjectExpression ) expression ).getPropertyName ());
105146 } else if (expression instanceof ArrayExpression ){
106147 ArrayExpression arrayExpression = (ArrayExpression ) expression ;
107148 newExpression = new ArrayExpression (arrayExpression .getPropertyName (), arrayExpression .getIndex ());
108149 } else {
109- newExpression = EMPTY_EXPRESSION ;
150+ newExpression = EmptyExpression . INSTANCE ;
110151 }
111- if (expression .getParentExpression () != EMPTY_EXPRESSION ) {
152+ if (expression .getParentExpression () != EmptyExpression . INSTANCE ) {
112153 newExpression .setParentExpression (copy (expression .getParentExpression ()));
113154 } else {
114- newExpression .setParentExpression (EMPTY_EXPRESSION );
155+ newExpression .setParentExpression (EmptyExpression . INSTANCE );
115156 }
116157 return newExpression ;
117158 }
@@ -123,7 +164,7 @@ public static final Expression copy(Expression expression) {
123164 * @param childExpressionValue 子表达式字符串
124165 * @return 新的表达式对象
125166 */
126- public static final Expression concat (Expression parentExpression , String childExpressionValue ) {
167+ public static Expression concat (Expression parentExpression , String childExpressionValue ) {
127168 Expression childExpression = parse (childExpressionValue );
128169 return concat (parentExpression , childExpression );
129170 }
@@ -134,15 +175,15 @@ public static final Expression concat(Expression parentExpression, String childE
134175 * @param childExpression 子表达式对象
135176 * @return 新的表达式对象
136177 */
137- public static final Expression concat (Expression parentExpression , Expression childExpression ) {
138- if (childExpression == EMPTY_EXPRESSION ) {
178+ public static Expression concat (Expression parentExpression , Expression childExpression ) {
179+ if (childExpression == EmptyExpression . INSTANCE ) {
139180 return parentExpression ;
140181 }
141- if (parentExpression == EMPTY_EXPRESSION ) {
182+ if (parentExpression == EmptyExpression . INSTANCE ) {
142183 return childExpression ;
143184 } else {
144185 Expression newExpression = copy (childExpression );
145- if (newExpression .getParentExpression () == EMPTY_EXPRESSION ) {
186+ if (newExpression .getParentExpression () == EmptyExpression . INSTANCE ) {
146187 newExpression .setParentExpression (parentExpression );
147188 } else {
148189 newExpression .getParentExpression ().setParentExpression (parentExpression );
0 commit comments