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.lang.StringUtils;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.springmodules.validation.valang.functions.Function;
23  
24  /**
25   * <p>GenericTestPredicate can test if a property value is null or not null.
26   *
27   * @author Steven Devijver
28   * @since 23-04-2005
29   */
30  public class GenericTestPredicate extends AbstractPropertyPredicate {
31  
32      final Logger logger = LoggerFactory.getLogger(GenericTestPredicate.class);
33      
34      private final EqualsTestPredicate equalsTestPredicate;
35      private final NotEqualsTestPredicate notEqualsTestPredicate;
36      private final LessThanTestPredicate lessThanTestPredicate;
37      private final LessThanOrEqualTestPredicate lessThanOrEqualTestPredicate;
38      private final GreaterThanTestPredicate greaterThanTestPredicate;
39      private final GreaterThanOrEqualTestPredicate greaterThanOrEqualTestPredicate;
40      private final InTestPredicate inTestPredicate;
41      private final NotInTestPredicate notInTestPredicate;
42      private final BetweenTestPredicate betweenTestPredicate;
43      private final NotBetweenTestPredicate notBetweenTestPredicate;
44      
45      /**
46       * <p>Constructor taking two functions and an operator.
47       *
48       * @param leftFunction the left function
49       * @param operator the operator.
50       */
51      public GenericTestPredicate(Function leftFunction, Operator operator, Function rightFunction, int line, int column) {
52          super(leftFunction, operator, rightFunction, line, column);
53          
54          this.equalsTestPredicate = new EqualsTestPredicate(leftFunction, operator, rightFunction, line, column);
55          this.notEqualsTestPredicate = new NotEqualsTestPredicate(leftFunction, operator, rightFunction, line, column);
56          this.lessThanTestPredicate = new LessThanTestPredicate(leftFunction, operator, rightFunction, line, column);
57          this.lessThanOrEqualTestPredicate = new LessThanOrEqualTestPredicate(leftFunction, operator, rightFunction, line, column);
58          this.greaterThanTestPredicate = new GreaterThanTestPredicate(leftFunction, operator, rightFunction, line, column);
59          this.greaterThanOrEqualTestPredicate = new GreaterThanOrEqualTestPredicate(leftFunction, operator, rightFunction, line, column);
60          this.inTestPredicate = new InTestPredicate(leftFunction, operator, rightFunction, line, column);
61          this.notInTestPredicate = new NotInTestPredicate(leftFunction, operator, rightFunction, line, column);
62          this.betweenTestPredicate = new BetweenTestPredicate(leftFunction, operator, rightFunction, line, column);
63          this.notBetweenTestPredicate = new NotBetweenTestPredicate(leftFunction, operator, rightFunction, line, column);
64      }
65  
66  
67      /**
68       * <p>The evaluate method takes the result of both functions and tests with the operator.
69       *
70       * @param   target      The target bean.
71       * @return  boolean      Whether or not the test passed.
72       */
73      public boolean evaluate(Object target) {
74          // constructor checks that left function is never null
75          Object leftValue = getLeftFunction().getResult(target);
76          Object rightValue = (getRightFunction() != null ? getRightFunction().getResult(target) : null);
77  
78          switch(getOperator()) {
79              case NULL:
80                  return leftValue == null;
81              case NOT_NULL:
82                  return leftValue != null;
83              case EQUAL:
84                  return equalsTestPredicate.evaluate(target);
85              case NOT_EQUAL:
86                  return notEqualsTestPredicate.evaluate(target);
87              case LESS_THAN:
88                  return lessThanTestPredicate.evaluate(target);
89              case LESS_THAN_OR_EQUAL:
90                  return lessThanOrEqualTestPredicate.evaluate(target);
91              case GREATER_THAN:
92                  return greaterThanTestPredicate.evaluate(target);
93              case GREATER_THAN_OR_EQUAL:
94                  return greaterThanOrEqualTestPredicate.evaluate(target);
95              case IN:
96                  return inTestPredicate.evaluate(target);
97              case NOT_IN:
98                  return notInTestPredicate.evaluate(target);
99              case BETWEEN:
100                 return betweenTestPredicate.evaluate(target);
101             case NOT_BETWEEN:
102                 return notBetweenTestPredicate.evaluate(target);
103             case HAS_LENGTH:
104                 return org.springframework.util.StringUtils.hasLength(leftValue != null ? leftValue.toString() : null);
105             case HAS_NO_LENGTH:
106                 return !org.springframework.util.StringUtils.hasLength(leftValue != null ? leftValue.toString() : null);
107             case HAS_TEXT:
108                 return org.springframework.util.StringUtils.hasText(leftValue != null ? leftValue.toString() : null);
109             case HAS_NO_TEXT:
110                 return !org.springframework.util.StringUtils.hasText(leftValue != null ? leftValue.toString() : null);
111             case IS_BLANK:
112                 return isBlank(leftValue != null ? leftValue.toString() : null);
113             case IS_NOT_BLANK:
114                 return !isBlank(leftValue != null ? leftValue.toString() : null);
115             case IS_WORD:
116                 return isWord(leftValue != null ? leftValue.toString() : null);
117             case IS_NOT_WORD:
118                 return !isWord(leftValue != null ? leftValue.toString() : null);
119             case IS_LOWERCASE:
120                 return isLowerCase(leftValue != null ? leftValue.toString() : null);
121             case IS_NOT_LOWERCASE:
122                 return !isLowerCase(leftValue != null ? leftValue.toString() : null);
123             case IS_UPPERCASE:
124                 return isUpperCase(leftValue != null ? leftValue.toString() : null);
125             case IS_NOT_UPPERCASE:
126                 return !isUpperCase(leftValue != null ? leftValue.toString() : null);
127         }
128         
129         throw new IllegalStateException("Operator class [" + getOperator().getClass().getName() + "] not supported!");
130     }
131 
132     /**
133      * Checks if the value is a word.
134      */
135     private boolean isWord(String value) {
136         return StringUtils.isAlphanumeric(value);
137     }
138 
139     /**
140      * Checks if the value is lower case.
141      */
142     private boolean isLowerCase(String value) {
143         return (value.length() > 0 && value.toLowerCase().equals(value));
144     }
145 
146     /**
147      * Checks if the value is upper case.
148      */
149     private boolean isUpperCase(String value) {
150         return value.length() > 0 && value.toUpperCase().equals(value);
151     }
152 
153     /**
154      * Checks if the value is blank.
155      */
156     private boolean isBlank(String value) {
157         return StringUtils.isBlank(value);
158     }
159     
160 }