KickJava   Java API By Example, From Geeks To Geeks.

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


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  * RangeConstraint.java April 16, 2003, 6:22 PM
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 import java.text.MessageFormat JavaDoc;
34
35 import com.sun.enterprise.tools.common.validation.constraints.ConstraintFailure;
36 import com.sun.enterprise.tools.common.validation.util.BundleReader;
37
38 /**
39  * RangeConstraint is a {@link Constraint} to validate a
40  * numeric element against the given range.
41  * It implements <code>Constraint</code> interface and extends
42  * {@link ConstraintUtils} class.
43  * <code>match</code> method of this object returns an empty
44  * <code>Collection</code> if the value being validated belongs to the range
45  * represented by this <code>Constraint</code>; else it returns a
46  * <code>Collection</code> with a {@link ConstraintFailure} object in it.
47  * <code>ConstraintUtils</code> class provides utility methods for formating
48  * failure messages and a <code>print<method> method to print this object.
49  *
50  * @author Rajeshwar Patil
51  * @version %I%, %G%
52  */

53 public class RangeConstraint extends ConstraintUtils
54                 implements Constraint {
55
56     /**
57      * A start value of the range represented by
58      * this <code>Constraint</code>.
59      */

60     private Double JavaDoc startValue = null;
61
62     /**
63      * An end value of the range represented by
64      * this <code>Constraint</code>.
65      */

66     private Double JavaDoc endValue = null;
67
68
69     /** Creates a new instance of RangeConstraint */
70     public RangeConstraint() {
71         startValue = null;
72         endValue = null;
73     }
74
75
76     /** Creates a new instance of <code>RangeConstraint</code>. */
77     public RangeConstraint(String JavaDoc startValue, String JavaDoc endValue) {
78         try {
79            this.startValue = new Double JavaDoc(startValue);
80            this.endValue = new Double JavaDoc(endValue);
81         } catch(NumberFormatException JavaDoc e) {
82             String JavaDoc format =
83                 BundleReader.getValue("Error_failed_to_create"); //NOI18N
84
Object JavaDoc[] arguments =
85                 new Object JavaDoc[]{"RangeConstaint"}; //NOI18N
86

87             System.out.println(MessageFormat.format(format, arguments));
88         }
89     }
90
91
92     /**
93      * Validates the given value against this <code>Constraint</code>.
94      *
95      * @param value the value to be validated.
96      * @param name the element name, value of which is being validated.
97      * It is used only in case of <code>Constraint</code> failure, to
98      * construct the failure message.
99      *
100      * @return <code>Collection</code> the Collection of
101      * <code>ConstraintFailure</code> Objects. Collection is empty
102      * if there are no failures. This method will fail, if the given value
103      * is non-numeric or its numeric & does not belong to the range
104      * represented by this object.
105      */

106     public Collection JavaDoc match(String JavaDoc value, String JavaDoc name) {
107         ArrayList JavaDoc failed_constrained_list = new ArrayList JavaDoc();
108         
109         if((startValue == null) || (endValue == null)) {
110             return failed_constrained_list;
111         }
112         
113         if((value != null) && (value.length() != 0)){
114             try {
115                 Double JavaDoc val = new Double JavaDoc(value);
116                 if((val.compareTo(startValue) < 0) ||
117                     (val.compareTo(endValue) > 0)){
118                     addFailure(failed_constrained_list, name, value);
119                 }
120             } catch(NumberFormatException JavaDoc e) {
121                 addFailure(failed_constrained_list, name, value); }
122         }
123         return failed_constrained_list;
124     }
125
126
127     /**
128      * Sets the start value of the range represented by this object.
129      *
130      * @param value the value to be set as the start value of the
131      * range represented by this object.
132      */

133     public void setRangeStart(String JavaDoc value){
134         try {
135            startValue = new Double JavaDoc(value);
136         } catch(NumberFormatException JavaDoc e) {
137             String JavaDoc format =
138                 BundleReader.getValue("Error_failed_to_set"); //NOI18N
139
Object JavaDoc[] arguments =
140                 new Object JavaDoc[]{this.toString(), "Range Start"}; //NOI18N
141

142             System.out.println(MessageFormat.format(format, arguments));
143         }
144     }
145
146
147     /**
148      * Sets the end value of the range represented by this object.
149      *
150      * @param value the value to be set as the end value of the
151      * range represented by this object.
152      */

153     public void setRangeEnd(String JavaDoc value){
154         try {
155            endValue = new Double JavaDoc(value);
156         } catch(NumberFormatException JavaDoc e) {
157             String JavaDoc format =
158                 BundleReader.getValue("Error_failed_to_set"); //NOI18N
159
Object JavaDoc[] arguments =
160                 new Object JavaDoc[]{this.toString(), "Range End"}; //NOI18N
161

162             System.out.println(MessageFormat.format(format, arguments));
163         }
164     }
165
166
167     /**
168      * Prints this <code>Constraint</code>.
169      */

170     public void print() {
171         super.print();
172         
173         String JavaDoc format = BundleReader.getValue("Name_Value_Pair_Format");//NOI18N
174
Object JavaDoc[] arguments =
175             new Object JavaDoc[]{"Range Start", startValue}; //NOI18N
176
System.out.println(MessageFormat.format(format, arguments));
177
178         arguments = new Object JavaDoc[]{"Range End", endValue}; //NOI18N
179
System.out.println(MessageFormat.format(format, arguments));
180     }
181
182
183     /**
184      * Sets the start value of the range represented by this object.
185      *
186      * @param value the value to be set as the start value of the
187      * range represented by this object.
188      */

189     public void setRangeStart(Double JavaDoc value){
190            startValue = value;
191     }
192
193
194     /**
195      * Sets the end value of the range represented by this object.
196      *
197      * @param value the value to be set as the end value of the
198      * range represented by this object.
199      */

200     public void setRangeEnd(Double JavaDoc value){
201         endValue = value;
202     }
203     
204     
205     private void addFailure(Collection JavaDoc failed_constrained_list,
206         String JavaDoc name, String JavaDoc value){
207         String JavaDoc failureMessage = formatFailureMessage(toString(), value, name);
208         
209         String JavaDoc format = BundleReader.getValue(
210             "MSG_RangeConstraint_Failure"); //NOI18N
211
String JavaDoc range = startValue + " - " + endValue;
212         Object JavaDoc[] arguments = new Object JavaDoc[]{range};
213         String JavaDoc genericFailureMessage =
214             MessageFormat.format(format, arguments);
215
216         failed_constrained_list.add(new ConstraintFailure(toString(),
217             value, name, failureMessage, genericFailureMessage));
218     }
219 }
220
Popular Tags