KickJava   Java API By Example, From Geeks To Geeks.

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


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

44 public class IntegerGreaterThanConstraint extends ConstraintUtils
45         implements Constraint {
46     
47     /**
48      * A value represented by this <code>Constraint</code>.
49      */

50     private int value = -1;
51
52     
53     /** Creates a new instance of <code>IntegerGreaterThanConstraint</code>. */
54     public IntegerGreaterThanConstraint() {
55         value = -1;
56     }
57
58
59     /** Creates a new instance of <code>IntegerGreaterThanConstraint</code>. */
60     public IntegerGreaterThanConstraint(String JavaDoc inputValue) {
61         try {
62            this.value = Integer.parseInt(inputValue);
63         } catch(NumberFormatException JavaDoc e) {
64
65             String JavaDoc format =
66                 BundleReader.getValue("Error_failed_to_create"); //NOI18N
67
Object JavaDoc[] arguments =
68                 new Object JavaDoc[]{"IntegerGreaterThanConstraint"}; //NOI18N
69

70             System.out.println(MessageFormat.format(format, arguments));
71         }
72     }
73
74
75     /**
76      * Validates the given value against this <code>Constraint</code>.
77      *
78      * @param inputValue the value to be validated.
79      * @param name the element name, value of which is being validated.
80      * It is used only in case of <code>Constraint</code> failure, to construct
81      * the failure message.
82      *
83      * @return <code>Collection</code> the Collection of
84      * <code>ConstraintFailure</code> Objects. Collection is empty if
85      * there are no failures. This method will fail, if the given value
86      * is not between the value this Constraint represents and Integer.MAX_VALUE.
87      */

88     public Collection JavaDoc match(String JavaDoc inputValue, String JavaDoc name) {
89         ArrayList JavaDoc failed_constrained_list = new ArrayList JavaDoc();
90         if((inputValue != null) && (inputValue.length() != 0)){
91             try {
92                 int intValue = Integer.parseInt(inputValue);
93                 
94                 if((intValue <= value) || (intValue > Integer.MAX_VALUE)){
95                     addFailure(failed_constrained_list, name, inputValue);
96                 }
97             } catch(NumberFormatException JavaDoc e) {
98                 addFailure(failed_constrained_list, name, inputValue);
99             }
100         }
101         return failed_constrained_list;
102     }
103
104
105     /**
106      * Sets the value represented by this object.
107      *
108      * @param value the value represented by this object.
109      */

110     public void setValue(String JavaDoc value){
111         try {
112            this.value = Integer.parseInt(value);
113         } catch(NumberFormatException JavaDoc e) {
114             String JavaDoc format =
115                 BundleReader.getValue("Error_failed_to_set"); //NOI18N
116
Object JavaDoc[] arguments =
117                 new Object JavaDoc[]{this.toString(), "Value"}; //NOI18N
118

119             System.out.println(MessageFormat.format(format, arguments));
120         }
121     }
122
123
124     /**
125      * Sets the value represented by this object.
126      *
127      * @param value the value represented by this object.
128      */

129     public void setValue(Integer JavaDoc value){
130         this.value = value.intValue();
131     }
132
133
134     /**
135      * Prints this <code>Constraint</code>.
136      */

137     public void print() {
138         super.print();
139
140         String JavaDoc format = BundleReader.getValue("Name_Value_Pair_Format");//NOI18N
141
Object JavaDoc[] arguments =
142             new Object JavaDoc[]{"Value", String.valueOf(value)}; //NOI18N
143
System.out.println(MessageFormat.format(format, arguments));
144     }
145
146
147     private void addFailure(Collection JavaDoc failed_constrained_list,
148         String JavaDoc name, String JavaDoc inputValue){
149         String JavaDoc failureMessage = formatFailureMessage(toString(),
150             inputValue, name);
151
152         String JavaDoc format = BundleReader.getValue(
153             "MSG_IntegerGreaterThanConstraint_Failure"); //NOI18N
154
Object JavaDoc[] arguments =
155             new Object JavaDoc[]{String.valueOf(value)};
156         String JavaDoc genericFailureMessage = MessageFormat.format(format, arguments);
157
158         failed_constrained_list.add(new ConstraintFailure(toString(),
159             inputValue, name, failureMessage, genericFailureMessage));
160     }
161 }
162
Popular Tags