KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.validator.rules;
2
3 import org.sapia.validator.BeanRule;
4
5 import java.util.Collection JavaDoc;
6
7 /**
8  * Insures that the size/length of a collection or array is lower or equal
9  * to a given maximum.
10  *
11  * @author Yanick Duchesne
12  * <dl>
13  * <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>
14  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
15  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
16  * </dl>
17  */

18 public class MaxSize extends BeanRule {
19   private String JavaDoc _attribute;
20   private int _size;
21
22   /**
23    * Constructor for MinSize.
24    */

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

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

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