Skip to content

Commit 69f6861

Browse files
committed
优化代码
1 parent b45e3e8 commit 69f6861

18 files changed

+407
-548
lines changed

flinkx-restapi/flinkx-restapi-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
<artifactId>httpclient</artifactId>
1717
<version>${http.version}</version>
1818
</dependency>
19+
20+
<dependency>
21+
<groupId>com.github.pfmiles</groupId>
22+
<artifactId>dropincc.java</artifactId>
23+
<version>0.2.2</version>
24+
</dependency>
1925
</dependencies>
2026
</project>

flinkx-restapi/flinkx-restapi-core/src/main/java/com/dtstack/flinkx/restapi/common/ConstantParam.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ public class ConstantParam<T> implements ParamDefinition {
4343

4444
private final String description;
4545

46-
private String formatDescription;
46+
private String formatDescription;
4747

4848
private final DateTimeFormatter format;
4949

5050
public ConstantParam(String name, ParamType paramType, Class valueClass, Object value, String description, String format) {
5151
this.name = name;
5252
this.paramType = paramType;
5353
this.valueClass = valueClass;
54-
this.value = objectConvent(valueClass, value);
5554
this.description = description;
56-
this.formatDescription=format;
55+
this.value = value;
56+
this.formatDescription = format;
5757
if (StringUtils.isNotBlank(format)) {
5858
this.format = DateTimeFormatter.ofPattern(format);
5959
} else {
@@ -78,8 +78,8 @@ public Object getValue() {
7878
}
7979

8080
@Override
81-
public Class getValueType() {
82-
return valueClass;
81+
public String getValueType() {
82+
return "valueClass";
8383
}
8484

8585

@@ -108,11 +108,5 @@ public Object format(Object data) {
108108
return null;
109109
}
110110

111-
@Override
112-
public <T> T objectConvent(Class<T> tClass, Object data) {
113-
return (T)data;
114-
}
115-
116-
117111

118112
}

flinkx-restapi/flinkx-restapi-core/src/main/java/com/dtstack/flinkx/restapi/common/ConstantVarible.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
package com.dtstack.flinkx.restapi.common;
22

3-
public class ConstantVarible implements Paramitem {
4-
private final Object object;
3+
public class ConstantVarible<T> implements Paramitem {
4+
private final T object;
5+
private final String name;
56
// private ParamDefinition paramDefinition;
67

7-
public ConstantVarible(Object object ) {
8+
public ConstantVarible(T object, String name) {
89
this.object = object;
10+
this.name = name;
911
// this.paramDefinition = paramDefinition;
1012
}
1113

1214
@Override
13-
public Object getValue() {
15+
public T getValue(RestContext restContext) {
1416
return object;
1517
}
1618

19+
@Override
20+
public String getName() {
21+
return name;
22+
}
23+
1724
// @Override
1825
// public ParamDefinition getParamDefinition() {
1926
// return paramDefinition;

flinkx-restapi/flinkx-restapi-core/src/main/java/com/dtstack/flinkx/restapi/common/CurrentTimeVarible.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,27 @@
1717
*/
1818
package com.dtstack.flinkx.restapi.common;
1919

20+
import java.util.Date;
21+
2022
/**
2123
* CurrentTimeVarible
2224
*
2325
* @author by [email protected]
2426
* @Date 2020/9/28
2527
*/
26-
public class CurrentTimeVarible implements Paramitem {
28+
public class CurrentTimeVarible implements Paramitem<Date> {
29+
30+
public CurrentTimeVarible() {
31+
32+
}
33+
34+
@Override
35+
public Date getValue(RestContext restContext) {
36+
return new Date();
37+
}
2738

2839
@Override
29-
public Object getValue() {
30-
return System.currentTimeMillis();
40+
public String getName() {
41+
return "currenttime";
3142
}
3243
}

flinkx-restapi/flinkx-restapi-core/src/main/java/com/dtstack/flinkx/restapi/common/DymaticParam.java

Lines changed: 159 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,56 @@
1717
*/
1818
package com.dtstack.flinkx.restapi.common;
1919

20-
import java.util.List;
21-
import java.util.Objects;
20+
import com.github.pfmiles.dropincc.*;
21+
import org.apache.commons.collections.CollectionUtils;
22+
import org.apache.commons.lang3.StringUtils;
23+
import org.apache.commons.lang3.math.NumberUtils;
24+
25+
import java.math.BigDecimal;
26+
import java.sql.Timestamp;
27+
import java.util.*;
28+
import java.util.concurrent.atomic.AtomicReference;
2229

2330
/**
2431
* DymaticParam
2532
*
2633
* @author by [email protected]
2734
* @Date 2020/9/26
2835
*/
29-
public class DymaticParam extends ConstantParam implements ParamDefinitionNextAble ,lifecycle {
36+
public class DymaticParam extends ConstantParam implements ParamDefinitionNextAble {
3037

3138
private RestContext restContext;
32-
private ParamItemContext valueDymaticParam;
33-
private ParamItemContext nextvalueDymaticParam;
3439

35-
public DymaticParam(String name, ParamType paramType, Class valueClass, String description, String format, RestContext context) {
40+
private final List<Paramitem> dymaticNowString;
41+
private final List<Paramitem> dymaticNextString;
42+
43+
44+
private final String initValueExpression;
45+
private final String nextValueExpression;
46+
47+
private Exe exe = getExe();
48+
49+
50+
public DymaticParam(String name, ParamType paramType, Class valueClass, String nowValue, String nextValue, String description, String format, RestContext context) {
3651
super(name, paramType, valueClass, null, description, format);
52+
// nowvalue一定是存在的
53+
initValueExpression = nowValue;
54+
55+
dymaticNowString = ParamFactory.getVarible(nowValue);
56+
57+
if (StringUtils.isBlank(nextValue)) {
58+
dymaticNextString = dymaticNowString;
59+
nextValueExpression = initValueExpression;
60+
} else {
61+
dymaticNextString = ParamFactory.getVarible(nextValue);
62+
nextValueExpression = nextValue;
63+
}
3764
this.restContext = context;
3865
}
3966

4067
@Override
4168
public Object getValue() {
42-
Object data;
43-
if (restContext.getTime() > 0 && Objects.nonNull(nextvalueDymaticParam)) {
44-
data = nextvalueDymaticParam.getValue();
45-
} else {
46-
data = nextvalueDymaticParam.getValue();
47-
}
48-
data = format(data);
49-
return objectConvent(getValueType(), data);
69+
return getDymaticValue(dymaticNowString, initValueExpression);
5070
}
5171

5272
/**
@@ -59,8 +79,7 @@ public Object getValue() {
5979
*/
6080
@Override
6181
public Object getNextValue() {
62-
// restContext.calcute();
63-
return getValue();
82+
return getDymaticValue(dymaticNextString, nextValueExpression);
6483
}
6584

6685
//判断是否是运算符
@@ -103,20 +122,134 @@ public int operationLv(char operation) {//给运算符设置优先级
103122

104123
@Override
105124
public void init() {
106-
if(Objects.nonNull(valueDymaticParam)){
107-
valueDymaticParam.init();
108-
}
109125

110-
if(Objects.nonNull(nextvalueDymaticParam)){
111-
nextvalueDymaticParam.init();
112-
}
113126
}
114127

115-
public void setValueDymaticParam(ParamItemContext valueDymaticParam) {
116-
this.valueDymaticParam = valueDymaticParam;
128+
public Exe getExe() {
129+
Lang c = new Lang("Calculator");
130+
Grule expr = c.newGrule();
131+
c.defineGrule(expr, CC.EOF).action(new Action() {
132+
public BigDecimal act(Object matched) {
133+
return new BigDecimal(((Object[]) matched)[0].toString());
134+
}
135+
});
136+
TokenDef a = c.newToken("\\+");
137+
Grule addend = c.newGrule();
138+
expr.define(addend, CC.ks(a.or("\\-"), addend)).action(new Action() {
139+
public BigDecimal act(Object matched) {
140+
Object[] ms = (Object[]) matched;
141+
BigDecimal a0 = (BigDecimal) ms[0];
142+
Object[] aPairs = (Object[]) ms[1];
143+
for (Object p : aPairs) {
144+
String op = (String) ((Object[]) p)[0];
145+
BigDecimal a = (BigDecimal) ((Object[]) p)[1];
146+
if ("+".equals(op)) {
147+
a0 = a.add(a0);
148+
} else {
149+
a0 = a0.subtract(a);
150+
}
151+
}
152+
return a0;
153+
}
154+
});
155+
TokenDef m = c.newToken("\\*");
156+
Grule factor = c.newGrule();
157+
addend.define(factor, CC.ks(m.or("/"), factor)).action(new Action() {
158+
public BigDecimal act(Object matched) {
159+
Object[] ms = (Object[]) matched;
160+
BigDecimal f0 = (BigDecimal) ms[0];
161+
Object[] fPairs = (Object[]) ms[1];
162+
for (Object p : fPairs) {
163+
String op = (String) ((Object[]) p)[0];
164+
BigDecimal f = (BigDecimal) ((Object[]) p)[1];
165+
if ("*".equals(op)) {
166+
f0 = f0.multiply(f);
167+
} else {
168+
f0 = f0.divide(f, BigDecimal.ROUND_HALF_UP, 3);
169+
}
170+
}
171+
return f0;
172+
}
173+
});
174+
factor.define("\\(", expr, "\\)").action(new Action() {
175+
public BigDecimal act(Object matched) {
176+
return (BigDecimal) ((Object[]) matched)[1];
177+
}
178+
}).alt("\\-\\d+(\\.\\d+)?|\\d+(\\.\\d+)?").action(new Action() {
179+
public BigDecimal act(Object matched) {
180+
return new BigDecimal(matched.toString());
181+
}
182+
});
183+
Exe exe = c.compile();
184+
return exe;
117185
}
118186

119-
public void setNextvalueDymaticParam(ParamItemContext nextvalueDymaticParam) {
120-
this.nextvalueDymaticParam = nextvalueDymaticParam;
187+
private Object getDymaticValue(List<Paramitem> dymaticString, String expression) {
188+
if (CollectionUtils.isEmpty(dymaticString)) {
189+
return expression;
190+
}
191+
Map<String, Object> tempReplaceValue = new HashMap<>(16);
192+
AtomicReference<String> tempExpression = new AtomicReference<>(expression);
193+
Object tempValue;
194+
dymaticString.forEach(k -> {
195+
Object value = k.getValue(restContext);
196+
tempReplaceValue.put(escapeExprSpecialWord("${" + k.getName() + "}"), value);
197+
});
198+
tempReplaceValue.forEach((k, v) -> {
199+
if(Objects.isNull(v)){
200+
tempExpression.set(tempExpression.get().replaceFirst(k, 0+""));
201+
}else if (NumberUtils.isNumber(v.toString())) {
202+
tempExpression.set(tempExpression.get().replaceFirst(k, v.toString()));
203+
} else if (v instanceof Date) {
204+
tempExpression.set(tempExpression.get().replaceFirst(k, ((Date) v).getTime() + ""));
205+
}
206+
if (v instanceof java.sql.Date) {
207+
tempExpression.set(tempExpression.get().replaceFirst(k, ((java.sql.Date) v).getTime() + ""));
208+
}
209+
if (v instanceof Timestamp) {
210+
tempExpression.set(tempExpression.get().replaceFirst(k, ((Timestamp) v).getTime() + ""));
211+
}
212+
});
213+
214+
215+
try {
216+
tempValue = exe.eval(tempExpression.get());
217+
} catch (Exception e) {
218+
tempReplaceValue.forEach((k, v) -> {
219+
if(Objects.isNull(v)){
220+
tempExpression.set(tempExpression.get().replaceFirst(k, ""));
221+
}else if (NumberUtils.isNumber(v.toString())) {
222+
tempExpression.set(tempExpression.get().replaceFirst(k, v.toString()));
223+
} else if (v instanceof Date) {
224+
tempExpression.set(tempExpression.get().replaceFirst(k, ((Date) v).toString() + ""));
225+
}
226+
if (v instanceof java.sql.Date) {
227+
tempExpression.set(tempExpression.get().replaceFirst(k, ((java.sql.Date) v).toString() + ""));
228+
}
229+
if (v instanceof Timestamp) {
230+
tempExpression.set(tempExpression.get().replaceFirst(k, ((Timestamp) v).toString() + ""));
231+
}
232+
});
233+
tempValue = initValueExpression;
234+
}
235+
restContext.updateValue(getType().name().toLowerCase(Locale.ENGLISH) + "." + getName(), tempValue);
236+
return tempValue;
237+
}
238+
/**
239+
* 转义正则特殊字符 ($()*+.[]?\^{},|)
240+
*
241+
* @param keyword
242+
* @return
243+
*/
244+
public static String escapeExprSpecialWord(String keyword) {
245+
if (StringUtils.isNotBlank(keyword)) {
246+
String[] fbsArr = { "\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|" };
247+
for (String key : fbsArr) {
248+
if (keyword.contains(key)) {
249+
keyword = keyword.replace(key, "\\" + key);
250+
}
251+
}
252+
}
253+
return keyword;
121254
}
122255
}

flinkx-restapi/flinkx-restapi-core/src/main/java/com/dtstack/flinkx/restapi/common/InnerVaribleFactory.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class InnerVaribleFactory {
3333
static {
3434
InnerVaribles = new HashMap<>(18);
3535
InnerVaribles.put("uuid", new UuidVarible());
36-
InnerVaribles.put("currentTime", new UuidVarible());
3736
InnerVaribles.put("currenttime", new CurrentTimeVarible());
3837

3938
}
@@ -42,7 +41,13 @@ public static Paramitem createInnerVarible(String name) {
4241
return InnerVaribles.get(name);
4342
}
4443

45-
public static boolean isInnerVariable(String name){
44+
public static boolean isInnerVariable(String name) {
4645
return InnerVaribles.containsKey(name);
4746
}
47+
48+
49+
public static void addVarible(String name,Paramitem paramitem) {
50+
InnerVaribles.put(name,paramitem);
51+
}
52+
4853
}

0 commit comments

Comments
 (0)