KickJava   Java API By Example, From Geeks To Geeks.

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


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

48 public class IntegerGreaterThanConstraint extends ConstraintUtils
49         implements Constraint {
50     
51     /**
52      * A value represented by this <code>Constraint</code>.
53      */

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

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

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

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

123             System.out.println(MessageFormat.format(format, arguments));
124         }
125     }
126
127
128     /**
129      * Sets the value represented by this object.
130      *
131      * @param value the value represented by this object.
132      */

133     public void setValue(Integer JavaDoc value){
134         this.value = value.intValue();
135     }
136
137
138     /**
139      * Prints this <code>Constraint</code>.
140      */

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