KickJava   Java API By Example, From Geeks To Geeks.

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


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  * RangeConstraint is a {@link Constraint} to validate a
31  * numeric element against the given range.
32  * It implements <code>Constraint</code> interface and extends
33  * {@link ConstraintUtils} class.
34  * <code>match</code> method of this object returns an empty
35  * <code>Collection</code> if the value being validated belongs to the range
36  * represented by this <code>Constraint</code>; 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 RangeConstraint extends ConstraintUtils
45                 implements Constraint {
46
47     /**
48      * A start value of the range represented by
49      * this <code>Constraint</code>.
50      */

51     private Double JavaDoc startValue = null;
52
53     /**
54      * An end value of the range represented by
55      * this <code>Constraint</code>.
56      */

57     private Double JavaDoc endValue = null;
58
59
60     /** Creates a new instance of RangeConstraint */
61     public RangeConstraint() {
62         startValue = null;
63         endValue = null;
64     }
65
66
67     /** Creates a new instance of <code>RangeConstraint</code>. */
68     public RangeConstraint(String JavaDoc startValue, String JavaDoc endValue) {
69         try {
70            this.startValue = new Double JavaDoc(startValue);
71            this.endValue = new Double JavaDoc(endValue);
72         } catch(NumberFormatException JavaDoc e) {
73             String JavaDoc format =
74                 BundleReader.getValue("Error_failed_to_create"); //NOI18N
75
Object JavaDoc[] arguments =
76                 new Object JavaDoc[]{"RangeConstaint"}; //NOI18N
77

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

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

124     public void setRangeStart(String JavaDoc value){
125         try {
126            startValue = new Double JavaDoc(value);
127         } catch(NumberFormatException JavaDoc e) {
128             String JavaDoc format =
129                 BundleReader.getValue("Error_failed_to_set"); //NOI18N
130
Object JavaDoc[] arguments =
131                 new Object JavaDoc[]{this.toString(), "Range Start"}; //NOI18N
132

133             System.out.println(MessageFormat.format(format, arguments));
134         }
135     }
136
137
138     /**
139      * Sets the end value of the range represented by this object.
140      *
141      * @param value the value to be set as the end value of the
142      * range represented by this object.
143      */

144     public void setRangeEnd(String JavaDoc value){
145         try {
146            endValue = new Double JavaDoc(value);
147         } catch(NumberFormatException JavaDoc e) {
148             String JavaDoc format =
149                 BundleReader.getValue("Error_failed_to_set"); //NOI18N
150
Object JavaDoc[] arguments =
151                 new Object JavaDoc[]{this.toString(), "Range End"}; //NOI18N
152

153             System.out.println(MessageFormat.format(format, arguments));
154         }
155     }
156
157
158     /**
159      * Prints this <code>Constraint</code>.
160      */

161     public void print() {
162         super.print();
163         
164         String JavaDoc format = BundleReader.getValue("Name_Value_Pair_Format");//NOI18N
165
Object JavaDoc[] arguments =
166             new Object JavaDoc[]{"Range Start", startValue}; //NOI18N
167
System.out.println(MessageFormat.format(format, arguments));
168
169         arguments = new Object JavaDoc[]{"Range End", endValue}; //NOI18N
170
System.out.println(MessageFormat.format(format, arguments));
171     }
172
173
174     /**
175      * Sets the start value of the range represented by this object.
176      *
177      * @param value the value to be set as the start value of the
178      * range represented by this object.
179      */

180     public void setRangeStart(Double JavaDoc value){
181            startValue = value;
182     }
183
184
185     /**
186      * Sets the end value of the range represented by this object.
187      *
188      * @param value the value to be set as the end value of the
189      * range represented by this object.
190      */

191     public void setRangeEnd(Double JavaDoc value){
192         endValue = value;
193     }
194     
195     
196     private void addFailure(Collection JavaDoc failed_constrained_list,
197         String JavaDoc name, String JavaDoc value){
198         String JavaDoc failureMessage = formatFailureMessage(toString(), value, name);
199         
200         String JavaDoc format = BundleReader.getValue(
201             "MSG_RangeConstraint_Failure"); //NOI18N
202
String JavaDoc range = startValue + " - " + endValue;
203         Object JavaDoc[] arguments = new Object JavaDoc[]{range};
204         String JavaDoc genericFailureMessage =
205             MessageFormat.format(format, arguments);
206
207         failed_constrained_list.add(new ConstraintFailure(toString(),
208             value, name, failureMessage, genericFailureMessage));
209     }
210 }
211
Popular Tags