KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > web > cache > mapping > ValueConstraint


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.appserv.web.cache.mapping;
25
26 import java.text.MessageFormat JavaDoc;
27
28 import java.util.logging.Logger JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30
31 import com.sun.enterprise.web.logging.pwc.LogDomains;
32
33 /** ValueConstraint class represents a field's value constraint;
34  * supports common matching expressions.
35  */

36 public class ValueConstraint {
37
38     // PWC_LOGGER
39
private static Logger JavaDoc _logger;
40     /**
41      * The resource bundle containing the localized message strings.
42      */

43     private static ResourceBundle JavaDoc _rb = null;
44
45     // field values to match
46
private String JavaDoc matchValue = null;
47     private float minValue = Float.MIN_VALUE;
48     private float maxValue = Float.MAX_VALUE;
49
50     // the default match expr
51
private int matchExpr = Constants.MATCH_EQUALS;
52
53     // string representation of this expr and the object
54
private String JavaDoc str;
55
56     // whether to cache if there was a match
57
private boolean cacheOnMatch = true;
58     // whether to cache if there was a failure to match
59
private boolean cacheOnMatchFailure = false;
60
61     /** create constraint: field value matches with the given string expression
62      * @param value specific value to match
63      * @param expr match expression
64      */

65     public ValueConstraint(String JavaDoc value, String JavaDoc expr)
66                                 throws IllegalArgumentException JavaDoc {
67         int match;
68
69         // web container logger
70
_logger = LogDomains.getLogger(LogDomains.PWC_LOGGER);
71         _rb = _logger.getResourceBundle();
72
73         if (expr == null || expr.equals("equals")) {
74             match = Constants.MATCH_EQUALS;
75         } else if (expr.equals("greater")) {
76             match = Constants.MATCH_GREATER;
77             try {
78                 minValue = Float.parseFloat(value);
79             } catch (NumberFormatException JavaDoc nfe) {
80                 String JavaDoc msg = _rb.getString("cache.mapping.greaterExpRequiresNumeric");
81                 Object JavaDoc[] params = { value };
82                 msg = MessageFormat.format(msg, params);
83
84                 throw new IllegalArgumentException JavaDoc(msg);
85             }
86         }
87         else if (expr.equals("lesser")) {
88             match = Constants.MATCH_LESSER;
89             try {
90                 maxValue = Float.parseFloat(value);
91             } catch (NumberFormatException JavaDoc nfe) {
92                 String JavaDoc msg = _rb.getString("cache.mapping.lesserExpRequiresNumeric");
93                 Object JavaDoc[] params = { value };
94                 msg = MessageFormat.format(msg, params);
95
96                 throw new IllegalArgumentException JavaDoc(msg);
97             }
98         }
99         else if (expr.equals("not-equals"))
100             match = Constants.MATCH_NOT_EQUALS;
101         else if (expr.equals("in-range")) {
102             match = Constants.MATCH_IN_RANGE;
103             parseRangeValue(value);
104         }
105         else {
106             String JavaDoc msg = _rb.getString("cache.mapping.illegalValueExpr");
107             Object JavaDoc[] params = { value, expr };
108             msg = MessageFormat.format(msg, params);
109
110             throw new IllegalArgumentException JavaDoc(msg);
111         }
112
113         this.matchExpr = match;
114         this.matchValue = value;
115     }
116
117     /**
118      * match the given a range of values
119      * @param value range 2034 - 4156
120      * @throws IllegalArgumentException
121      * check for numeric values
122      * check if the first value is lesser than the next
123      * at a minimum, there should be separator and one digit min and
124      * max values("m-n").
125      *
126      * XXX: check how this handles negative ranges: -1230 - -124.
127      */

128     private void parseRangeValue(String JavaDoc value) throws IllegalArgumentException JavaDoc {
129         float val1, val2;
130
131         if (value == null || value.length() <= 2) {
132             String JavaDoc msg = _rb.getString("cache.mapping.illegalValueRange");
133             Object JavaDoc[] params = { value };
134             msg = MessageFormat.format(msg, params);
135
136             throw new IllegalArgumentException JavaDoc(msg);
137         }
138
139         // get the separator first
140
int separator;
141         if (value.charAt(0) == '-') {
142            separator = value.indexOf('-', 1);
143         } else {
144            separator = value.indexOf('-', 0);
145         }
146         if (separator == -1) {
147             String JavaDoc msg = _rb.getString("cache.mapping.missingRangeSep");
148             Object JavaDoc[] params = { value };
149             msg = MessageFormat.format(msg, params);
150
151             throw new IllegalArgumentException JavaDoc(msg);
152         }
153
154         // get the first value
155
String JavaDoc sval1 = value.substring(0, separator).trim();
156         try {
157             val1 = Float.parseFloat(sval1);
158         } catch (NumberFormatException JavaDoc nfe) {
159             String JavaDoc msg = _rb.getString("cache.mapping.lowerRangeRequiresNumber");
160             Object JavaDoc[] params = { sval1 };
161             msg = MessageFormat.format(msg, params);
162
163             throw new IllegalArgumentException JavaDoc(msg);
164         }
165
166         // is max value specified at all?
167
if (separator == value.length()){
168             String JavaDoc msg = _rb.getString("cache.mapping.rangeRequiresUpperBound");
169             Object JavaDoc[] params = { value };
170             msg = MessageFormat.format(msg, params);
171
172             throw new IllegalArgumentException JavaDoc(msg);
173         }
174
175         String JavaDoc sval2 = value.substring(separator + 1).trim();
176         try {
177             val2 = Float.parseFloat(sval2);
178         } catch (NumberFormatException JavaDoc nfe) {
179             String JavaDoc msg = _rb.getString("cache.mapping.upperRangeRequiresNumber");
180             Object JavaDoc[] params = { sval2 };
181             msg = MessageFormat.format(msg, params);
182
183             throw new IllegalArgumentException JavaDoc(msg);
184         }
185
186         this.minValue = val1;
187         this.maxValue = val2;
188     }
189
190     /** set value for this constraint
191      * @param value specific value to match
192      */

193     public void setValue(String JavaDoc value) {
194         this.matchValue = value;
195     }
196
197     /** set minimum value
198      * @param value minimum value
199      */

200     public void setMinValue(float value) {
201         this.minValue = value;
202     }
203
204     /** set the maximum value
205      * @param value maximum value
206      */

207     public void setMaxValue(float value) {
208         this.maxValue = value;
209     }
210
211     /** set field matching expression
212      * @param expr match expression
213      */

214     public void setMatchExpr(int expr) {
215         this.matchExpr = expr;
216     }
217
218     /** set whether to cache if there was a match
219      * @param cacheOnMatch should the field value match, enable cache?
220      */

221     public void setCacheOnMatch(boolean cacheOnMatch) {
222         this.cacheOnMatch = cacheOnMatch;
223     }
224
225     /** set whether to cache if there was a failure to match
226      * @param cacheOnMatchFailure should the field value doesn't match,
227      * enable cache?
228      */

229     public void setCacheOnMatchFailure(boolean cacheOnMatchFailure) {
230         this.cacheOnMatchFailure = cacheOnMatchFailure;
231     }
232
233     /** match with the given <code>Object</code> value.
234      * @return <code>true</code> if the value passes the constraint,
235      * <code>false</code> otherwise.
236      */

237     public boolean matches(Object JavaDoc value) {
238         boolean result = false;
239         switch (matchExpr) {
240             case Constants.MATCH_EQUALS:
241                 result = matchValue.equals(value);
242                 break;
243             case Constants.MATCH_NOT_EQUALS:
244                 result = !(matchValue.equals(value));
245                 break;
246             case Constants.MATCH_GREATER:
247                 // convert to a Float type
248
{
249                     Float JavaDoc lval = new Float JavaDoc(value.toString());
250                     result = (lval.floatValue() > minValue);
251                 }
252                 break;
253             case Constants.MATCH_LESSER:
254                 // convert to a Float type
255
{
256                     Float JavaDoc lval = new Float JavaDoc(value.toString());
257                     result = (lval.floatValue() < maxValue);
258                 }
259                 break;
260             case Constants.MATCH_IN_RANGE:
261                 // convert to a Float type
262
{
263                     Float JavaDoc lval = new Float JavaDoc(value.toString());
264                     result = (lval.floatValue() >= minValue &&
265                                     lval.floatValue() <= maxValue);
266                 }
267                 break;
268         }
269
270         // did it match?
271
return (result == true) ? cacheOnMatch : cacheOnMatchFailure;
272     }
273
274     /**
275      * @return a string representation of this value/expr element
276      */

277     public String JavaDoc toString() {
278         if (str == null) {
279             str = "match value = " + matchValue + " match expr = " + Constants.EXPR_NAMES[matchExpr];
280         }
281         return str;
282     }
283 }
284
Popular Tags