KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
25  * NumberConstraint.java March 24, 2003, 11:04 AM
26  *
27  */

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

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

72     public Collection JavaDoc match(String JavaDoc value, String JavaDoc name) {
73         ArrayList JavaDoc failed_constrained_list = new ArrayList JavaDoc();
74         if(null != value){
75             try {
76                 new Double JavaDoc(value);
77                 return failed_constrained_list;
78             } catch(NumberFormatException JavaDoc e) {
79                 String JavaDoc failureMessage = formatFailureMessage(toString(), value,
80                     name);
81
82                 failed_constrained_list.add(new ConstraintFailure(toString(),
83                     value, name, failureMessage, BundleReader.getValue(
84                         "MSG_NumberConstraint_Failure"))); //NOI18N
85
}
86         }
87         return failed_constrained_list;
88     }
89 }
90
Popular Tags