Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,20 @@
public abstract class AbstractCompareOperator extends
AbstractBinaryFullCircuitOperator {

protected static int compareJavaObjectTo(final BasicJavaObjectValue f,
final RightValue op) throws BasicRuntimeException {
final var o = BasicJavaObjectValue.asObject(op);
if (f.getValue() instanceof Comparable<?> && o instanceof Comparable<?>) {
@SuppressWarnings("unchecked") final Comparable<Comparable<?>> a = (Comparable<Comparable<?>>) f
.getValue();
final Comparable<?> b = (Comparable<?>) o;
return a.compareTo(b);
}
throw new BasicRuntimeException(
"Can not compare the java objects, at least one of them is not comparable");
}

protected abstract Boolean compareTo(BasicDoubleValue d, RightValue op)
throws BasicRuntimeException;

protected abstract Boolean compareTo(BasicLongValue l, RightValue op)
throws BasicRuntimeException;
/**
* Get final decision from comparison result.
*
* @param comparisonResult
* Result from function compareTo
* @return final decision
*/
abstract protected boolean decide(final int comparisonResult);

protected abstract Boolean compareTo(BasicStringValue s, RightValue op)
throws BasicRuntimeException;

protected abstract Boolean compareTo(BasicJavaObjectValue s, RightValue op)
throws BasicRuntimeException;

protected abstract Boolean compareTo(BasicBooleanValue s, RightValue op)
;
protected <T> Boolean compareTo(Comparable<T> l, T r)
throws BasicRuntimeException {
final int comparisonResult = l.compareTo(r);
return decide(comparisonResult);
}

@Override
protected RightValue evaluateOn(final RightValue leftOperand,
Expand All @@ -45,26 +32,43 @@ protected RightValue evaluateOn(final RightValue leftOperand,
if (leftOperand == null || rightOperand == null) {
return BasicValue.FALSE;
}
if (leftOperand.isDouble()) {
return new BasicBooleanValue(compareTo(
((BasicDoubleValue) leftOperand), rightOperand));
if (leftOperand.isLong() || rightOperand.isLong()) {
final Long leftValue = getAsLong(leftOperand);
final Long rightValue = getAsLong(rightOperand);
if (leftValue != null && rightValue != null)
return new BasicBooleanValue(compareTo(leftValue, rightValue));
}
if (leftOperand.isLong()) {
return new BasicBooleanValue(compareTo(
((BasicLongValue) leftOperand), rightOperand));
if (leftOperand.isLong() || rightOperand.isLong() || leftOperand.isDouble() || rightOperand.isDouble()) {
final Double leftValue = getAsDouble(leftOperand);
final Double rightValue = getAsDouble(rightOperand);
if (leftValue != null && rightValue != null)
return new BasicBooleanValue(compareTo(leftValue, rightValue));
}
if (leftOperand.isBoolean()) {
return new BasicBooleanValue(compareTo(
((BasicBooleanValue) leftOperand), rightOperand));
if (leftOperand.isBoolean() && rightOperand.isBoolean()) {
final Boolean leftValue = getAsBoolean(leftOperand);
final Boolean rightValue = getAsBoolean(rightOperand);
if (leftValue != null && rightValue != null)
return new BasicBooleanValue(compareTo(leftValue, rightValue));
}
if (leftOperand.isString()) {
return new BasicBooleanValue(compareTo(
((BasicStringValue) leftOperand), rightOperand));
if (leftOperand.isString() || rightOperand.isString()) {
final String leftValue = getAsString(leftOperand);
final String rightValue = getAsString(rightOperand);
if (leftValue != null && rightValue != null)
return new BasicBooleanValue(compareTo(leftValue, rightValue));
}
if (leftOperand.isJavaObject()) {
return new BasicBooleanValue(compareTo(
((BasicJavaObjectValue) leftOperand), rightOperand));
if (leftOperand.isJavaObject() || rightOperand.isJavaObject()) {
final Object leftValue = getAsObject(leftOperand);
final Object rightValue = getAsObject(rightOperand);
if (leftValue != null && rightValue != null) {
if (leftValue instanceof Comparable<?> && rightValue instanceof Comparable<?>) {
@SuppressWarnings("unchecked")
final Comparable<Comparable<?>> a = (Comparable<Comparable<?>>) leftValue;
final Comparable<?> b = (Comparable<?>) rightValue;
return new BasicBooleanValue(compareTo(a, b));
}
}
}
return null;
throw new BasicRuntimeException("Type mismatch, left operand: " + leftOperand +
", right operand: " + rightOperand);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,88 @@
package com.scriptbasic.executors.operators;

import com.scriptbasic.executors.AbstractExpression;
import com.scriptbasic.executors.rightvalues.BasicBooleanValue;
import com.scriptbasic.executors.rightvalues.BasicDoubleValue;
import com.scriptbasic.executors.rightvalues.BasicJavaObjectValue;
import com.scriptbasic.executors.rightvalues.BasicLongValue;
import com.scriptbasic.executors.rightvalues.BasicStringValue;
import com.scriptbasic.interfaces.BasicRuntimeException;
import com.scriptbasic.spi.RightValue;

public abstract class AbstractOperator extends AbstractExpression {

/**
* Try to convert operator to double
*
* @param op
* operator
* @return Return double or null if cannot be converted
*/
static protected Double getAsDouble(RightValue op) {
try {
return BasicDoubleValue.asDouble(op);
} catch (BasicRuntimeException e) {
return null;
}
}

/**
* Try to convert operator to long
*
* @param op
* operator
* @return Return long or null if cannot be converted
*/
static protected Long getAsLong(RightValue op) {
try {
return BasicLongValue.asLong(op);
} catch (BasicRuntimeException e) {
return null;
}
}

/**
* Try to convert operator to boolean
*
* @param op
* operator
* @return Return boolean or null if cannot be converted
*/
static protected Boolean getAsBoolean(RightValue op) {
try {
return BasicBooleanValue.asBoolean(op);
} catch (BasicRuntimeException e) {
return null;
}
}

/**
* Try to convert operator to string
*
* @param op
* operator
* @return Return string or null if cannot be converted
*/
static protected String getAsString(RightValue op) {
try {
return BasicStringValue.asString(op);
} catch (BasicRuntimeException e) {
return null;
}
}

/**
* Try to convert operator to java object
*
* @param op
* operator
* @return Return object or null if cannot be converted
*/
static protected Object getAsObject(RightValue op) {
try {
return BasicJavaObjectValue.asObject(op);
} catch (BasicRuntimeException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,9 @@
package com.scriptbasic.executors.operators;

import com.scriptbasic.executors.rightvalues.*;
import com.scriptbasic.interfaces.BasicRuntimeException;
import com.scriptbasic.spi.RightValue;

public class EqualsOperator extends AbstractCompareOperator {

@Override
protected Boolean compareTo(final BasicDoubleValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue().equals(BasicDoubleValue.asDouble(op));
}

@Override
protected Boolean compareTo(final BasicLongValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue().equals(BasicLongValue.asLong(op));
}

@Override
protected Boolean compareTo(final BasicStringValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue().equals(BasicStringValue.asString(op));
protected boolean decide(int comparisonResult) {
return comparisonResult == 0;
}

@Override
protected Boolean compareTo(final BasicJavaObjectValue f,
final RightValue op) throws BasicRuntimeException {
return compareJavaObjectTo(f, op) == 0;
}

@Override
protected Boolean compareTo(final BasicBooleanValue f, final RightValue op) {
return f.getValue().equals(BasicBooleanValue.asBoolean(op));
}

}
Original file line number Diff line number Diff line change
@@ -1,40 +1,9 @@
package com.scriptbasic.executors.operators;

import com.scriptbasic.executors.rightvalues.*;
import com.scriptbasic.interfaces.BasicRuntimeException;
import com.scriptbasic.spi.RightValue;

public class GreaterOrEqualOperator extends AbstractCompareOperator {

@Override
protected Boolean compareTo(final BasicDoubleValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue() >= BasicDoubleValue.asDouble(op, "Cannot float compare >= on undef value");
}

@Override
protected Boolean compareTo(final BasicLongValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue() >= BasicLongValue.asLong(op, "Cannot integer compare >= undef value");
}

@Override
protected Boolean compareTo(final BasicStringValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue().compareTo(BasicStringValue.asString(op)) >= 0;
protected boolean decide(int comparisonResult) {
return comparisonResult >= 0;
}

@Override
protected Boolean compareTo(final BasicJavaObjectValue f,
final RightValue op) throws BasicRuntimeException {
return compareJavaObjectTo(f, op) >= 0;
}

@Override
protected Boolean compareTo(final BasicBooleanValue f, final RightValue op) {
final var a = f.getValue() ? 1 : 0;
final var b = BasicBooleanValue.asBoolean(op) ? 1 : 0;
return a >= b;
}

}
Original file line number Diff line number Diff line change
@@ -1,40 +1,9 @@
package com.scriptbasic.executors.operators;

import com.scriptbasic.executors.rightvalues.*;
import com.scriptbasic.interfaces.BasicRuntimeException;
import com.scriptbasic.spi.RightValue;

public class GreaterThanOperator extends AbstractCompareOperator {

@Override
protected Boolean compareTo(final BasicDoubleValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue() > BasicDoubleValue.asDouble(op, "Cannot float compare > undef value");
}

@Override
protected Boolean compareTo(final BasicLongValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue() > BasicLongValue.asLong(op, "Cannot integer compare > undef value");
}

@Override
protected Boolean compareTo(final BasicStringValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue().compareTo(BasicStringValue.asString(op)) > 0;
protected boolean decide(int comparisonResult) {
return comparisonResult > 0;
}

@Override
protected Boolean compareTo(final BasicJavaObjectValue f,
final RightValue op) throws BasicRuntimeException {
return compareJavaObjectTo(f, op) > 0;
}

@Override
protected Boolean compareTo(final BasicBooleanValue f, final RightValue op) {
final var a = f.getValue() ? 1 : 0;
final var b = BasicBooleanValue.asBoolean(op) ? 1 : 0;
return a > b;
}

}
Original file line number Diff line number Diff line change
@@ -1,40 +1,9 @@
package com.scriptbasic.executors.operators;

import com.scriptbasic.executors.rightvalues.*;
import com.scriptbasic.interfaces.BasicRuntimeException;
import com.scriptbasic.spi.RightValue;

public class LessOrEqualOperator extends AbstractCompareOperator {

@Override
protected Boolean compareTo(final BasicDoubleValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue() <= BasicDoubleValue.asDouble(op, "Cannot float compare <= undef value");
}

@Override
protected Boolean compareTo(final BasicLongValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue() <= BasicLongValue.asLong(op, "Cannot integer compare <= undef value");
}

@Override
protected Boolean compareTo(final BasicStringValue f, final RightValue op)
throws BasicRuntimeException {
return f.getValue().compareTo(BasicStringValue.asString(op)) <= 0;
protected boolean decide(int comparisonResult) {
return comparisonResult <= 0;
}

@Override
protected Boolean compareTo(final BasicJavaObjectValue f,
final RightValue op) throws BasicRuntimeException {
return compareJavaObjectTo(f, op) <= 0;
}

@Override
protected Boolean compareTo(final BasicBooleanValue f, final RightValue op) {
final var a = f.getValue() ? 1 : 0;
final var b = BasicBooleanValue.asBoolean(op) ? 1 : 0;
return a <= b;
}

}
Loading