org.springframework.expression.Operation Java Examples

The following examples show how to use org.springframework.expression.Operation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OperatorOverloaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Object operate(Operation operation, Object leftOperand, Object rightOperand) throws EvaluationException {
	if (operation==Operation.ADD) {
		return ((String)leftOperand)+((Boolean)rightOperand).toString();
	}
	else {
		return leftOperand;
	}
}
 
Example #2
Source File: ExpressionState.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public TypedValue operate(Operation op, Object left, Object right) throws EvaluationException {
	OperatorOverloader overloader = this.relatedContext.getOperatorOverloader();
	if (overloader.overridesOperation(op, left, right)) {
		Object returnValue = overloader.operate(op, left, right);
		return new TypedValue(returnValue);
	}
	else {
		String leftType = (left == null ? "null" : left.getClass().getName());
		String rightType = (right == null? "null" : right.getClass().getName());
		throw new SpelEvaluationException(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES, op, leftType, rightType);
	}
}
 
Example #3
Source File: ExpressionState.java    From java-technology-stack with MIT License 5 votes vote down vote up
public TypedValue operate(Operation op, @Nullable Object left, @Nullable Object right) throws EvaluationException {
	OperatorOverloader overloader = this.relatedContext.getOperatorOverloader();
	if (overloader.overridesOperation(op, left, right)) {
		Object returnValue = overloader.operate(op, left, right);
		return new TypedValue(returnValue);
	}
	else {
		String leftType = (left == null ? "null" : left.getClass().getName());
		String rightType = (right == null? "null" : right.getClass().getName());
		throw new SpelEvaluationException(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES, op, leftType, rightType);
	}
}
 
Example #4
Source File: OperatorPower.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();
	SpelNodeImpl rightOp = getRightOperand();

	Object leftOperand = leftOp.getValueInternal(state).getValue();
	Object rightOperand = rightOp.getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.pow(rightNumber.intValue()));
		}
		else if (leftNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.pow(rightNumber.intValue()));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			return new TypedValue(Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue()));
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			return new TypedValue(Math.pow(leftNumber.floatValue(), rightNumber.floatValue()));
		}

		double d = Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue());
		if (d > Integer.MAX_VALUE || leftNumber instanceof Long || rightNumber instanceof Long) {
			return new TypedValue((long) d);
		}
		else {
			return new TypedValue((int) d);
		}
	}

	return state.operate(Operation.POWER, leftOperand, rightOperand);
}
 
Example #5
Source File: OperatorPower.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();
	SpelNodeImpl rightOp = getRightOperand();

	Object leftOperand = leftOp.getValueInternal(state).getValue();
	Object rightOperand = rightOp.getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.pow(rightNumber.intValue()));
		}
		else if (leftNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.pow(rightNumber.intValue()));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			return new TypedValue(Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue()));
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			return new TypedValue(Math.pow(leftNumber.floatValue(), rightNumber.floatValue()));
		}

		double d = Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue());
		if (d > Integer.MAX_VALUE || leftNumber instanceof Long || rightNumber instanceof Long) {
			return new TypedValue((long) d);
		}
		else {
			return new TypedValue((int) d);
		}
	}

	return state.operate(Operation.POWER, leftOperand, rightOperand);
}
 
Example #6
Source File: OperatorOverloaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Object operate(Operation operation, Object leftOperand, Object rightOperand) throws EvaluationException {
	if (operation==Operation.ADD) {
		return ((String)leftOperand)+((Boolean)rightOperand).toString();
	}
	else {
		return leftOperand;
	}
}
 
Example #7
Source File: OperatorOverloaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean overridesOperation(Operation operation, Object leftOperand, Object rightOperand) throws EvaluationException {
	if (leftOperand instanceof String && rightOperand instanceof Boolean) {
		return true;
	}
	return false;

}
 
Example #8
Source File: OperatorOverloaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean overridesOperation(Operation operation, Object leftOperand, Object rightOperand) throws EvaluationException {
	if (leftOperand instanceof String && rightOperand instanceof Boolean) {
		return true;
	}
	return false;

}
 
Example #9
Source File: ExpressionState.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public TypedValue operate(Operation op, @Nullable Object left, @Nullable Object right) throws EvaluationException {
	OperatorOverloader overloader = this.relatedContext.getOperatorOverloader();
	if (overloader.overridesOperation(op, left, right)) {
		Object returnValue = overloader.operate(op, left, right);
		return new TypedValue(returnValue);
	}
	else {
		String leftType = (left == null ? "null" : left.getClass().getName());
		String rightType = (right == null? "null" : right.getClass().getName());
		throw new SpelEvaluationException(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES, op, leftType, rightType);
	}
}
 
Example #10
Source File: ExpressionState.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public TypedValue operate(Operation op, Object left, Object right) throws EvaluationException {
	OperatorOverloader overloader = this.relatedContext.getOperatorOverloader();
	if (overloader.overridesOperation(op, left, right)) {
		Object returnValue = overloader.operate(op, left, right);
		return new TypedValue(returnValue);
	}
	else {
		String leftType = (left == null ? "null" : left.getClass().getName());
		String rightType = (right == null? "null" : right.getClass().getName());
		throw new SpelEvaluationException(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES, op, leftType, rightType);
	}
}
 
Example #11
Source File: OperatorOverloaderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Object operate(Operation operation, Object leftOperand, Object rightOperand) throws EvaluationException {
	if (operation==Operation.ADD) {
		return ((String)leftOperand)+((Boolean)rightOperand).toString();
	} else {
		return leftOperand;
	}
}
 
Example #12
Source File: OperatorOverloaderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean overridesOperation(Operation operation, Object leftOperand, Object rightOperand)
		throws EvaluationException {
	if (leftOperand instanceof String && rightOperand instanceof Boolean) {
		return true;
	}
	return false;

}
 
Example #13
Source File: OperatorPower.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();
	SpelNodeImpl rightOp = getRightOperand();

	Object leftOperand = leftOp.getValueInternal(state).getValue();
	Object rightOperand = rightOp.getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.pow(rightNumber.intValue()));
		}
		else if (leftNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.pow(rightNumber.intValue()));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			return new TypedValue(Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue()));
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			return new TypedValue(Math.pow(leftNumber.floatValue(), rightNumber.floatValue()));
		}

		double d = Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue());
		if (d > Integer.MAX_VALUE || leftNumber instanceof Long || rightNumber instanceof Long) {
			return new TypedValue((long) d);
		}
		else {
			return new TypedValue((int) d);
		}
	}

	return state.operate(Operation.POWER, leftOperand, rightOperand);
}
 
Example #14
Source File: OperatorPower.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();
	SpelNodeImpl rightOp = getRightOperand();

	Object leftOperand = leftOp.getValueInternal(state).getValue();
	Object rightOperand = rightOp.getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.pow(rightNumber.intValue()));
		}
		else if (leftNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.pow(rightNumber.intValue()));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			return new TypedValue(Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue()));
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			return new TypedValue(Math.pow(leftNumber.floatValue(), rightNumber.floatValue()));
		}

		double d = Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue());
		if (d > Integer.MAX_VALUE || leftNumber instanceof Long || rightNumber instanceof Long) {
			return new TypedValue((long) d);
		}
		else {
			return new TypedValue((int) d);
		}
	}

	return state.operate(Operation.POWER, leftOperand, rightOperand);
}
 
Example #15
Source File: OpModulus.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
	Object rightOperand = getRightOperand().getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.remainder(rightBigDecimal));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.remainder(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() % rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() % rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double division
			return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
		}
	}

	return state.operate(Operation.MODULUS, leftOperand, rightOperand);
}
 
Example #16
Source File: OpMultiply.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Implements the {@code multiply} operator directly here for certain types
 * of supported operands and otherwise delegates to any registered overloader
 * for types not supported here.
 * <p>Supported operand types:
 * <ul>
 * <li>numbers
 * <li>String and int ('abc' * 2 == 'abcabc')
 * </ul>
 */
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
	Object rightOperand = getRightOperand().getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.multiply(rightBigDecimal));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() * rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.multiply(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() * rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() * rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double multiplication
			return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue());
		}
	}

	if (leftOperand instanceof String && rightOperand instanceof Integer) {
		int repeats = (Integer) rightOperand;
		StringBuilder result = new StringBuilder();
		for (int i = 0; i < repeats; i++) {
			result.append(leftOperand);
		}
		return new TypedValue(result.toString());
	}

	return state.operate(Operation.MULTIPLY, leftOperand, rightOperand);
}
 
Example #17
Source File: StandardOperatorOverloader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean overridesOperation(Operation operation, Object leftOperand, Object rightOperand)
		throws EvaluationException {
	return false;
}
 
Example #18
Source File: OpPlus.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();
	SpelNodeImpl rightOp = getRightOperand();

	if (rightOp == null) {  // if only one operand, then this is unary plus
		Object operandOne = leftOp.getValueInternal(state).getValue();
		if (operandOne instanceof Number) {
			if (operandOne instanceof Double) {
				this.exitTypeDescriptor = "D";
			}
			else if (operandOne instanceof Float) {
				this.exitTypeDescriptor = "F";
			}
			else if (operandOne instanceof Long) {
				this.exitTypeDescriptor = "J";
			}
			else if (operandOne instanceof Integer) {
				this.exitTypeDescriptor = "I";
			}
			return new TypedValue(operandOne);
		}
		return state.operate(Operation.ADD, operandOne, null);
	}

	TypedValue operandOneValue = leftOp.getValueInternal(state);
	Object leftOperand = operandOneValue.getValue();
	TypedValue operandTwoValue = rightOp.getValueInternal(state);
	Object rightOperand = operandTwoValue.getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.add(rightBigDecimal));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() + rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.add(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() + rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() + rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double addition
			return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue());
		}
	}

	if (leftOperand instanceof String && rightOperand instanceof String) {
		this.exitTypeDescriptor = "Ljava/lang/String";
		return new TypedValue((String) leftOperand + rightOperand);
	}

	if (leftOperand instanceof String) {
		return new TypedValue(
				leftOperand + (rightOperand == null ? "null" : convertTypedValueToString(operandTwoValue, state)));
	}

	if (rightOperand instanceof String) {
		return new TypedValue(
				(leftOperand == null ? "null" : convertTypedValueToString(operandOneValue, state)) + rightOperand);
	}

	return state.operate(Operation.ADD, leftOperand, rightOperand);
}
 
Example #19
Source File: StandardOperatorOverloader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Object operate(Operation operation, Object leftOperand, Object rightOperand) throws EvaluationException {
	throw new EvaluationException("No operation overloaded by default");
}
 
Example #20
Source File: OpMinus.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();
	SpelNodeImpl rightOp = getRightOperand();

	if (rightOp == null) {  // if only one operand, then this is unary minus
		Object operand = leftOp.getValueInternal(state).getValue();
		if (operand instanceof Number) {
			if (operand instanceof BigDecimal) {
				return new TypedValue(((BigDecimal) operand).negate());
			}
			else if (operand instanceof Double) {
				this.exitTypeDescriptor = "D";
				return new TypedValue(0 - ((Number) operand).doubleValue());
			}
			else if (operand instanceof Float) {
				this.exitTypeDescriptor = "F";
				return new TypedValue(0 - ((Number) operand).floatValue());
			}
			else if (operand instanceof BigInteger) {
				return new TypedValue(((BigInteger) operand).negate());
			}
			else if (operand instanceof Long) {
				this.exitTypeDescriptor = "J";
				return new TypedValue(0 - ((Number) operand).longValue());
			}
			else if (operand instanceof Integer) {
				this.exitTypeDescriptor = "I";
				return new TypedValue(0 - ((Number) operand).intValue());
			}
			else if (operand instanceof Short) {
				return new TypedValue(0 - ((Number) operand).shortValue());
			}
			else if (operand instanceof Byte) {
				return new TypedValue(0 - ((Number) operand).byteValue());
			}
			else {
				// Unknown Number subtypes -> best guess is double subtraction
				return new TypedValue(0 - ((Number) operand).doubleValue());
			}
		}
		return state.operate(Operation.SUBTRACT, operand, null);
	}

	Object left = leftOp.getValueInternal(state).getValue();
	Object right = rightOp.getValueInternal(state).getValue();

	if (left instanceof Number && right instanceof Number) {
		Number leftNumber = (Number) left;
		Number rightNumber = (Number) right;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.subtract(rightBigDecimal));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() - rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.subtract(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() - rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() - rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double subtraction
			return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue());
		}
	}

	if (left instanceof String && right instanceof Integer && ((String) left).length() == 1) {
		String theString = (String) left;
		Integer theInteger = (Integer) right;
		// Implements character - int (ie. b - 1 = a)
		return new TypedValue(Character.toString((char) (theString.charAt(0) - theInteger)));
	}

	return state.operate(Operation.SUBTRACT, left, right);
}
 
Example #21
Source File: OpDivide.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
	Object rightOperand = getRightOperand().getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			int scale = Math.max(leftBigDecimal.scale(), rightBigDecimal.scale());
			return new TypedValue(leftBigDecimal.divide(rightBigDecimal, scale, RoundingMode.HALF_EVEN));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() / rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.divide(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() / rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() / rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double division
			return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue());
		}
	}

	return state.operate(Operation.DIVIDE, leftOperand, rightOperand);
}
 
Example #22
Source File: StandardComponentsTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test(expected = EvaluationException.class)
public void testStandardOperatorOverloader() throws EvaluationException {
	OperatorOverloader oo = new StandardOperatorOverloader();
	assertFalse(oo.overridesOperation(Operation.ADD, null, null));
	oo.operate(Operation.ADD, 2, 3);
}
 
Example #23
Source File: OpPlus.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();
	SpelNodeImpl rightOp = getRightOperand();

	if (rightOp == null) {  // if only one operand, then this is unary plus
		Object operandOne = leftOp.getValueInternal(state).getValue();
		if (operandOne instanceof Number) {
			if (operandOne instanceof Double) {
				this.exitTypeDescriptor = "D";
			}
			else if (operandOne instanceof Float) {
				this.exitTypeDescriptor = "F";
			}
			else if (operandOne instanceof Long) {
				this.exitTypeDescriptor = "J";
			}
			else if (operandOne instanceof Integer) {
				this.exitTypeDescriptor = "I";
			}
			return new TypedValue(operandOne);
		}
		return state.operate(Operation.ADD, operandOne, null);
	}

	TypedValue operandOneValue = leftOp.getValueInternal(state);
	Object leftOperand = operandOneValue.getValue();
	TypedValue operandTwoValue = rightOp.getValueInternal(state);
	Object rightOperand = operandTwoValue.getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.add(rightBigDecimal));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() + rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.add(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() + rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() + rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double addition
			return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue());
		}
	}

	if (leftOperand instanceof String && rightOperand instanceof String) {
		this.exitTypeDescriptor = "Ljava/lang/String";
		return new TypedValue((String) leftOperand + rightOperand);
	}

	if (leftOperand instanceof String) {
		return new TypedValue(
				leftOperand + (rightOperand == null ? "null" : convertTypedValueToString(operandTwoValue, state)));
	}

	if (rightOperand instanceof String) {
		return new TypedValue(
				(leftOperand == null ? "null" : convertTypedValueToString(operandOneValue, state)) + rightOperand);
	}

	return state.operate(Operation.ADD, leftOperand, rightOperand);
}
 
Example #24
Source File: OpMultiply.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Implements the {@code multiply} operator directly here for certain types
 * of supported operands and otherwise delegates to any registered overloader
 * for types not supported here.
 * <p>Supported operand types:
 * <ul>
 * <li>numbers
 * <li>String and int ('abc' * 2 == 'abcabc')
 * </ul>
 */
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
	Object rightOperand = getRightOperand().getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.multiply(rightBigDecimal));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() * rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.multiply(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() * rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() * rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double multiplication
			return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue());
		}
	}

	if (leftOperand instanceof String && rightOperand instanceof Integer) {
		int repeats = (Integer) rightOperand;
		StringBuilder result = new StringBuilder();
		for (int i = 0; i < repeats; i++) {
			result.append(leftOperand);
		}
		return new TypedValue(result.toString());
	}

	return state.operate(Operation.MULTIPLY, leftOperand, rightOperand);
}
 
Example #25
Source File: StandardOperatorOverloader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public boolean overridesOperation(Operation operation, Object leftOperand, Object rightOperand)
		throws EvaluationException {
	return false;
}
 
Example #26
Source File: StandardOperatorOverloader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public Object operate(Operation operation, Object leftOperand, Object rightOperand) throws EvaluationException {
	throw new EvaluationException("No operation overloaded by default");
}
 
Example #27
Source File: OpMinus.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();

	if (this.children.length < 2) {  // if only one operand, then this is unary minus
		Object operand = leftOp.getValueInternal(state).getValue();
		if (operand instanceof Number) {
			if (operand instanceof BigDecimal) {
				return new TypedValue(((BigDecimal) operand).negate());
			}
			else if (operand instanceof Double) {
				this.exitTypeDescriptor = "D";
				return new TypedValue(0 - ((Number) operand).doubleValue());
			}
			else if (operand instanceof Float) {
				this.exitTypeDescriptor = "F";
				return new TypedValue(0 - ((Number) operand).floatValue());
			}
			else if (operand instanceof BigInteger) {
				return new TypedValue(((BigInteger) operand).negate());
			}
			else if (operand instanceof Long) {
				this.exitTypeDescriptor = "J";
				return new TypedValue(0 - ((Number) operand).longValue());
			}
			else if (operand instanceof Integer) {
				this.exitTypeDescriptor = "I";
				return new TypedValue(0 - ((Number) operand).intValue());
			}
			else if (operand instanceof Short) {
				return new TypedValue(0 - ((Number) operand).shortValue());
			}
			else if (operand instanceof Byte) {
				return new TypedValue(0 - ((Number) operand).byteValue());
			}
			else {
				// Unknown Number subtypes -> best guess is double subtraction
				return new TypedValue(0 - ((Number) operand).doubleValue());
			}
		}
		return state.operate(Operation.SUBTRACT, operand, null);
	}

	Object left = leftOp.getValueInternal(state).getValue();
	Object right = getRightOperand().getValueInternal(state).getValue();

	if (left instanceof Number && right instanceof Number) {
		Number leftNumber = (Number) left;
		Number rightNumber = (Number) right;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.subtract(rightBigDecimal));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() - rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.subtract(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() - rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() - rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double subtraction
			return new TypedValue(leftNumber.doubleValue() - rightNumber.doubleValue());
		}
	}

	if (left instanceof String && right instanceof Integer && ((String) left).length() == 1) {
		String theString = (String) left;
		Integer theInteger = (Integer) right;
		// Implements character - int (ie. b - 1 = a)
		return new TypedValue(Character.toString((char) (theString.charAt(0) - theInteger)));
	}

	return state.operate(Operation.SUBTRACT, left, right);
}
 
Example #28
Source File: OpDivide.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
	Object rightOperand = getRightOperand().getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			int scale = Math.max(leftBigDecimal.scale(), rightBigDecimal.scale());
			return new TypedValue(leftBigDecimal.divide(rightBigDecimal, scale, RoundingMode.HALF_EVEN));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() / rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.divide(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() / rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() / rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double division
			return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue());
		}
	}

	return state.operate(Operation.DIVIDE, leftOperand, rightOperand);
}
 
Example #29
Source File: OpDivide.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
	Object rightOperand = getRightOperand().getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			int scale = Math.max(leftBigDecimal.scale(), rightBigDecimal.scale());
			return new TypedValue(leftBigDecimal.divide(rightBigDecimal, scale, RoundingMode.HALF_EVEN));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() / rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.divide(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() / rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() / rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double division
			return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue());
		}
	}

	return state.operate(Operation.DIVIDE, leftOperand, rightOperand);
}
 
Example #30
Source File: OpModulus.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
	Object rightOperand = getRightOperand().getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.remainder(rightBigDecimal));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.remainder(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() % rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() % rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double division
			return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
		}
	}

	return state.operate(Operation.MODULUS, leftOperand, rightOperand);
}