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.util.condition;
18  
19  import java.util.Calendar;
20  import java.util.Collection;
21  import java.util.Date;
22  
23  import org.joda.time.ReadableInstant;
24  import org.springmodules.validation.util.condition.bean.EqualPropertiesBeanCondition;
25  import org.springmodules.validation.util.condition.bean.PropertyBeanCondition;
26  import org.springmodules.validation.util.condition.collection.IsEmptyCollectionCondition;
27  import org.springmodules.validation.util.condition.collection.MaxSizeCollectionCondition;
28  import org.springmodules.validation.util.condition.collection.MinSizeCollectionCondition;
29  import org.springmodules.validation.util.condition.collection.SizeRangeCollectionCondition;
30  import org.springmodules.validation.util.condition.common.*;
31  import org.springmodules.validation.util.condition.date.IsAfterDateCondition;
32  import org.springmodules.validation.util.condition.date.IsBeforeDateCondition;
33  import org.springmodules.validation.util.condition.date.IsInTheFutureDateCondition;
34  import org.springmodules.validation.util.condition.date.IsInThePastDateCondition;
35  import org.springmodules.validation.util.condition.date.jodatime.IsAfterInstantCondition;
36  import org.springmodules.validation.util.condition.date.jodatime.IsBeforeInstantCondition;
37  import org.springmodules.validation.util.condition.date.jodatime.IsInTheFutureInstantCondition;
38  import org.springmodules.validation.util.condition.date.jodatime.IsInThePastInstantCondition;
39  import org.springmodules.validation.util.condition.range.*;
40  import org.springmodules.validation.util.condition.string.*;
41  
42  /**
43   * A static factory class to help with creating the various conditions.
44   *
45   * @author Uri Boness
46   */
47  public final class Conditions {
48  
49      // should never be instantiated
50      private Conditions() {
51      }
52  
53      // common conditions
54  
55      public static Condition not(Condition condition) {
56          return new NotCondition(condition);
57      }
58  
59      public static Condition and(Condition c1, Condition c2) {
60          return and(new Condition[]{c1, c2});
61      }
62  
63      public static Condition and(Condition[] conditions) {
64          return new AndCondition(conditions);
65      }
66  
67      public static Condition and(Collection conditions) {
68          return new AndCondition(conditions);
69      }
70  
71      public static Condition or(Condition c1, Condition c2) {
72          return or(new Condition[]{c1, c2});
73      }
74  
75      public static Condition or(Condition[] conditions) {
76          return new OrCondition(conditions);
77      }
78  
79      public static Condition or(Collection conditions) {
80          return new OrCondition(conditions);
81      }
82  
83      public static Condition isTrue() {
84          return new IsTrueCondition();
85      }
86  
87      public static Condition isFalse() {
88          return not(isTrue());
89      }
90  
91      public static Condition instanceOf(Class clazz) {
92          return new InstanceOfCondition(clazz);
93      }
94  
95      public static Condition isNull() {
96          return new IsNullCondition();
97      }
98  
99      public static Condition notNull() {
100         return not(isNull());
101     }
102 
103     // bean conditions
104 
105     public static Condition propertiesMatch(String[] propertyNames) {
106         return new EqualPropertiesBeanCondition(propertyNames);
107     }
108 
109     // date conditions
110 
111     public static Condition isDateInTheFuture() {
112         return new IsInTheFutureDateCondition();
113     }
114 
115     public static Condition isDateInThePast() {
116         return new IsInThePastDateCondition();
117     }
118 
119     public static Condition isInstantInTheFuture() {
120         return new IsInTheFutureInstantCondition();
121     }
122 
123     public static Condition isInstantInThePast() {
124         return new IsInThePastInstantCondition();
125     }
126 
127     public static Condition isBefore(Date date) {
128         return new IsBeforeDateCondition(date);
129     }
130 
131     public static Condition isBefore(ReadableInstant instant) {
132         return new IsBeforeInstantCondition(instant);
133     }
134 
135     public static Condition isBefore(Calendar calendar) {
136         return new IsBeforeDateCondition(calendar);
137     }
138 
139     public static Condition isAfter(Date date) {
140         return new IsAfterDateCondition(date);
141     }
142 
143     public static Condition isAfter(ReadableInstant instant) {
144         return new IsAfterInstantCondition(instant);
145     }
146 
147     public static Condition isAfter(Calendar calendar) {
148         return new IsAfterDateCondition(calendar);
149     }
150 
151     // string conditions
152 
153     public static Condition contains(String text) {
154         return new ContainsSubstringStringCondition(text);
155     }
156 
157     public static Condition equalsIgnoreCase(String text) {
158         return new EqualsIgnoreCaseStringCondition(text);
159     }
160 
161     public static Condition isEmptyString() {
162         return new IsEmptyStringCondition();
163     }
164 
165     public static Condition notBlank() {
166         return not(isEmptyString());
167     }
168 
169     public static Condition regexp(String regexp) {
170         return new RegExpStringCondition(regexp);
171     }
172 
173     public static Condition minLength(int minLength) {
174         return new MinLengthStringCondition(minLength);
175     }
176 
177     public static Condition maxLength(int maxLength) {
178         return new MaxLengthStringCondition(maxLength);
179     }
180 
181     public static Condition lengthBetween(int minLength, int maxLength) {
182         return minLength(minLength).and(maxLength(maxLength));
183     }
184 
185     // collection & array conditions
186 
187     public static Condition isEmpty() {
188         return new IsEmptyCollectionCondition();
189     }
190 
191     public static Condition notEmpty() {
192         return not(isEmpty());
193     }
194 
195     public static Condition minSize(int minSize) {
196         return new MinSizeCollectionCondition(minSize);
197     }
198 
199     public static Condition minSize(String propertyName, int minSize) {
200         return property(propertyName, minSize(minSize));
201     }
202 
203     public static Condition maxSize(int maxSize) {
204         return new MaxSizeCollectionCondition(maxSize);
205     }
206 
207     public static Condition maxSize(String propertyName, int maxSize) {
208         return property(propertyName, maxSize(maxSize));
209     }
210 
211     public static Condition sizeRange(int minSize, int maxSize) {
212         return new SizeRangeCollectionCondition(minSize, maxSize);
213     }
214 
215     public static Condition sizeRange(String propertyName, int minSize, int maxSize) {
216         return property(propertyName, sizeRange(minSize, maxSize));
217     }
218 
219     // range conditions
220 
221     public static Condition isGt(Comparable min) {
222         return new GreaterThanCondition(min);
223     }
224 
225     public static Condition isGt(String propertyName, Comparable min) {
226         return property(propertyName, isGt(min));
227     }
228 
229     public static Condition isGte(Comparable min) {
230         return new GreaterThanOrEqualsCondition(min);
231     }
232 
233     public static Condition isGte(String propertyName, Comparable min) {
234         return property(propertyName, isGte(min));
235     }
236 
237     public static Condition isLt(Comparable max) {
238         return new LessThanCondition(max);
239     }
240 
241     public static Condition isLte(Comparable max) {
242         return new LessThanOrEqualsCondition(max);
243     }
244 
245     public static Condition isLte(String propertyName, Comparable max) {
246         return property(propertyName, isLte(max));
247     }
248 
249     public static Condition isBetween(Comparable min, Comparable max) {
250         return new BetweenCondition(min, max);
251     }
252 
253     public static Condition isBetweenIncluding(Comparable min, Comparable max) {
254         return new BetweenIncludingCondition(min, max);
255     }
256 
257     public static Condition isBetweenIncludingMin(Comparable min, Comparable max) {
258         return new BetweenIncludingLowerBoundCondition(min, max);
259     }
260 
261     public static Condition isBetweenIncludingMax(Comparable min, Comparable max) {
262         return new BetweenIncludingUpperBoundCondition(min, max);
263     }
264 
265     // property based conditions
266 
267     public static Condition property(String propertyName, Condition condition) {
268         return new PropertyBeanCondition(propertyName, condition);
269     }
270 
271     public static Condition isTrue(String propertyName) {
272         return property(propertyName, isTrue());
273     }
274 
275     public static Condition isFalse(String propertyName) {
276         return not(isTrue(propertyName));
277     }
278 
279     public static Condition isNull(String propertyName) {
280         return property(propertyName, isNull());
281     }
282 
283     public static Condition notNull(String propertyName) {
284         return not(isNull(propertyName));
285     }
286 
287     public static Condition instanceOf(String propertyName, Class clazz) {
288         return property(propertyName, instanceOf(clazz));
289     }
290 
291     public static Condition isEmptyString(String propertyName) {
292         return property(propertyName, isEmptyString());
293     }
294 
295     public static Condition notEmptyString(String propertyName) {
296         return not(isEmptyString(propertyName));
297     }
298 
299     public static Condition maxLength(String propertyName, int maxLength) {
300         return property(propertyName, maxLength(maxLength));
301     }
302 
303     public static Condition minLength(String propertyName, int minLength) {
304         return property(propertyName, minLength(minLength));
305     }
306 
307     public static Condition isEmpty(String propertyName) {
308         return property(propertyName, isEmpty());
309     }
310 
311     public static Condition notEmpty(String propertyName) {
312         return not(isEmpty(propertyName));
313     }
314 
315     public static Condition isBetween(String propertyName, Comparable min, Comparable max) {
316         return property(propertyName, isBetween(min, max));
317     }
318 
319     public static Condition notInBetween(String propertyName, Comparable min, Comparable max) {
320         return not(isBetween(propertyName, min, max));
321     }
322 
323 }