1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package org.springmodules.validation.valang.predicates;
18  
19  import org.apache.commons.collections.Predicate;
20  import org.apache.commons.collections.functors.AndPredicate;
21  import org.springmodules.validation.valang.functions.Function;
22  import org.springmodules.validation.valang.functions.LiteralFunction;
23  
24  /**
25   * Tests if a value is between two others.
26   *
27   * @author David Winterfeldt
28   */
29  public class BetweenTestPredicate extends AbstractPropertyPredicate {
30  
31      /**
32       * <p>Constructor taking two functions and an operator.
33       *
34       * @param leftFunction the left function
35       * @param operator the operator.
36       */
37      public BetweenTestPredicate(Function leftFunction, Operator operator, Function rightFunction, int line, int column) {
38          super(leftFunction, operator, rightFunction, line, column);
39      }
40  
41  
42      /**
43       * <p>The evaluate method takes the result of both functions and tests with the operator.
44       *
45       * @param   target      The target bean.
46       * @return  boolean      Whether or not the test passed.
47       */
48      public boolean evaluate(Object target) {
49          
50          Object leftValue = getLeftFunction().getResult(target);
51          Object rightValue = (getRightFunction() != null ? getRightFunction().getResult(target) : null);
52  
53          Object[] array = getArray(rightValue);
54          
55          Predicate predicate1 = new GreaterThanOrEqualTestPredicate(new LiteralFunction(leftValue), Operator.GREATER_THAN_OR_EQUAL, (Function) array[0], getLine(), getColumn());
56          Predicate predicate2 = new LessThanOrEqualTestPredicate(new LiteralFunction(leftValue), Operator.LESS_THAN_OR_EQUAL, (Function) array[1], getLine(), getColumn());
57  
58          return AndPredicate.getInstance(predicate1, predicate2).evaluate(target);
59      }
60  
61  }