use of org.springframework.expression.TypeComparator in project spring-framework by spring-projects.
the class DefaultComparatorUnitTests method testNonPrimitiveNumbers.
@Test
public void testNonPrimitiveNumbers() throws EvaluationException {
TypeComparator comparator = new StandardTypeComparator();
BigDecimal bdOne = new BigDecimal("1");
BigDecimal bdTwo = new BigDecimal("2");
assertTrue(comparator.compare(bdOne, bdTwo) < 0);
assertTrue(comparator.compare(bdOne, new BigDecimal("1")) == 0);
assertTrue(comparator.compare(bdTwo, bdOne) > 0);
assertTrue(comparator.compare(1, bdTwo) < 0);
assertTrue(comparator.compare(1, bdOne) == 0);
assertTrue(comparator.compare(2, bdOne) > 0);
assertTrue(comparator.compare(1.0d, bdTwo) < 0);
assertTrue(comparator.compare(1.0d, bdOne) == 0);
assertTrue(comparator.compare(2.0d, bdOne) > 0);
assertTrue(comparator.compare(1.0f, bdTwo) < 0);
assertTrue(comparator.compare(1.0f, bdOne) == 0);
assertTrue(comparator.compare(2.0f, bdOne) > 0);
assertTrue(comparator.compare(1L, bdTwo) < 0);
assertTrue(comparator.compare(1L, bdOne) == 0);
assertTrue(comparator.compare(2L, bdOne) > 0);
}
use of org.springframework.expression.TypeComparator in project spring-framework by spring-projects.
the class OperatorBetween method getValueInternal.
/**
* Returns a boolean based on whether a value is in the range expressed. The first
* operand is any value whilst the second is a list of two values - those two values
* being the bounds allowed for the first operand (inclusive).
* @param state the expression state
* @return true if the left operand is in the range specified, false otherwise
* @throws EvaluationException if there is a problem evaluating the expression
*/
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
Object left = getLeftOperand().getValueInternal(state).getValue();
Object right = getRightOperand().getValueInternal(state).getValue();
if (!(right instanceof List) || ((List<?>) right).size() != 2) {
throw new SpelEvaluationException(getRightOperand().getStartPosition(), SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST);
}
List<?> list = (List<?>) right;
Object low = list.get(0);
Object high = list.get(1);
TypeComparator comp = state.getTypeComparator();
try {
return BooleanTypedValue.forValue(comp.compare(left, low) >= 0 && comp.compare(left, high) <= 0);
} catch (SpelEvaluationException ex) {
ex.setPosition(getStartPosition());
throw ex;
}
}
use of org.springframework.expression.TypeComparator in project spring-framework by spring-projects.
the class StandardComponentsTests method testStandardEvaluationContext.
@Test
public void testStandardEvaluationContext() {
StandardEvaluationContext context = new StandardEvaluationContext();
assertNotNull(context.getTypeComparator());
TypeComparator tc = new StandardTypeComparator();
context.setTypeComparator(tc);
assertEquals(tc, context.getTypeComparator());
TypeLocator tl = new StandardTypeLocator();
context.setTypeLocator(tl);
assertEquals(tl, context.getTypeLocator());
}
Aggregations