KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > DjDomainValue


1 /*
2  Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  Redistribution and use in source and binary forms, with or without modification, is permitted
4  provided that the following conditions are met:
5  - Redistributions of source code must retain the above copyright notice, this list of conditions
6  and the following disclaimer.
7  - Redistributions in binary form must reproduce the above copyright notice, this list of
8  conditions and the following disclaimer in the documentation and/or other materials
9  provided with the distribution.
10  - All advertising materials mentioning features or use of this software must display the
11  following acknowledgment: "This product includes Djeneric."
12  - Products derived from this software may not be called "Djeneric" nor may
13  "Djeneric" appear in their names without prior written permission of Genimen BV.
14  - Redistributions of any form whatsoever must retain the following acknowledgment: "This
15  product includes Djeneric."
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
17  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
20  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */

28 package com.genimen.djeneric.repository;
29
30 import java.math.BigDecimal JavaDoc;
31 import java.util.Comparator JavaDoc;
32
33 import com.genimen.djeneric.repository.exceptions.DjenericException;
34 import com.genimen.djeneric.util.DjLogger;
35
36 /**
37  * A domain value defines a valid value or range for use by the validation
38  * method of a domain.
39  *
40  *@author Wido Riezebos
41  *@created 24 mei 2002
42  */

43 public class DjDomainValue
44 {
45   Object JavaDoc _val = null;
46   Object JavaDoc _high = null;
47   int _seq = 0;
48   String JavaDoc _description;
49   DjDomain _domain;
50   boolean _isDynamic = false;
51
52   /**
53    * Constructor for the DjDomainValue object
54    *
55    *@param domain The domain this value is for
56    *@param val The value
57    *@param description The description of the value
58    *@exception DjenericException Thrown if coersion of the value fails
59    * (coersion to the type defined by the domain)
60    */

61   public DjDomainValue(DjDomain domain, Object JavaDoc val, String JavaDoc description) throws DjenericException
62   {
63     _domain = domain;
64     if (domain == null) _val = val;
65     else _val = domain.coerse(val);
66     _description = description;
67     if (domain != null) _isDynamic = domain.isDynamic();
68   }
69
70   /**
71    * Constructor for the DjDomainValue object
72    *
73    *@param domain The domain this value is for
74    *@param val The low range value (inclusive)
75    *@param high The high range value (inclusive)
76    *@param description The description of the value
77    *@exception DjenericException Thrown if coersion of the value fails
78    * (coersion to the type defined by the domain)
79    */

80   public DjDomainValue(DjDomain domain, String JavaDoc val, String JavaDoc high, String JavaDoc description) throws DjenericException
81   {
82     _domain = domain;
83     if (domain == null)
84     {
85       // Not ranged?
86
if (high != null && high.equals("")) high = null;
87       _val = val;
88       _high = high;
89     }
90     else
91     {
92       _val = domain.coerse(val);
93       _high = domain.coerse(high);
94     }
95     _description = description;
96   }
97
98   /**
99    * Sets the sequence of this DjDomainValue within the domain's list of values
100    *
101    *@param seq The new sequence
102    */

103   public void setSeq(int seq)
104   {
105     _seq = seq;
106   }
107
108   /**
109    * Returns the sequence of this DjDomainValue within the domain's list of
110    * values
111    *
112    *@return The sequence
113    */

114   public int getSeq()
115   {
116     return _seq;
117   }
118
119   /**
120    * Sets the value
121    *
122    *@param val The new value value
123    *@exception DjenericException Description of the Exception
124    */

125   public void setValue(Object JavaDoc val) throws DjenericException
126   {
127     if (_domain != null) _val = _domain.coerse(val);
128     else _val = val;
129   }
130
131   /**
132    * Sets the description
133    *
134    *@param descr The new description value
135    */

136   public void setDescription(String JavaDoc descr)
137   {
138     _description = descr;
139   }
140
141   /**
142    * Returns the value / low range
143    *
144    *@return The value value
145    */

146   public Object JavaDoc getValue()
147   {
148     return _val;
149   }
150
151   /**
152    * Sets the high range value
153    *
154    *@param high The new high value
155    *@exception DjenericException Description of the Exception
156    */

157   public void setHigh(Object JavaDoc high) throws DjenericException
158   {
159     if (_domain != null) high = _domain.coerse(high);
160     else if (high != null && high.equals("")) high = null;
161
162     _high = high;
163   }
164
165   /**
166    * Returns the high of the DjDomainValue object
167    *
168    *@return The high value
169    */

170   public Object JavaDoc getHigh()
171   {
172     return _high;
173   }
174
175   /**
176    * Returns the range of the DjDomainValue object
177    *
178    *@return The range value
179    */

180   public boolean isRange()
181   {
182     return _high != null;
183   }
184
185   /**
186    * Returns the description of the DjDomainValue object
187    *
188    *@return The description value
189    */

190   public String JavaDoc getDescription()
191   {
192     return _description;
193   }
194
195   /**
196    * Returns true if the supplied value matches or is within range
197    *
198    *@param val The value to check
199    *@return true if valid
200    */

201   public boolean isValid(Object JavaDoc val)
202   {
203     if (val == null || _val == null) return true;
204
205     if (_high == null)
206     {
207       return _val.toString().equals(val.toString());
208     }
209
210     if (!(_high instanceof String JavaDoc))
211     {
212       return isValidNumericRange(val);
213     }
214
215     return val.toString().compareTo(_high.toString()) <= 0 && val.toString().compareTo(_val.toString()) >= 0;
216   }
217
218   /**
219    * Returns true if the supplied value matches or is within range
220    *
221    *@param val The value to check
222    *@return true if valid
223    */

224   protected boolean isValidNumericRange(Object JavaDoc val)
225   {
226     if (val == null) return false;
227
228     try
229     {
230       val = _domain.coerse(val);
231     }
232     catch (DjenericException dex)
233     {
234       DjLogger.log(dex);
235       return false;
236     }
237
238     if (val instanceof Integer JavaDoc)
239     {
240       Integer JavaDoc i = (Integer JavaDoc) val;
241       int value = ((Integer JavaDoc) _val).intValue();
242       int high = ((Integer JavaDoc) _high).intValue();
243       int chk = i.intValue();
244       return (chk >= value) && (chk <= high);
245     }
246     if (val instanceof Long JavaDoc)
247     {
248       Long JavaDoc i = (Long JavaDoc) val;
249       long value = ((Long JavaDoc) _val).longValue();
250       long high = ((Long JavaDoc) _high).longValue();
251       long chk = i.longValue();
252       return (chk >= value) && (chk <= high);
253     }
254     if (val instanceof BigDecimal JavaDoc)
255     {
256       BigDecimal JavaDoc i = (BigDecimal JavaDoc) val;
257       double value = ((BigDecimal JavaDoc) _val).doubleValue();
258       double high = ((BigDecimal JavaDoc) _high).doubleValue();
259       double chk = i.doubleValue();
260       return (chk >= value) && (chk <= high);
261     }
262     if (val instanceof String JavaDoc)
263     {
264       return val.toString().compareTo(_high.toString()) <= 0 && val.toString().compareTo(_val.toString()) >= 0;
265     }
266
267     return false;
268   }
269
270   /**
271    * Returns the description of the value
272    *
273    *@return The description of the value
274    */

275   public String JavaDoc toString()
276   {
277     if (_description == null || _description.trim().length() == 0) return getValue().toString();
278     return _description;
279   }
280
281   /**
282    * Compares two domain values and returns true if their values are equal
283    *
284    *@param obj The other value
285    *@return True if the values are equal
286    */

287   public boolean equals(Object JavaDoc obj)
288   {
289     if (obj instanceof DjDomainValue)
290     {
291       DjDomainValue dv = (DjDomainValue) obj;
292       Object JavaDoc v1 = dv.getValue();
293       Object JavaDoc v2 = getValue();
294
295       if ((v1 == null && v2 != null) || (v1 != null && v2 == null)) return false;
296       if (v1 == v2) return true;
297
298       return v1.equals(v2);
299     }
300     if (obj instanceof String JavaDoc)
301     {
302       return obj.equals(getValue());
303     }
304
305     return false;
306   }
307
308   public int hashCode()
309   {
310     if (getValue() != null) return getValue().hashCode();
311     return super.hashCode();
312   }
313
314   public boolean isDynamic()
315   {
316     return _isDynamic;
317   }
318
319   public void setDynamic(boolean isDynamic)
320   {
321     _isDynamic = isDynamic;
322   }
323
324 }
325
326 class DomainValueComparator implements Comparator JavaDoc
327 {
328
329   /**
330    * Compares two domain values using their sequence
331    *
332    *@param o1 value1
333    *@param o2 value2
334    *@return < 0 if value2 is greater than value1
335    */

336   public int compare(Object JavaDoc o1, Object JavaDoc o2)
337   {
338     if (!(o1 instanceof DjDomainValue) || !(o2 instanceof DjDomainValue)) return -1;
339     DjDomainValue cu1 = (DjDomainValue) o1;
340     DjDomainValue cu2 = (DjDomainValue) o2;
341     return cu1.getSeq() - cu2.getSeq();
342   }
343
344   /**
345    * Returns only true for this
346    *
347    *@param obj Object to match
348    *@return true if obj is this
349    */

350   public boolean equals(Object JavaDoc obj)
351   {
352     return obj == this;
353   }
354 }
355
356 class DomainValueDescrComparator implements Comparator JavaDoc
357 {
358
359   /**
360    * Compares two domain values using their description
361    *
362    *@param o1 value1
363    *@param o2 value2
364    *@return < 0 if value2's description is greater than value1's
365    */

366   public int compare(Object JavaDoc o1, Object JavaDoc o2)
367   {
368     DjDomainValue dv1 = (DjDomainValue) o1;
369     DjDomainValue dv2 = (DjDomainValue) o2;
370
371     String JavaDoc str1 = dv1.getDescription();
372     String JavaDoc str2 = dv2.getDescription();
373     if (str1 == null) str1 = "";
374     if (str2 == null) str2 = "";
375
376     return str1.compareTo(str2);
377   }
378
379   /**
380    * Returns only true for this
381    *
382    *@param obj Object to match
383    *@return Returns only true for this
384    */

385   public boolean equals(Object JavaDoc obj)
386   {
387     return false;
388   }
389 }
Popular Tags