View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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          // constructor checks that left function is never null
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  }