KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > Validate


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
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 package org.apache.commons.lang;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * <p>Assists in validating arguments.</p>
24  *
25  * <p>The class is based along the lines of JUnit. If an argument value is
26  * deemed invalid, an IllegalArgumentException is thrown. For example:</p>
27  *
28  * <pre>
29  * Validate.isTrue( i > 0, "The value must be greater than zero: ", i);
30  * Validate.notNull( surname, "The surname must not be null");
31  * </pre>
32  *
33  * @author <a HREF="mailto:ola.berg@arkitema.se">Ola Berg</a>
34  * @author Stephen Colebourne
35  * @author Gary Gregory
36  * @author Norm Deane
37  * @since 2.0
38  * @version $Id: Validate.java 165657 2005-05-02 18:31:49Z ggregory $
39  */

40 public class Validate {
41     // Validate has no dependencies on other classes in Commons Lang at present
42

43     /**
44      * Constructor. This class should not normally be instantiated.
45      */

46     public Validate() {
47     }
48     
49     // isTrue
50
//---------------------------------------------------------------------------------
51

52     /**
53      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
54      * if the test result is <code>false</code>.</p>
55      *
56      * <p>This is used when validating according to an arbitrary boolean expression,
57      * such as validating a primitive number or using your own custom validation
58      * expression.</p>
59      *
60      * <pre>
61      * Validate.isTrue( myObject.isOk(), "The object is not OK: ", myObject);
62      * </pre>
63      *
64      * <p>For performance reasons, the object is passed as a separate parameter and
65      * appended to the message string only in the case of an error.</p>
66      *
67      * @param expression a boolean expression
68      * @param message the exception message you would like to see if the
69      * expression is <code>false</code>
70      * @param value the value to append to the message in case of error
71      * @throws IllegalArgumentException if expression is <code>false</code>
72      */

73     public static void isTrue(boolean expression, String JavaDoc message, Object JavaDoc value) {
74         if (expression == false) {
75             throw new IllegalArgumentException JavaDoc(message + value);
76         }
77     }
78
79     /**
80      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
81      * if the test result is <code>false</code>.</p>
82      *
83      * <p>This is used when validating according to an arbitrary boolean expression,
84      * such as validating a primitive number or using your own custom validation
85      * expression.</p>
86      *
87      * <pre>
88      * Validate.isTrue( i > 0, "The value must be greater than zero: ", i);
89      * </pre>
90      *
91      * <p>For performance reasons, the long value is passed as a separate parameter and
92      * appended to the message string only in the case of an error.</p>
93      *
94      * @param expression a boolean expression
95      * @param message the exception message you would like to see if the expression is <code>false</code>
96      * @param value the value to append to the message in case of error
97      * @throws IllegalArgumentException if expression is <code>false</code>
98      */

99     public static void isTrue(boolean expression, String JavaDoc message, long value) {
100         if (expression == false) {
101             throw new IllegalArgumentException JavaDoc(message + value);
102         }
103     }
104
105     /**
106      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
107      * if the test result is <code>false</code>.</p>
108      *
109      * <p>This is used when validating according to an arbitrary boolean expression,
110      * such as validating a primitive number or using your own custom validation
111      * expression.</p>
112      *
113      * <pre>
114      * Validate.isTrue( d > 0.0, "The value must be greater than zero: ", d);
115      * </pre>
116      *
117      * <p>For performance reasons, the double value is passed as a separate parameter and
118      * appended to the message string only in the case of an error.</p>
119      *
120      * @param expression a boolean expression
121      * @param message the exception message you would like to see if the expression
122      * is <code>false</code>
123      * @param value the value to append to the message in case of error
124      * @throws IllegalArgumentException if expression is <code>false</code>
125      */

126     public static void isTrue(boolean expression, String JavaDoc message, double value) {
127         if (expression == false) {
128             throw new IllegalArgumentException JavaDoc(message + value);
129         }
130     }
131
132     /**
133      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
134      * if the test result is <code>false</code>.</p>
135      *
136      * <p>This is used when validating according to an arbitrary boolean expression,
137      * such as validating a primitive number or using your own custom validation
138      * expression.</p>
139      *
140      * <pre>
141      * Validate.isTrue( (i > 0), "The value must be greater than zero");
142      * Validate.isTrue( myObject.isOk(), "The object is not OK");
143      * </pre>
144      *
145      * <p>For performance reasons, the message string should not involve a string append,
146      * instead use the {@link #isTrue(boolean, String, Object)} method.</p>
147      *
148      * @param expression a boolean expression
149      * @param message the exception message you would like to see if the expression
150      * is <code>false</code>
151      * @throws IllegalArgumentException if expression is <code>false</code>
152      */

153     public static void isTrue(boolean expression, String JavaDoc message) {
154         if (expression == false) {
155             throw new IllegalArgumentException JavaDoc(message);
156         }
157     }
158
159     /**
160      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
161      * if the test result is <code>false</code>.</p>
162      *
163      * <p>This is used when validating according to an arbitrary boolean expression,
164      * such as validating a primitive number or using your own custom validation
165      * expression.</p>
166      *
167      * <pre>
168      * Validate.isTrue( i > 0 );
169      * Validate.isTrue( myObject.isOk() );
170      * </pre>
171      *
172      * <p>The message in the exception is 'The validated expression is false'.</p>
173      *
174      * @param expression a boolean expression
175      * @throws IllegalArgumentException if expression is <code>false</code>
176      */

177     public static void isTrue(boolean expression) {
178         if (expression == false) {
179             throw new IllegalArgumentException JavaDoc("The validated expression is false");
180         }
181     }
182
183     // notNull
184
//---------------------------------------------------------------------------------
185

186     /**
187      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
188      * if the argument is <code>null</code>.</p>
189      *
190      * <pre>
191      * Validate.notNull(myObject, "The object must not be null");
192      * </pre>
193      *
194      * @param object the object to check is not <code>null</code>
195      * @param message the exception message you would like to see
196      * if the object is <code>null</code>
197      * @throws IllegalArgumentException if the object is <code>null</code>
198      */

199     public static void notNull(Object JavaDoc object, String JavaDoc message) {
200         if (object == null) {
201             throw new IllegalArgumentException JavaDoc(message);
202         }
203     }
204
205     /**
206      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
207      * if the argument is <code>null</code>.</p>
208      *
209      * <pre>
210      * Validate.notNull(myObject);
211      * </pre>
212      *
213      * <p>The message in the exception is 'The validated object is null'.</p>
214      *
215      * @param object the object to check is not <code>null</code>
216      * @throws IllegalArgumentException if the object is <code>null</code>
217      */

218     public static void notNull(Object JavaDoc object) {
219         if (object == null) {
220             throw new IllegalArgumentException JavaDoc("The validated object is null");
221         }
222     }
223
224     // notEmpty array
225
//---------------------------------------------------------------------------------
226

227     /**
228      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
229      * if the argument array is empty (<code>null</code> or no elements).</p>
230      *
231      * <pre>
232      * Validate.notEmpty(myArray, "The array must not be empty");
233      * </pre>
234      *
235      * @param array the array to check is not empty
236      * @param message the exception message you would like to see if the array is empty
237      * @throws IllegalArgumentException if the array is empty
238      */

239     public static void notEmpty(Object JavaDoc[] array, String JavaDoc message) {
240         if (array == null || array.length == 0) {
241             throw new IllegalArgumentException JavaDoc(message);
242         }
243     }
244
245     /**
246      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
247      * if the argument array is empty (<code>null</code> or no elements).</p>
248      *
249      * <pre>
250      * Validate.notEmpty(myArray);
251      * </pre>
252      *
253      * <p>The message in the exception is 'The validated array is empty'.
254      *
255      * @param array the array to check is not empty
256      * @throws IllegalArgumentException if the array is empty
257      */

258     public static void notEmpty(Object JavaDoc[] array) {
259         if (array == null || array.length == 0) {
260             throw new IllegalArgumentException JavaDoc("The validated array is empty");
261         }
262     }
263
264     // notEmpty collection
265
//---------------------------------------------------------------------------------
266

267     /**
268      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
269      * if the argument Collection is empty (<code>null</code> or no elements).</p>
270      *
271      * <pre>
272      * Validate.notEmpty(myCollection, "The collection must not be empty");
273      * </pre>
274      *
275      * @param collection the collection to check is not empty
276      * @param message the exception message you would like to see if the collection is empty
277      * @throws IllegalArgumentException if the collection is empty
278      */

279     public static void notEmpty(Collection JavaDoc collection, String JavaDoc message) {
280         if (collection == null || collection.size() == 0) {
281             throw new IllegalArgumentException JavaDoc(message);
282         }
283     }
284
285     /**
286      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
287      * if the argument Collection is empty (<code>null</code> or no elements).</p>
288      *
289      * <pre>
290      * Validate.notEmpty(myCollection);
291      * </pre>
292      *
293      * <p>The message in the exception is 'The validated collection is empty'.</p>
294      *
295      * @param collection the collection to check is not empty
296      * @throws IllegalArgumentException if the collection is empty
297      */

298     public static void notEmpty(Collection JavaDoc collection) {
299         if (collection == null || collection.size() == 0) {
300             throw new IllegalArgumentException JavaDoc("The validated collection is empty");
301         }
302     }
303
304     // notEmpty map
305
//---------------------------------------------------------------------------------
306

307     /**
308      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
309      * if the argument Map is empty (<code>null</code> or no elements).</p>
310      *
311      * <pre>
312      * Validate.notEmpty(myMap, "The map must not be empty");
313      * </pre>
314      *
315      * @param map the map to check is not empty
316      * @param message the exception message you would like to see if the map is empty
317      * @throws IllegalArgumentException if the map is empty
318      */

319     public static void notEmpty(Map JavaDoc map, String JavaDoc message) {
320         if (map == null || map.size() == 0) {
321             throw new IllegalArgumentException JavaDoc(message);
322         }
323     }
324
325     /**
326      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
327      * if the argument Map is empty (<code>null</code> or no elements).</p>
328      *
329      * <pre>
330      * Validate.notEmpty(myMap);
331      * </pre>
332      *
333      * <p>The message in the exception is 'The validated map is empty'.</p>
334      *
335      * @param map the map to check is not empty
336      * @throws IllegalArgumentException if the map is empty
337      */

338     public static void notEmpty(Map JavaDoc map) {
339         if (map == null || map.size() == 0) {
340             throw new IllegalArgumentException JavaDoc("The validated map is empty");
341         }
342     }
343
344     // notEmpty string
345
//---------------------------------------------------------------------------------
346

347     /**
348      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
349      * if the argument String is empty (<code>null</code> or zero length).</p>
350      *
351      * <pre>
352      * Validate.notEmpty(myString, "The string must not be empty");
353      * </pre>
354      *
355      * @param string the string to check is not empty
356      * @param message the exception message you would like to see if the string is empty
357      * @throws IllegalArgumentException if the string is empty
358      */

359     public static void notEmpty(String JavaDoc string, String JavaDoc message) {
360         if (string == null || string.length() == 0) {
361             throw new IllegalArgumentException JavaDoc(message);
362         }
363     }
364
365     /**
366      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
367      * if the argument String is empty (<code>null</code> or zero length).</p>
368      *
369      * <pre>
370      * Validate.notEmpty(myString);
371      * </pre>
372      *
373      * <p>The message in the exception is 'The validated string is empty'.</p>
374      *
375      * @param string the string to check is not empty
376      * @throws IllegalArgumentException if the string is empty
377      */

378     public static void notEmpty(String JavaDoc string) {
379         if (string == null || string.length() == 0) {
380             throw new IllegalArgumentException JavaDoc("The validated string is empty");
381         }
382     }
383
384     // notNullElements array
385
//---------------------------------------------------------------------------------
386

387     /**
388      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
389      * if the argument array has <code>null</code> elements or is
390      * <code>null</code>.</p>
391      *
392      * <pre>
393      * Validate.noNullElements(myArray, "The array must not contain null elements");
394      * </pre>
395      *
396      * <p>If the array is null then the message in the exception is 'The validated object is null'.</p>
397      *
398      * @param array the array to check
399      * @param message the exception message if the array has
400      * <code>null</code> elements
401      * @throws IllegalArgumentException if the array has <code>null</code>
402      * elements or is <code>null</code>
403      */

404     public static void noNullElements(Object JavaDoc[] array, String JavaDoc message) {
405         Validate.notNull(array);
406         for (int i = 0; i < array.length; i++) {
407             if (array[i] == null) {
408                 throw new IllegalArgumentException JavaDoc(message);
409             }
410         }
411     }
412
413     /**
414      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
415      * if the argument array has <code>null</code> elements or is
416      * <code>null</code>.</p>
417      *
418      * <pre>
419      * Validate.noNullElements(myArray);
420      * </pre>
421      *
422      * <p>If the array has a null element the message in the exception is
423      * 'The validated array contains null element at index: '.</p>
424      *
425      * <p>If the array is null then the message in the exception is 'The validated object is null'.</p>
426      *
427      * @param array the array to check
428      * @throws IllegalArgumentException if the array has <code>null</code>
429      * elements or is <code>null</code>
430      */

431     public static void noNullElements(Object JavaDoc[] array) {
432         Validate.notNull(array);
433         for (int i = 0; i < array.length; i++) {
434             if (array[i] == null) {
435                 throw new IllegalArgumentException JavaDoc("The validated array contains null element at index: " + i);
436             }
437         }
438     }
439
440     // notNullElements collection
441
//---------------------------------------------------------------------------------
442

443     /**
444      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
445      * if the argument Collection has <code>null</code> elements or is
446      * <code>null</code>.</p>
447      *
448      * <pre>
449      * Validate.noNullElements(myCollection, "The collection must not contain null elements");
450      * </pre>
451      *
452      * <p>If the collection is null then the message in the exception is 'The validated object is null'.</p>
453      *
454      * @param collection the collection to check
455      * @param message the exception message if the collection has
456      * <code>null</code> elements
457      * @throws IllegalArgumentException if the collection has
458      * <code>null</code> elements or is <code>null</code>
459      */

460     public static void noNullElements(Collection JavaDoc collection, String JavaDoc message) {
461         Validate.notNull(collection);
462         for (Iterator JavaDoc it = collection.iterator(); it.hasNext();) {
463             if (it.next() == null) {
464                 throw new IllegalArgumentException JavaDoc(message);
465             }
466         }
467     }
468
469     /**
470      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
471      * if the argument Collection has <code>null</code> elements or is
472      * <code>null</code>.</p>
473      *
474      * <pre>
475      * Validate.noNullElements(myCollection);
476      * </pre>
477      *
478      * <p>The message in the exception is 'The validated collection contains null element at index: '.</p>
479      *
480      * <p>If the collection is null then the message in the exception is 'The validated object is null'.</p>
481      *
482      * @param collection the collection to check
483      * @throws IllegalArgumentException if the collection has
484      * <code>null</code> elements or is <code>null</code>
485      */

486     public static void noNullElements(Collection JavaDoc collection) {
487         Validate.notNull(collection);
488         int i = 0;
489         for (Iterator JavaDoc it = collection.iterator(); it.hasNext(); i++) {
490             if (it.next() == null) {
491                 throw new IllegalArgumentException JavaDoc("The validated collection contains null element at index: " + i);
492             }
493         }
494     }
495
496     /**
497      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
498      * if the argument collection is <code>null</code> or has elements that
499      * are not of type <code>clazz</code> or a subclass.</p>
500      *
501      * <pre>
502      * Validate.allElementsOfType(collection, String.class, "Collection has invalid elements");
503      * </pre>
504      *
505      * @param collection the collection to check, not null
506      * @param clazz the <code>Class</code> which the collection's elements are expected to be, not null
507      * @param message the exception message if the <code>Collection</code> has elements not of type <code>clazz</code>
508      * @since 2.1
509      */

510     public static void allElementsOfType(Collection JavaDoc collection, Class JavaDoc clazz, String JavaDoc message) {
511         Validate.notNull(collection);
512         Validate.notNull(clazz);
513         for (Iterator JavaDoc it = collection.iterator(); it.hasNext(); ) {
514             if (clazz.isInstance(it.next()) == false) {
515                 throw new IllegalArgumentException JavaDoc(message);
516             }
517         }
518     }
519
520     /**
521      * <p>
522      * Validate an argument, throwing <code>IllegalArgumentException</code> if the argument collection is
523      * <code>null</code> or has elements that are not of type <code>clazz</code> or a subclass.
524      * </p>
525      *
526      * <pre>
527      * Validate.allElementsOfType(collection, String.class);
528      * </pre>
529      *
530      * <p>
531      * The message in the exception is 'The validated collection contains an element not of type clazz at index: '.
532      * </p>
533      *
534      * @param collection
535      * the collection to check, not null
536      * @param clazz
537      * the <code>Class</code> which the collection's elements are expected to be, not null
538      * @since 2.1
539      */

540     public static void allElementsOfType(Collection JavaDoc collection, Class JavaDoc clazz) {
541         Validate.notNull(collection);
542         Validate.notNull(clazz);
543         int i = 0;
544         for (Iterator JavaDoc it = collection.iterator(); it.hasNext(); i++) {
545             if (clazz.isInstance(it.next()) == false) {
546                 throw new IllegalArgumentException JavaDoc("The validated collection contains an element not of type "
547                     + clazz.getName() + " at index: " + i);
548             }
549         }
550     }
551
552 }
553
Popular Tags