Skip to content

Commit b8a7355

Browse files
authored
Merge pull request #20 from ZhangBohan/master
✨ 数字实际类型支持
2 parents b4b3242 + b8f964e commit b8a7355

File tree

4 files changed

+106
-35
lines changed

4 files changed

+106
-35
lines changed

java/toolgood.algorithm/src/main/java/toolgood/algorithm/Operand.java

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public int IntValue() {
4141
return 0;
4242
}
4343

44+
public long LongValue() {
45+
return 0;
46+
}
47+
48+
public Object Value() {
49+
return null;
50+
}
51+
4452
public String TextValue() {
4553
return null;
4654
}
@@ -66,27 +74,27 @@ public static Operand Create(final boolean obj) {
6674
}
6775

6876
public static Operand Create(final short obj) {
69-
return new OperandNumber((double) obj);
77+
return new OperandLongNumber((long) obj);
7078
}
7179

7280
public static Operand Create(final int obj) {
73-
return new OperandNumber((double) obj);
81+
return new OperandLongNumber((long) obj);
7482
}
7583

7684
public static Operand Create(final long obj) {
77-
return new OperandNumber((double) obj);
85+
return new OperandLongNumber(obj);
7886
}
7987

8088
public static Operand Create(final float obj) {
81-
return new OperandNumber((double) obj);
89+
return new OperandDoubleNumber((double) obj);
8290
}
8391

8492
public static Operand Create(final double obj) {
85-
return new OperandNumber(obj);
93+
return new OperandDoubleNumber(obj);
8694
}
8795

8896
public static Operand Create(final BigDecimal obj) {
89-
return new OperandNumber(obj.doubleValue());
97+
return new OperandDoubleNumber(obj.doubleValue());
9098
}
9199

92100
public static Operand Create(final String obj) {
@@ -136,7 +144,7 @@ public static Operand CreateNull() {
136144
}
137145

138146
public Operand ToNumber(final String errorMessage) {
139-
if (Type() == OperandType.NUMBER) {
147+
if (Type().isNumber()) {
140148
return this;
141149
}
142150
if (IsError()) {
@@ -165,7 +173,7 @@ public Operand ToBoolean(final String errorMessage) {
165173
if (IsError()) {
166174
return this;
167175
}
168-
if (Type() == OperandType.NUMBER) {
176+
if (Type().isNumber()) {
169177
return (NumberValue() != 0) ? True : False;
170178
}
171179
if (Type() == OperandType.DATE) {
@@ -195,7 +203,7 @@ public Operand ToText(final String errorMessage) {
195203
if (IsError()) {
196204
return this;
197205
}
198-
if (Type() == OperandType.NUMBER) {
206+
if (Type().isNumber()) {
199207
String str = ((Double) NumberValue()).toString();
200208
if (str.contains(".")) {
201209
str = Pattern.compile("(\\.)?0+$").matcher(str).replaceAll("");
@@ -219,7 +227,7 @@ public Operand ToDate(final String errorMessage) {
219227
if (IsError()) {
220228
return this;
221229
}
222-
if (Type() == OperandType.NUMBER) {
230+
if (Type().isNumber()) {
223231
return Create(new MyDate(NumberValue()));
224232
}
225233
if (Type() == OperandType.TEXT) {
@@ -289,6 +297,11 @@ static abstract class OperandT<T> extends Operand {
289297
public OperandT(final T obj) {
290298
Value = obj;
291299
}
300+
301+
@Override
302+
public Object Value() {
303+
return Value;
304+
}
292305
}
293306

294307
static class OperandArray extends OperandT<java.util.List<Operand>> {
@@ -393,27 +406,68 @@ public boolean IsNull() {
393406

394407
}
395408

396-
static class OperandNumber extends OperandT<Double> {
409+
static class OperandDoubleNumber extends OperandT<Double> {
397410

398-
public OperandNumber(Double obj) {
411+
public OperandDoubleNumber(Double obj) {
399412
super(obj);
400413
}
401414

402415
@Override
403416
public OperandType Type() {
404-
return OperandType.NUMBER;
417+
return OperandType.DOUBLE_NUMBER;
418+
}
419+
420+
@Override
421+
public double NumberValue() {
422+
return Value;
423+
}
424+
425+
@Override
426+
public Object Value() {
427+
return Value;
405428
}
406429

407430
@Override
408431
public int IntValue() {
409-
return (int) (double) Value;
432+
return Value.intValue();
433+
}
434+
435+
@Override
436+
public long LongValue() {
437+
return Value.longValue();
438+
}
439+
}
440+
441+
static class OperandLongNumber extends OperandT<Long> {
442+
443+
public OperandLongNumber(Long obj) {
444+
super(obj);
445+
}
446+
447+
@Override
448+
public OperandType Type() {
449+
return OperandType.LONG_NUMBER;
450+
}
451+
452+
@Override
453+
public Object Value() {
454+
return Value.longValue();
410455
}
411456

412457
@Override
413458
public double NumberValue() {
414459
return Value;
415460
}
416461

462+
@Override
463+
public int IntValue() {
464+
return Value.intValue();
465+
}
466+
467+
@Override
468+
public long LongValue() {
469+
return Value;
470+
}
417471
}
418472

419473
static class OperandString extends OperandT<String> {
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package toolgood.algorithm;
22

33
public enum OperandType {
4-
NULL,
5-
ERROR,
6-
DATE,
7-
ARRARY,
8-
NUMBER,
9-
BOOLEAN,
10-
TEXT,
11-
JSON
4+
NULL,
5+
ERROR,
6+
DATE,
7+
ARRARY,
8+
BOOLEAN,
9+
TEXT,
10+
DOUBLE_NUMBER,
11+
LONG_NUMBER,
12+
JSON;
13+
14+
public boolean isNumber() {
15+
return this == DOUBLE_NUMBER || this == LONG_NUMBER;
16+
}
1217
}

java/toolgood.algorithm/src/main/java/toolgood/algorithm/internals/MathVisitor.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ public Operand visitJudge_fun(final Judge_funContext context) {
317317
}
318318
} else if ((firstValue.Type() == OperandType.DATE && secondValue.Type() == OperandType.TEXT)
319319
|| (secondValue.Type() == OperandType.DATE && firstValue.Type() == OperandType.TEXT)
320-
|| (firstValue.Type() == OperandType.NUMBER && secondValue.Type() == OperandType.TEXT)
321-
|| (secondValue.Type() == OperandType.NUMBER && firstValue.Type() == OperandType.TEXT)) {
320+
|| (firstValue.Type().isNumber() && secondValue.Type() == OperandType.TEXT)
321+
|| (secondValue.Type().isNumber() && firstValue.Type() == OperandType.TEXT)) {
322322
firstValue = firstValue.ToText("Function '" + type + "' parameter 1 is error!");
323323
if (firstValue.IsError()) {
324324
return firstValue;
@@ -443,7 +443,7 @@ public Operand visitISNUMBER_fun(final ISNUMBER_funContext context) {
443443
return firstValue;
444444
}
445445

446-
if (firstValue.Type() == OperandType.NUMBER) {
446+
if (firstValue.Type().isNumber()) {
447447
return Operand.True;
448448
}
449449
return Operand.False;
@@ -520,7 +520,7 @@ public Operand visitISNULLORERROR_fun(final ISNULLORERROR_funContext context) {
520520

521521
public Operand visitISEVEN_fun(final ISEVEN_funContext context) {
522522
final Operand firstValue = context.expr().accept(this);
523-
if (firstValue.Type() == OperandType.NUMBER) {
523+
if (firstValue.Type().isNumber()) {
524524
if (firstValue.IntValue() % 2 == 0) {
525525
return Operand.True;
526526
}
@@ -538,7 +538,7 @@ public Operand visitISLOGICAL_fun(final ISLOGICAL_funContext context) {
538538

539539
public Operand visitISODD_fun(final ISODD_funContext context) {
540540
final Operand firstValue = context.expr().accept(this);
541-
if (firstValue.Type() == OperandType.NUMBER) {
541+
if (firstValue.Type().isNumber()) {
542542
if (firstValue.IntValue() % 2 == 1) {
543543
return Operand.True;
544544
}
@@ -2213,7 +2213,7 @@ public Operand visitTEXT_fun(final TEXT_funContext context) {
22132213
return firstValue;
22142214
} else if (firstValue.Type() == OperandType.BOOLEAN) {
22152215
return Operand.Create(firstValue.BooleanValue() ? "TRUE" : "FALSE");
2216-
} else if (firstValue.Type() == OperandType.NUMBER) {
2216+
} else if (firstValue.Type().isNumber()) {
22172217
final DecimalFormat myFormatter = new DecimalFormat(secondValue.TextValue());
22182218
return Operand.Create(myFormatter.format(firstValue.NumberValue()));
22192219
} else if (firstValue.Type() == OperandType.DATE) {
@@ -3178,7 +3178,7 @@ public Operand visitAVERAGEIF_fun(final AVERAGEIF_funContext context) {
31783178

31793179
double sum;
31803180
int count;
3181-
if (args.get(1).Type() == OperandType.NUMBER) {
3181+
if (args.get(1).Type().isNumber()) {
31823182
count = F_base_countif(list, args.get(1).NumberValue());
31833183
sum = count * args.get(1).NumberValue();
31843184
} else {
@@ -3303,7 +3303,7 @@ public Operand visitCOUNTIF_fun(final COUNTIF_funContext context) {
33033303
}
33043304

33053305
int count;
3306-
if (args.get(1).Type() == OperandType.NUMBER) {
3306+
if (args.get(1).Type().isNumber()) {
33073307
count = F_base_countif(list, args.get(1).NumberValue());
33083308
} else {
33093309
try {
@@ -3383,7 +3383,7 @@ public Operand visitSUMIF_fun(final SUMIF_funContext context) {
33833383
}
33843384

33853385
double sum;
3386-
if (args.get(1).Type() == OperandType.NUMBER) {
3386+
if (args.get(1).Type().isNumber()) {
33873387
sum = F_base_countif(list, args.get(1).NumberValue()) * args.get(1).NumberValue();
33883388
} else {
33893389
if (Pattern.compile("^-?(\\d+)(\\.\\d+)?$").matcher(args.get(1).TextValue().trim()).find()) {
@@ -4115,7 +4115,7 @@ private boolean F_base_compare(final double a, final double b, final String ss)
41154115

41164116
private boolean F_base_GetList_1(final List<Operand> args, final List<Double> list) {
41174117
for (final Operand item : args) {
4118-
if (item.Type() == OperandType.NUMBER) {
4118+
if (item.Type().isNumber()) {
41194119
list.add(item.NumberValue());
41204120
} else if (item.Type() == OperandType.ARRARY) {
41214121
final boolean o = F_base_GetList_1(item.ArrayValue(), list);
@@ -4146,7 +4146,7 @@ private boolean F_base_GetList_2(final Operand args, final List<Double> list) {
41464146
if (args.IsError()) {
41474147
return false;
41484148
}
4149-
if (args.Type() == OperandType.NUMBER) {
4149+
if (args.Type().isNumber()) {
41504150
list.add(args.NumberValue());
41514151
} else if (args.Type() == OperandType.ARRARY) {
41524152
final boolean o = F_base_GetList_1(args.ArrayValue(), list);
@@ -5107,7 +5107,7 @@ public Operand visitVLOOKUP_fun(final VLOOKUP_funContext context) {
51075107
if (o.ArrayValue().size() > 0) {
51085108
final Operand o1 = o.ArrayValue().get(0);// [0];
51095109
int b = -1;
5110-
if (secondValue.Type() == OperandType.NUMBER) {
5110+
if (secondValue.Type().isNumber()) {
51115111
final Operand o2 = o1.ToNumber(null);
51125112
if (o2.IsError() == false) {
51135113
b = Compare(o2.NumberValue(), secondValue.NumberValue());
@@ -5140,7 +5140,7 @@ public Operand visitVLOOKUP_fun(final VLOOKUP_funContext context) {
51405140
if (o.ArrayValue().size() > 0) {
51415141
final Operand o1 = o.ArrayValue().get(0);// [0];
51425142
int b = -1;
5143-
if (secondValue.Type() == OperandType.NUMBER) {
5143+
if (secondValue.Type().isNumber()) {
51445144
final Operand o2 = o1.ToNumber(null);
51455145
if (o2.IsError() == false) {
51465146
b = Compare(o2.NumberValue(), secondValue.NumberValue());

java/toolgood.algorithm/src/test/java/toolgood/algorithm/Tests/AlgorithmEngineTest_dateTime.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
import toolgood.algorithm.AlgorithmEngine;
1010
import toolgood.algorithm.MyDate;
11+
import toolgood.algorithm.Operand;
12+
import toolgood.algorithm.OperandType;
13+
1114

1215
public class AlgorithmEngineTest_dateTime {
1316

@@ -211,4 +214,13 @@ public void WEEKNUM_test()
211214
assertEquals(dt, 1);
212215
}
213216

217+
@Test
218+
public void Year_test_withType() throws Exception {
219+
AlgorithmEngine engine = new AlgorithmEngine();
220+
engine.Parse("year(now())");
221+
Operand operand = engine.Evaluate();
222+
assertEquals(OperandType.LONG_NUMBER, operand.Type());
223+
assertEquals(Long.valueOf(DateTime.now().getYear()), operand.Value());
224+
}
225+
214226
}

0 commit comments

Comments
 (0)