KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > validation > constraints > ZeroToMaxIntegerConstraint


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.validation.constraints;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 import org.netbeans.modules.j2ee.sun.validation.constraints.ConstraintFailure;
26 import org.netbeans.modules.j2ee.sun.validation.util.BundleReader;
27
28 /**
29  * ZeroToMaxIntegerConstraint is a {@link Constraint} to validate numbers between
30  * Zero to Integer.MAX_VALUE.
31  * It implements <code>Constraint</code> interface and extends
32  * {@link ConstraintUtils} class.
33  * <code>match</code> method of this object returns empty
34  * <code>Collection</code> if the value being validated is a number between Zero
35  * to Integer.MAX_VALUE; else it returns a <code>Collection</code> with a
36  * {@link ConstraintFailure} object in it.
37  * <code>ConstraintUtils</code> class provides utility methods for formating
38  * failure messages and a <code>print<method> method to print this object.
39  *
40  * @author Rajeshwar Patil
41  * @version %I%, %G%
42  */

43 public class ZeroToMaxIntegerConstraint extends ConstraintUtils implements Constraint {
44     
45     /** Creates a new instance of <code>NumberConstraint</code>. */
46     public ZeroToMaxIntegerConstraint() {
47     }
48
49
50     /**
51      * Validates the given value against this <code>Constraint</code>.
52      *
53      * @param value the value to be validated.
54      * @param name the element name, value of which is being validated.
55      * It is used only in case of <code>Constraint</code> failure, to construct
56      * the failure message.
57      *
58      * @return <code>Collection</code> the Collection of
59      * <code>ConstraintFailure</code> Objects. Collection is empty if
60      * there are no failures. This method will fail, if the given value
61      * is not between Zero and Integer.MAX_VALUE.
62      */

63     public Collection JavaDoc match(String JavaDoc value, String JavaDoc name) {
64         ArrayList JavaDoc failed_constrained_list = new ArrayList JavaDoc();
65         if((value != null) && (value.length() != 0)){
66             try {
67                 int intValue = Integer.parseInt(value);
68                 if((intValue < 0) || (intValue > Integer.MAX_VALUE)){
69                     String JavaDoc failureMessage = formatFailureMessage(toString(),
70                         value, name);
71                     failed_constrained_list.add(new ConstraintFailure(toString(),
72                         value, name, failureMessage, BundleReader.getValue(
73                            "MSG_ZeroToMaxIntegerConstraint_Failure"))); //NOI18N
74
}
75                 return failed_constrained_list;
76             } catch(NumberFormatException JavaDoc e) {
77                 String JavaDoc failureMessage = formatFailureMessage(toString(), value,
78                     name);
79                 failed_constrained_list.add(new ConstraintFailure(toString(),
80                     value, name, failureMessage, BundleReader.getValue(
81                         "MSG_NumberConstraint_Failure"))); //NOI18N
82
}
83         }
84         return failed_constrained_list;
85     }
86 }
87
Popular Tags