Skip to content

Commit fb3cf79

Browse files
committed
✨ 数字实际类型支持
1 parent b4b3242 commit fb3cf79

File tree

4 files changed

+82
-35
lines changed

4 files changed

+82
-35
lines changed

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

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

44+
public Object Value() {
45+
return null;
46+
}
47+
4448
public String TextValue() {
4549
return null;
4650
}
@@ -66,27 +70,27 @@ public static Operand Create(final boolean obj) {
6670
}
6771

6872
public static Operand Create(final short obj) {
69-
return new OperandNumber((double) obj);
73+
return new OperandLongNumber((long) obj);
7074
}
7175

7276
public static Operand Create(final int obj) {
73-
return new OperandNumber((double) obj);
77+
return new OperandLongNumber((long) obj);
7478
}
7579

7680
public static Operand Create(final long obj) {
77-
return new OperandNumber((double) obj);
81+
return new OperandLongNumber(obj);
7882
}
7983

8084
public static Operand Create(final float obj) {
81-
return new OperandNumber((double) obj);
85+
return new OperandDoubleNumber((double) obj);
8286
}
8387

8488
public static Operand Create(final double obj) {
85-
return new OperandNumber(obj);
89+
return new OperandDoubleNumber(obj);
8690
}
8791

8892
public static Operand Create(final BigDecimal obj) {
89-
return new OperandNumber(obj.doubleValue());
93+
return new OperandDoubleNumber(obj.doubleValue());
9094
}
9195

9296
public static Operand Create(final String obj) {
@@ -136,7 +140,7 @@ public static Operand CreateNull() {
136140
}
137141

138142
public Operand ToNumber(final String errorMessage) {
139-
if (Type() == OperandType.NUMBER) {
143+
if (Type().isNumber()) {
140144
return this;
141145
}
142146
if (IsError()) {
@@ -165,7 +169,7 @@ public Operand ToBoolean(final String errorMessage) {
165169
if (IsError()) {
166170
return this;
167171
}
168-
if (Type() == OperandType.NUMBER) {
172+
if (Type().isNumber()) {
169173
return (NumberValue() != 0) ? True : False;
170174
}
171175
if (Type() == OperandType.DATE) {
@@ -195,7 +199,7 @@ public Operand ToText(final String errorMessage) {
195199
if (IsError()) {
196200
return this;
197201
}
198-
if (Type() == OperandType.NUMBER) {
202+
if (Type().isNumber()) {
199203
String str = ((Double) NumberValue()).toString();
200204
if (str.contains(".")) {
201205
str = Pattern.compile("(\\.)?0+$").matcher(str).replaceAll("");
@@ -219,7 +223,7 @@ public Operand ToDate(final String errorMessage) {
219223
if (IsError()) {
220224
return this;
221225
}
222-
if (Type() == OperandType.NUMBER) {
226+
if (Type().isNumber()) {
223227
return Create(new MyDate(NumberValue()));
224228
}
225229
if (Type() == OperandType.TEXT) {
@@ -393,27 +397,53 @@ public boolean IsNull() {
393397

394398
}
395399

396-
static class OperandNumber extends OperandT<Double> {
400+
static class OperandDoubleNumber extends OperandT<Double> {
397401

398-
public OperandNumber(Double obj) {
402+
public OperandDoubleNumber(Double obj) {
399403
super(obj);
400404
}
401405

402406
@Override
403407
public OperandType Type() {
404-
return OperandType.NUMBER;
408+
return OperandType.DOUBLE_NUMBER;
409+
}
410+
411+
@Override
412+
public double NumberValue() {
413+
return Value;
405414
}
406415

407416
@Override
408417
public int IntValue() {
409-
return (int) (double) Value;
418+
return Value.intValue();
419+
}
420+
}
421+
422+
static class OperandLongNumber extends OperandT<Long> {
423+
424+
public OperandLongNumber(Long obj) {
425+
super(obj);
426+
}
427+
428+
@Override
429+
public OperandType Type() {
430+
return OperandType.LONG_NUMBER;
431+
}
432+
433+
@Override
434+
public Object Value() {
435+
return Value.longValue();
410436
}
411437

412438
@Override
413439
public double NumberValue() {
414440
return Value;
415441
}
416442

443+
@Override
444+
public int IntValue() {
445+
return Value.intValue();
446+
}
417447
}
418448

419449
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)