Skip to content

Commit 58278a9

Browse files
authored
Merge pull request #26 from PetrPytelka/symetric_comparator
Refactorized AbstractCompareOperator - is symetric / improved comparison between different types
2 parents 85d32e9 + adb0775 commit 58278a9

16 files changed

+168
-251
lines changed

src/main/java/com/scriptbasic/executors/operators/AbstractCompareOperator.java

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,20 @@
88
public abstract class AbstractCompareOperator extends
99
AbstractBinaryFullCircuitOperator {
1010

11-
protected static int compareJavaObjectTo(final BasicJavaObjectValue f,
12-
final RightValue op) throws BasicRuntimeException {
13-
final var o = BasicJavaObjectValue.asObject(op);
14-
if (f.getValue() instanceof Comparable<?> && o instanceof Comparable<?>) {
15-
@SuppressWarnings("unchecked") final Comparable<Comparable<?>> a = (Comparable<Comparable<?>>) f
16-
.getValue();
17-
final Comparable<?> b = (Comparable<?>) o;
18-
return a.compareTo(b);
19-
}
20-
throw new BasicRuntimeException(
21-
"Can not compare the java objects, at least one of them is not comparable");
22-
}
23-
24-
protected abstract Boolean compareTo(BasicDoubleValue d, RightValue op)
25-
throws BasicRuntimeException;
26-
27-
protected abstract Boolean compareTo(BasicLongValue l, RightValue op)
28-
throws BasicRuntimeException;
11+
/**
12+
* Get final decision from comparison result.
13+
*
14+
* @param comparisonResult
15+
* Result from function compareTo
16+
* @return final decision
17+
*/
18+
abstract protected boolean decide(final int comparisonResult);
2919

30-
protected abstract Boolean compareTo(BasicStringValue s, RightValue op)
31-
throws BasicRuntimeException;
32-
33-
protected abstract Boolean compareTo(BasicJavaObjectValue s, RightValue op)
34-
throws BasicRuntimeException;
35-
36-
protected abstract Boolean compareTo(BasicBooleanValue s, RightValue op)
37-
;
20+
protected <T> Boolean compareTo(Comparable<T> l, T r)
21+
throws BasicRuntimeException {
22+
final int comparisonResult = l.compareTo(r);
23+
return decide(comparisonResult);
24+
}
3825

3926
@Override
4027
protected RightValue evaluateOn(final RightValue leftOperand,
@@ -45,26 +32,43 @@ protected RightValue evaluateOn(final RightValue leftOperand,
4532
if (leftOperand == null || rightOperand == null) {
4633
return BasicValue.FALSE;
4734
}
48-
if (leftOperand.isDouble()) {
49-
return new BasicBooleanValue(compareTo(
50-
((BasicDoubleValue) leftOperand), rightOperand));
35+
if (leftOperand.isLong() || rightOperand.isLong()) {
36+
final Long leftValue = getAsLong(leftOperand);
37+
final Long rightValue = getAsLong(rightOperand);
38+
if (leftValue != null && rightValue != null)
39+
return new BasicBooleanValue(compareTo(leftValue, rightValue));
5140
}
52-
if (leftOperand.isLong()) {
53-
return new BasicBooleanValue(compareTo(
54-
((BasicLongValue) leftOperand), rightOperand));
41+
if (leftOperand.isLong() || rightOperand.isLong() || leftOperand.isDouble() || rightOperand.isDouble()) {
42+
final Double leftValue = getAsDouble(leftOperand);
43+
final Double rightValue = getAsDouble(rightOperand);
44+
if (leftValue != null && rightValue != null)
45+
return new BasicBooleanValue(compareTo(leftValue, rightValue));
5546
}
56-
if (leftOperand.isBoolean()) {
57-
return new BasicBooleanValue(compareTo(
58-
((BasicBooleanValue) leftOperand), rightOperand));
47+
if (leftOperand.isBoolean() && rightOperand.isBoolean()) {
48+
final Boolean leftValue = getAsBoolean(leftOperand);
49+
final Boolean rightValue = getAsBoolean(rightOperand);
50+
if (leftValue != null && rightValue != null)
51+
return new BasicBooleanValue(compareTo(leftValue, rightValue));
5952
}
60-
if (leftOperand.isString()) {
61-
return new BasicBooleanValue(compareTo(
62-
((BasicStringValue) leftOperand), rightOperand));
53+
if (leftOperand.isString() || rightOperand.isString()) {
54+
final String leftValue = getAsString(leftOperand);
55+
final String rightValue = getAsString(rightOperand);
56+
if (leftValue != null && rightValue != null)
57+
return new BasicBooleanValue(compareTo(leftValue, rightValue));
6358
}
64-
if (leftOperand.isJavaObject()) {
65-
return new BasicBooleanValue(compareTo(
66-
((BasicJavaObjectValue) leftOperand), rightOperand));
59+
if (leftOperand.isJavaObject() || rightOperand.isJavaObject()) {
60+
final Object leftValue = getAsObject(leftOperand);
61+
final Object rightValue = getAsObject(rightOperand);
62+
if (leftValue != null && rightValue != null) {
63+
if (leftValue instanceof Comparable<?> && rightValue instanceof Comparable<?>) {
64+
@SuppressWarnings("unchecked")
65+
final Comparable<Comparable<?>> a = (Comparable<Comparable<?>>) leftValue;
66+
final Comparable<?> b = (Comparable<?>) rightValue;
67+
return new BasicBooleanValue(compareTo(a, b));
68+
}
69+
}
6770
}
68-
return null;
71+
throw new BasicRuntimeException("Type mismatch, left operand: " + leftOperand +
72+
", right operand: " + rightOperand);
6973
}
7074
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,88 @@
11
package com.scriptbasic.executors.operators;
22

33
import com.scriptbasic.executors.AbstractExpression;
4+
import com.scriptbasic.executors.rightvalues.BasicBooleanValue;
5+
import com.scriptbasic.executors.rightvalues.BasicDoubleValue;
6+
import com.scriptbasic.executors.rightvalues.BasicJavaObjectValue;
7+
import com.scriptbasic.executors.rightvalues.BasicLongValue;
8+
import com.scriptbasic.executors.rightvalues.BasicStringValue;
9+
import com.scriptbasic.interfaces.BasicRuntimeException;
10+
import com.scriptbasic.spi.RightValue;
411

512
public abstract class AbstractOperator extends AbstractExpression {
613

14+
/**
15+
* Try to convert operator to double
16+
*
17+
* @param op
18+
* operator
19+
* @return Return double or null if cannot be converted
20+
*/
21+
static protected Double getAsDouble(RightValue op) {
22+
try {
23+
return BasicDoubleValue.asDouble(op);
24+
} catch (BasicRuntimeException e) {
25+
return null;
26+
}
27+
}
28+
29+
/**
30+
* Try to convert operator to long
31+
*
32+
* @param op
33+
* operator
34+
* @return Return long or null if cannot be converted
35+
*/
36+
static protected Long getAsLong(RightValue op) {
37+
try {
38+
return BasicLongValue.asLong(op);
39+
} catch (BasicRuntimeException e) {
40+
return null;
41+
}
42+
}
43+
44+
/**
45+
* Try to convert operator to boolean
46+
*
47+
* @param op
48+
* operator
49+
* @return Return boolean or null if cannot be converted
50+
*/
51+
static protected Boolean getAsBoolean(RightValue op) {
52+
try {
53+
return BasicBooleanValue.asBoolean(op);
54+
} catch (BasicRuntimeException e) {
55+
return null;
56+
}
57+
}
58+
59+
/**
60+
* Try to convert operator to string
61+
*
62+
* @param op
63+
* operator
64+
* @return Return string or null if cannot be converted
65+
*/
66+
static protected String getAsString(RightValue op) {
67+
try {
68+
return BasicStringValue.asString(op);
69+
} catch (BasicRuntimeException e) {
70+
return null;
71+
}
72+
}
73+
74+
/**
75+
* Try to convert operator to java object
76+
*
77+
* @param op
78+
* operator
79+
* @return Return object or null if cannot be converted
80+
*/
81+
static protected Object getAsObject(RightValue op) {
82+
try {
83+
return BasicJavaObjectValue.asObject(op);
84+
} catch (BasicRuntimeException e) {
85+
return null;
86+
}
87+
}
788
}
Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,9 @@
11
package com.scriptbasic.executors.operators;
22

3-
import com.scriptbasic.executors.rightvalues.*;
4-
import com.scriptbasic.interfaces.BasicRuntimeException;
5-
import com.scriptbasic.spi.RightValue;
6-
73
public class EqualsOperator extends AbstractCompareOperator {
84

95
@Override
10-
protected Boolean compareTo(final BasicDoubleValue f, final RightValue op)
11-
throws BasicRuntimeException {
12-
return f.getValue().equals(BasicDoubleValue.asDouble(op));
13-
}
14-
15-
@Override
16-
protected Boolean compareTo(final BasicLongValue f, final RightValue op)
17-
throws BasicRuntimeException {
18-
return f.getValue().equals(BasicLongValue.asLong(op));
19-
}
20-
21-
@Override
22-
protected Boolean compareTo(final BasicStringValue f, final RightValue op)
23-
throws BasicRuntimeException {
24-
return f.getValue().equals(BasicStringValue.asString(op));
6+
protected boolean decide(int comparisonResult) {
7+
return comparisonResult == 0;
258
}
26-
27-
@Override
28-
protected Boolean compareTo(final BasicJavaObjectValue f,
29-
final RightValue op) throws BasicRuntimeException {
30-
return compareJavaObjectTo(f, op) == 0;
31-
}
32-
33-
@Override
34-
protected Boolean compareTo(final BasicBooleanValue f, final RightValue op) {
35-
return f.getValue().equals(BasicBooleanValue.asBoolean(op));
36-
}
37-
389
}
Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,9 @@
11
package com.scriptbasic.executors.operators;
22

3-
import com.scriptbasic.executors.rightvalues.*;
4-
import com.scriptbasic.interfaces.BasicRuntimeException;
5-
import com.scriptbasic.spi.RightValue;
6-
73
public class GreaterOrEqualOperator extends AbstractCompareOperator {
84

95
@Override
10-
protected Boolean compareTo(final BasicDoubleValue f, final RightValue op)
11-
throws BasicRuntimeException {
12-
return f.getValue() >= BasicDoubleValue.asDouble(op, "Cannot float compare >= on undef value");
13-
}
14-
15-
@Override
16-
protected Boolean compareTo(final BasicLongValue f, final RightValue op)
17-
throws BasicRuntimeException {
18-
return f.getValue() >= BasicLongValue.asLong(op, "Cannot integer compare >= undef value");
19-
}
20-
21-
@Override
22-
protected Boolean compareTo(final BasicStringValue f, final RightValue op)
23-
throws BasicRuntimeException {
24-
return f.getValue().compareTo(BasicStringValue.asString(op)) >= 0;
6+
protected boolean decide(int comparisonResult) {
7+
return comparisonResult >= 0;
258
}
26-
27-
@Override
28-
protected Boolean compareTo(final BasicJavaObjectValue f,
29-
final RightValue op) throws BasicRuntimeException {
30-
return compareJavaObjectTo(f, op) >= 0;
31-
}
32-
33-
@Override
34-
protected Boolean compareTo(final BasicBooleanValue f, final RightValue op) {
35-
final var a = f.getValue() ? 1 : 0;
36-
final var b = BasicBooleanValue.asBoolean(op) ? 1 : 0;
37-
return a >= b;
38-
}
39-
409
}
Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,9 @@
11
package com.scriptbasic.executors.operators;
22

3-
import com.scriptbasic.executors.rightvalues.*;
4-
import com.scriptbasic.interfaces.BasicRuntimeException;
5-
import com.scriptbasic.spi.RightValue;
6-
73
public class GreaterThanOperator extends AbstractCompareOperator {
84

95
@Override
10-
protected Boolean compareTo(final BasicDoubleValue f, final RightValue op)
11-
throws BasicRuntimeException {
12-
return f.getValue() > BasicDoubleValue.asDouble(op, "Cannot float compare > undef value");
13-
}
14-
15-
@Override
16-
protected Boolean compareTo(final BasicLongValue f, final RightValue op)
17-
throws BasicRuntimeException {
18-
return f.getValue() > BasicLongValue.asLong(op, "Cannot integer compare > undef value");
19-
}
20-
21-
@Override
22-
protected Boolean compareTo(final BasicStringValue f, final RightValue op)
23-
throws BasicRuntimeException {
24-
return f.getValue().compareTo(BasicStringValue.asString(op)) > 0;
6+
protected boolean decide(int comparisonResult) {
7+
return comparisonResult > 0;
258
}
26-
27-
@Override
28-
protected Boolean compareTo(final BasicJavaObjectValue f,
29-
final RightValue op) throws BasicRuntimeException {
30-
return compareJavaObjectTo(f, op) > 0;
31-
}
32-
33-
@Override
34-
protected Boolean compareTo(final BasicBooleanValue f, final RightValue op) {
35-
final var a = f.getValue() ? 1 : 0;
36-
final var b = BasicBooleanValue.asBoolean(op) ? 1 : 0;
37-
return a > b;
38-
}
39-
409
}
Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,9 @@
11
package com.scriptbasic.executors.operators;
22

3-
import com.scriptbasic.executors.rightvalues.*;
4-
import com.scriptbasic.interfaces.BasicRuntimeException;
5-
import com.scriptbasic.spi.RightValue;
6-
73
public class LessOrEqualOperator extends AbstractCompareOperator {
84

95
@Override
10-
protected Boolean compareTo(final BasicDoubleValue f, final RightValue op)
11-
throws BasicRuntimeException {
12-
return f.getValue() <= BasicDoubleValue.asDouble(op, "Cannot float compare <= undef value");
13-
}
14-
15-
@Override
16-
protected Boolean compareTo(final BasicLongValue f, final RightValue op)
17-
throws BasicRuntimeException {
18-
return f.getValue() <= BasicLongValue.asLong(op, "Cannot integer compare <= undef value");
19-
}
20-
21-
@Override
22-
protected Boolean compareTo(final BasicStringValue f, final RightValue op)
23-
throws BasicRuntimeException {
24-
return f.getValue().compareTo(BasicStringValue.asString(op)) <= 0;
6+
protected boolean decide(int comparisonResult) {
7+
return comparisonResult <= 0;
258
}
26-
27-
@Override
28-
protected Boolean compareTo(final BasicJavaObjectValue f,
29-
final RightValue op) throws BasicRuntimeException {
30-
return compareJavaObjectTo(f, op) <= 0;
31-
}
32-
33-
@Override
34-
protected Boolean compareTo(final BasicBooleanValue f, final RightValue op) {
35-
final var a = f.getValue() ? 1 : 0;
36-
final var b = BasicBooleanValue.asBoolean(op) ? 1 : 0;
37-
return a <= b;
38-
}
39-
409
}

0 commit comments

Comments
 (0)