Java Code Examples for org.springframework.util.NumberUtils#convertNumberToTargetClass()
The following examples show how to use
org.springframework.util.NumberUtils#convertNumberToTargetClass() .
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: SingleColumnRowMapper.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Convert the given column value to the specified required type. * Only called if the extracted column value does not match already. * <p>If the required type is String, the value will simply get stringified * via {@code toString()}. In case of a Number, the value will be * converted into a Number, either through number conversion or through * String parsing (depending on the value type). * @param value the column value as extracted from {@code getColumnValue()} * (never {@code null}) * @param requiredType the type that each result object is expected to match * (never {@code null}) * @return the converted value * @see #getColumnValue(java.sql.ResultSet, int, Class) */ @SuppressWarnings("unchecked") protected Object convertValueToRequiredType(Object value, Class<?> requiredType) { if (String.class == requiredType) { return value.toString(); } else if (Number.class.isAssignableFrom(requiredType)) { if (value instanceof Number) { // Convert original Number to target Number class. return NumberUtils.convertNumberToTargetClass(((Number) value), (Class<Number>) requiredType); } else { // Convert stringified value to target Number class. return NumberUtils.parseNumber(value.toString(),(Class<Number>) requiredType); } } else { throw new IllegalArgumentException( "Value [" + value + "] is of type [" + value.getClass().getName() + "] and cannot be converted to required type [" + requiredType.getName() + "]"); } }
Example 2
Source File: WebSlider.java From cuba with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") protected V convertFromDouble(Double componentValue) throws ConversionException { if (componentValue == null) { return null; } Datatype<V> datatype = getDatatype(); return (V) NumberUtils.convertNumberToTargetClass(componentValue, datatype.getJavaClass()); }
Example 3
Source File: CustomNumberEditor.java From blog_demos with Apache License 2.0 | 5 votes |
/** * Coerce a Number value into the required target class, if necessary. */ @Override public void setValue(Object value) { if (value instanceof Number) { super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass)); } else { super.setValue(value); } }
Example 4
Source File: OperatorPower.java From spring-analysis-note with MIT License | 5 votes |
@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: CustomNumberEditor.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Coerce a Number value into the required target class, if necessary. */ @Override public void setValue(Object value) { if (value instanceof Number) { super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass)); } else { super.setValue(value); } }
Example 6
Source File: OperatorPower.java From lams with GNU General Public License v2.0 | 5 votes |
@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 7
Source File: OperatorPower.java From java-technology-stack with MIT License | 5 votes |
@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 8
Source File: StandardTypeComparator.java From lams with GNU General Public License v2.0 | 4 votes |
@Override @SuppressWarnings("unchecked") public int compare(Object left, Object right) throws SpelEvaluationException { // If one is null, check if the other is if (left == null) { return (right == null ? 0 : -1); } else if (right == null) { return 1; // left cannot be null at this point } // Basic number comparisons 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 leftBigDecimal.compareTo(rightBigDecimal); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { return Float.compare(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 leftBigInteger.compareTo(rightBigInteger); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { // Don't call Long.compare here - only available on JDK 1.7+ return compare(leftNumber.longValue(), rightNumber.longValue()); } else if (leftNumber instanceof Integer || rightNumber instanceof Integer) { // Don't call Integer.compare here - only available on JDK 1.7+ return compare(leftNumber.intValue(), rightNumber.intValue()); } else if (leftNumber instanceof Short || rightNumber instanceof Short) { // Don't call Short.compare here - only available on JDK 1.7+ return compare(leftNumber.shortValue(), rightNumber.shortValue()); } else if (leftNumber instanceof Byte || rightNumber instanceof Byte) { // Don't call Short.compare here - only available on JDK 1.7+ return compare(leftNumber.byteValue(), rightNumber.byteValue()); } else { // Unknown Number subtypes -> best guess is double multiplication return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue()); } } try { if (left instanceof Comparable) { return ((Comparable<Object>) left).compareTo(right); } } catch (ClassCastException ex) { throw new SpelEvaluationException(ex, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass()); } throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass()); }
Example 9
Source File: NumberToNumberConverterFactory.java From java-technology-stack with MIT License | 4 votes |
@Override public T convert(Number source) { return NumberUtils.convertNumberToTargetClass(source, this.targetType); }
Example 10
Source File: OpGE.java From java-technology-stack with MIT License | 4 votes |
@Override public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object left = getLeftOperand().getValueInternal(state).getValue(); Object right = getRightOperand().getValueInternal(state).getValue(); this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left); this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right); 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 BooleanTypedValue.forValue(leftBigDecimal.compareTo(rightBigDecimal) >= 0); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { return BooleanTypedValue.forValue(leftNumber.doubleValue() >= rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { return BooleanTypedValue.forValue(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 BooleanTypedValue.forValue(leftBigInteger.compareTo(rightBigInteger) >= 0); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { return BooleanTypedValue.forValue(leftNumber.longValue() >= rightNumber.longValue()); } else if (leftNumber instanceof Integer || rightNumber instanceof Integer) { return BooleanTypedValue.forValue(leftNumber.intValue() >= rightNumber.intValue()); } else if (leftNumber instanceof Short || rightNumber instanceof Short) { return BooleanTypedValue.forValue(leftNumber.shortValue() >= rightNumber.shortValue()); } else if (leftNumber instanceof Byte || rightNumber instanceof Byte) { return BooleanTypedValue.forValue(leftNumber.byteValue() >= rightNumber.byteValue()); } else { // Unknown Number subtypes -> best guess is double comparison return BooleanTypedValue.forValue(leftNumber.doubleValue() >= rightNumber.doubleValue()); } } return BooleanTypedValue.forValue(state.getTypeComparator().compare(left, right) >= 0); }
Example 11
Source File: OpModulus.java From java-technology-stack with MIT License | 4 votes |
@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 12
Source File: OpLE.java From java-technology-stack with MIT License | 4 votes |
@Override public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object left = getLeftOperand().getValueInternal(state).getValue(); Object right = getRightOperand().getValueInternal(state).getValue(); this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left); this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right); 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 BooleanTypedValue.forValue(leftBigDecimal.compareTo(rightBigDecimal) <= 0); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { return BooleanTypedValue.forValue(leftNumber.doubleValue() <= rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { return BooleanTypedValue.forValue(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 BooleanTypedValue.forValue(leftBigInteger.compareTo(rightBigInteger) <= 0); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { return BooleanTypedValue.forValue(leftNumber.longValue() <= rightNumber.longValue()); } else if (leftNumber instanceof Integer || rightNumber instanceof Integer) { return BooleanTypedValue.forValue(leftNumber.intValue() <= rightNumber.intValue()); } else if (leftNumber instanceof Short || rightNumber instanceof Short) { return BooleanTypedValue.forValue(leftNumber.shortValue() <= rightNumber.shortValue()); } else if (leftNumber instanceof Byte || rightNumber instanceof Byte) { return BooleanTypedValue.forValue(leftNumber.byteValue() <= rightNumber.byteValue()); } else { // Unknown Number subtypes -> best guess is double comparison return BooleanTypedValue.forValue(leftNumber.doubleValue() <= rightNumber.doubleValue()); } } return BooleanTypedValue.forValue(state.getTypeComparator().compare(left, right) <= 0); }
Example 13
Source File: OpLE.java From spring-analysis-note with MIT License | 4 votes |
@Override public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object left = getLeftOperand().getValueInternal(state).getValue(); Object right = getRightOperand().getValueInternal(state).getValue(); this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left); this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right); 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 BooleanTypedValue.forValue(leftBigDecimal.compareTo(rightBigDecimal) <= 0); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { return BooleanTypedValue.forValue(leftNumber.doubleValue() <= rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { return BooleanTypedValue.forValue(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 BooleanTypedValue.forValue(leftBigInteger.compareTo(rightBigInteger) <= 0); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { return BooleanTypedValue.forValue(leftNumber.longValue() <= rightNumber.longValue()); } else if (leftNumber instanceof Integer || rightNumber instanceof Integer) { return BooleanTypedValue.forValue(leftNumber.intValue() <= rightNumber.intValue()); } else if (leftNumber instanceof Short || rightNumber instanceof Short) { return BooleanTypedValue.forValue(leftNumber.shortValue() <= rightNumber.shortValue()); } else if (leftNumber instanceof Byte || rightNumber instanceof Byte) { return BooleanTypedValue.forValue(leftNumber.byteValue() <= rightNumber.byteValue()); } else { // Unknown Number subtypes -> best guess is double comparison return BooleanTypedValue.forValue(leftNumber.doubleValue() <= rightNumber.doubleValue()); } } return BooleanTypedValue.forValue(state.getTypeComparator().compare(left, right) <= 0); }
Example 14
Source File: CharacterToNumberFactory.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override public T convert(Character source) { return NumberUtils.convertNumberToTargetClass((short) source.charValue(), this.targetType); }
Example 15
Source File: Operator.java From spring4-understanding with Apache License 2.0 | 4 votes |
protected boolean equalityCheck(ExpressionState state, Object left, Object right) { 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 (leftBigDecimal == null ? rightBigDecimal == null : leftBigDecimal.compareTo(rightBigDecimal) == 0); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { return (leftNumber.doubleValue() == rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { return (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 (leftBigInteger == null ? rightBigInteger == null : leftBigInteger.compareTo(rightBigInteger) == 0); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { return (leftNumber.longValue() == rightNumber.longValue()); } else if (leftNumber instanceof Integer || rightNumber instanceof Integer) { return (leftNumber.intValue() == rightNumber.intValue()); } else if (leftNumber instanceof Short || rightNumber instanceof Short) { return (leftNumber.shortValue() == rightNumber.shortValue()); } else if (leftNumber instanceof Byte || rightNumber instanceof Byte) { return (leftNumber.byteValue() == rightNumber.byteValue()); } else { // Unknown Number subtypes -> best guess is double comparison return (leftNumber.doubleValue() == rightNumber.doubleValue()); } } if (left instanceof CharSequence && right instanceof CharSequence) { return left.toString().equals(right.toString()); } if (ObjectUtils.nullSafeEquals(left, right)) { return true; } if (left instanceof Comparable && right instanceof Comparable) { Class<?> ancestor = ClassUtils.determineCommonAncestor(left.getClass(), right.getClass()); if (ancestor != null && Comparable.class.isAssignableFrom(ancestor)) { return (state.getTypeComparator().compare(left, right) == 0); } } return false; }
Example 16
Source File: OpPlus.java From lams with GNU General Public License v2.0 | 4 votes |
@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 17
Source File: OpMultiply.java From lams with GNU General Public License v2.0 | 4 votes |
/** * 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 18
Source File: OpMultiply.java From spring4-understanding with Apache License 2.0 | 4 votes |
/** * 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 19
Source File: Operator.java From java-technology-stack with MIT License | 4 votes |
/** * Perform an equality check for the given operand values. * <p>This method is not just used for reflective comparisons in subclasses * but also from compiled expression code, which is why it needs to be * declared as {@code public static} here. * @param context the current evaluation context * @param left the left-hand operand value * @param right the right-hand operand value */ public static boolean equalityCheck(EvaluationContext context, @Nullable Object left, @Nullable Object right) { 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 (leftBigDecimal.compareTo(rightBigDecimal) == 0); } else if (leftNumber instanceof Double || rightNumber instanceof Double) { return (leftNumber.doubleValue() == rightNumber.doubleValue()); } else if (leftNumber instanceof Float || rightNumber instanceof Float) { return (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 (leftBigInteger.compareTo(rightBigInteger) == 0); } else if (leftNumber instanceof Long || rightNumber instanceof Long) { return (leftNumber.longValue() == rightNumber.longValue()); } else if (leftNumber instanceof Integer || rightNumber instanceof Integer) { return (leftNumber.intValue() == rightNumber.intValue()); } else if (leftNumber instanceof Short || rightNumber instanceof Short) { return (leftNumber.shortValue() == rightNumber.shortValue()); } else if (leftNumber instanceof Byte || rightNumber instanceof Byte) { return (leftNumber.byteValue() == rightNumber.byteValue()); } else { // Unknown Number subtypes -> best guess is double comparison return (leftNumber.doubleValue() == rightNumber.doubleValue()); } } if (left instanceof CharSequence && right instanceof CharSequence) { return left.toString().equals(right.toString()); } if (left instanceof Boolean && right instanceof Boolean) { return left.equals(right); } if (ObjectUtils.nullSafeEquals(left, right)) { return true; } if (left instanceof Comparable && right instanceof Comparable) { Class<?> ancestor = ClassUtils.determineCommonAncestor(left.getClass(), right.getClass()); if (ancestor != null && Comparable.class.isAssignableFrom(ancestor)) { return (context.getTypeComparator().compare(left, right) == 0); } } return false; }
Example 20
Source File: OpMinus.java From spring4-understanding with Apache License 2.0 | 4 votes |
@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); }