Search in sources :

Example 6 with TypeComparator

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);
}
Also used : TypeComparator(org.springframework.expression.TypeComparator) StandardTypeComparator(org.springframework.expression.spel.support.StandardTypeComparator) StandardTypeComparator(org.springframework.expression.spel.support.StandardTypeComparator) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 7 with TypeComparator

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;
    }
}
Also used : TypeComparator(org.springframework.expression.TypeComparator) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) List(java.util.List)

Example 8 with TypeComparator

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());
}
Also used : TypeComparator(org.springframework.expression.TypeComparator) TypeLocator(org.springframework.expression.TypeLocator) Test(org.junit.Test)

Aggregations

TypeComparator (org.springframework.expression.TypeComparator)8 Test (org.junit.Test)7 StandardTypeComparator (org.springframework.expression.spel.support.StandardTypeComparator)5 BigDecimal (java.math.BigDecimal)1 List (java.util.List)1 TypeLocator (org.springframework.expression.TypeLocator)1 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)1