KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > validation > constraints > ZeroToMaxIntegerConstraint


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

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

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

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