KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > rules > MinSize


1 package org.sapia.validator.rules;
2
3 import org.sapia.validator.BeanRule;
4 import java.util.Collection JavaDoc;
5
6 /**
7  * Insures that the size/length of a collection or array is greater or equal
8  * to a given minimum.
9  *
10  * @author Yanick Duchesne
11  * <dl>
12  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
13  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
14  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
15  * </dl>
16  */

17 public class MinSize extends BeanRule {
18   private int _size;
19
20   /**
21    * Constructor for MinSize.
22    */

23   public MinSize() {
24     super.throwExceptionOnNull(true);
25   }
26
27   /**
28    * Sets the minimum size of collections/arrays that
29    * are validated by an instance of this class.
30    *
31    * @param size a size.
32    */

33   public void setSize(int size) {
34     _size = size;
35   }
36
37   /**
38    * @see org.sapia.validator.Rule#validate(ValidationContext)
39    */

40   public boolean doValidate(Object JavaDoc obj) {
41     if (obj instanceof Object JavaDoc[]) {
42       return ((Object JavaDoc[]) obj).length >= _size;
43     } else if (obj instanceof Collection JavaDoc) {
44       return ((Collection JavaDoc) obj).size() >= _size;
45     } else {
46       throw new IllegalArgumentException JavaDoc(
47         "minSize only processes java.util.Collection instances or arrays " + qualifiedName());
48     }
49   }
50 }
51
Popular Tags