KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dbobj > ValidValue


1 /* ====================================================================
2 * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3 *
4 * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * 3. The end-user documentation included with the redistribution,
19 * if any, must include the following acknowledgment:
20 * "This product includes software developed by Jcorporate Ltd.
21 * (http://www.jcorporate.com/)."
22 * Alternately, this acknowledgment may appear in the software itself,
23 * if and wherever such third-party acknowledgments normally appear.
24 *
25 * 4. "Jcorporate" and product names such as "Expresso" must
26 * not be used to endorse or promote products derived from this
27 * software without prior written permission. For written permission,
28 * please contact info@jcorporate.com.
29 *
30 * 5. Products derived from this software may not be called "Expresso",
31 * or other Jcorporate product names; nor may "Expresso" or other
32 * Jcorporate product names appear in their name, without prior
33 * written permission of Jcorporate Ltd.
34 *
35 * 6. No product derived from this software may compete in the same
36 * market space, i.e. framework, without prior written permission
37 * of Jcorporate Ltd. For written permission, please contact
38 * partners@jcorporate.com.
39 *
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This software consists of voluntary contributions made by many
55 * individuals on behalf of the Jcorporate Ltd. Contributions back
56 * to the project(s) are encouraged when you make modifications.
57 * Please send them to support@jcorporate.com. For more information
58 * on Jcorporate Ltd. and its products, please see
59 * <http://www.jcorporate.com/>.
60 *
61 * Portions of this software are based upon other open source
62 * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.dbobj;
66
67 import com.jcorporate.expresso.core.cache.Cacheable;
68 import com.jcorporate.expresso.core.misc.StringUtil;
69
70
71 /**
72  * A valid value is a enumerated collection very similar to a pure
73  * Struts label and value bean. It typically represent an item of data
74  * that is displayed in a drop-down list box or a menu selection.
75  * A valid value has a real value as known as a key and description.
76  * <p/>
77  * <p/>
78  * <p/>
79  * <p/>
80  * To support internationalisation (i18n) the look at the subclass
81  * <code>ISOValidValue</code>.
82  * <p/>
83  * </p>
84  * <p/>
85  * <p/>
86  * <p/>
87  * <p/>
88  * This class also contains two very useful static inner classes
89  * <code>ValueComparator</code> and
90  * <code>DescriptionComparator</code>, which are useful for supporting
91  * Java collection objects that contain <code>ValidValue</code> types.
92  * <p/>
93  * </p>
94  *
95  * @author Peter A. Pilgrim, Fri Dec 27 22:33:27 GMT 2002
96  * @version $Id: ValidValue.java,v 1.14 2004/11/18 02:03:27 lhamel Exp $
97  * @see ISOValidValue
98  */

99 public class ValidValue implements Cacheable, Cloneable JavaDoc {
100
101     protected String JavaDoc value = "";
102     protected String JavaDoc description = "";
103
104     /**
105      * Default constructor for creating a valid value.
106      * Please note: no canonization takes place within this method.
107      */

108     public ValidValue() {
109         // No Operation
110
}
111
112     /**
113      * Original constructor for creating a valid value.
114      * Please note: no canonization takes place within this method.
115      *
116      * @param newValue the real value of the enumeration
117      * @param newDescrip the description of the enumeration
118      */

119     public ValidValue(String JavaDoc newValue, String JavaDoc newDescrip) {
120         setValue(newValue);
121         setDescription(newDescrip);
122     } /* ValidValue(String, String) */
123
124     /**
125      * Gets the real value of the valid value
126      *
127      * @return the value string
128      * @see #setValue
129      */

130     public String JavaDoc getValue() {
131         return value;
132     } /* getValue() */
133
134     /**
135      * Sets the real value of the valid value
136      *
137      * @param newValue the new value string
138      * @see #getValue
139      */

140     public void setValue(String JavaDoc newValue) {
141         this.value = StringUtil.notNull(newValue);
142     } /* getValue() */
143
144     /**
145      * Gets the real value of the valid value as a cache key
146      *
147      * @return the value string as a cache key
148      * @see #getValue
149      */

150     public String JavaDoc getKey() {
151         return getValue();
152     } /* getKey() */
153
154     /**
155      * Hashcode since we store by key
156      *
157      * @return integer
158      */

159     public int hashCode() {
160         return this.value.hashCode();
161     }
162
163
164     /**
165      * Gets the description of the valid value
166      *
167      * @return the description
168      */

169     public String JavaDoc getDescription() {
170         return description;
171     } /* getDescription() */
172
173     /**
174      * Sets the real description of the valid value
175      *
176      * @param newDescription the new description string
177      * @see #getDescription
178      */

179     public void setDescription(String JavaDoc newDescription) {
180         this.description = StringUtil.notNull(newDescription);
181     } /* getDescription() */
182
183     /**
184      * Human readable string for debugging
185      *
186      * @return String
187      */

188     public String JavaDoc toString() {
189         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
190         buf.append("ValidValue@" + Integer.toHexString(this.hashCode()) + "{");
191         buf.append("value:`" + value + "' (`" + description + "')");
192         buf.append("}");
193         return buf.toString();
194     }
195
196     /**
197      * {@inheritDoc}
198      *
199      * @return a clone of this instance.
200      * @throws CloneNotSupportedException if the object's class does not
201      * support the <code>Cloneable</code> interface. Subclasses that
202      * override the <code>clone</code> method can also throw this
203      * exception to indicate that an instance cannot be cloned.
204      * @todo Implement this java.lang.Object method
205      */

206     protected Object JavaDoc clone()
207             throws CloneNotSupportedException JavaDoc {
208         ValidValue v = (ValidValue) super.clone();
209         v.setDescription(this.getDescription());
210         v.setValue(this.getValue());
211         return v;
212     }
213
214     /**
215      * Indicates whether some other object is "equal to" this one.
216      *
217      * @param obj the reference object with which to compare.
218      * @return <code>true</code> if this object is the same as the obj
219      * argument; <code>false</code> otherwise.
220      * @todo Implement this java.lang.Object method
221      */

222     public boolean equals(Object JavaDoc obj) {
223         if (!(obj instanceof ValidValue)) {
224             return false;
225         }
226
227         ValidValue vv = (ValidValue) obj;
228         boolean returnVal = true;
229
230         returnVal &= (this.getValue().equals(vv.getValue()));
231         returnVal &= (this.getDescription().equals(vv.getDescription()));
232         return returnVal;
233     }
234
235
236
237
238     ////////////////////////////////////////////////////////////////////////////////
239
// I N N E R C L A S S
240
////////////////////////////////////////////////////////////////////////////////
241

242     /**
243      * A simple comparator for comparing the <code>value</code>
244      * attribute of <code>ValidValue</code> records.
245      */

246     public static class ValueComparator
247             implements java.util.Comparator JavaDoc {
248         boolean reverse;
249
250         /**
251          * Creates an ascending comparator
252          */

253         public ValueComparator() {
254             this(false);
255         }
256
257         /**
258          * Creates comparator with specified direction
259          *
260          * @param reverse true if we should use reverse sort order.
261          */

262         public ValueComparator(boolean reverse) {
263             this.reverse = reverse;
264         }
265
266         /**
267          * Comparison method
268          * Compares its two arguments for order. Returns a negative
269          * integer, zero, or a positive integer as the first argument
270          * is less than, equal to, or greater than the second.
271          * <p/>
272          * The implementor must ensure that sgn(compare(x, y)) ==
273          * -sgn(compare(y, x)) for all x and y. (This implies that
274          * compare(x, y) must throw an exception if and only if
275          * compare(y, x) throws an exception.)
276          * <p/>
277          * The implementor must also ensure that the relation is
278          * transitive: ((compare(x, y)>0) && (compare(y, z)>0))
279          * implies compare(x, z)>0.
280          * <p/>
281          * Finally, the implementer must ensure that compare(x, y)==0
282          * implies that sgn(compare(x, z))==sgn(compare(y, z)) for all
283          * z.
284          * <p/>
285          * It is generally the case, but not strictly required that
286          * (compare(x, y)==0) == (x.equals(y)). Generally speaking,
287          * any comparator that violates this condition should clearly
288          * indicate this fact. The recommended language is "Note: this
289          * comparator imposes orderings that inconsistent with equals."
290          *
291          * @param o1 the first <code>ValidValue</code> object
292          * @param o2 the second <code>ValidValue</code> object
293          * @return a negative integer, zero, or a positive integer as
294          * the first argument is less than, equal to, or greater than
295          * the second.
296          * @throws ClassCastException if the arguments' types prevent
297          * them from being compared by this Comparator.
298          */

299         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
300             ValidValue ref1 = (ValidValue) o1;
301             ValidValue ref2 = (ValidValue) o2;
302             int result = ref2.getValue().compareTo(ref1.getValue());
303             return result * (reverse ? 1 : -1);
304         }
305
306
307         /**
308          * Equivalence method
309          * {@inheritDoc}
310          *
311          * @param o the other object
312          * @return true if the two objects are equal
313          */

314         public boolean equals(Object JavaDoc o) {
315             if (!(o instanceof ValueComparator)) {
316                 return false;
317             }
318             ValueComparator ref = (ValueComparator) o;
319             return reverse == ref.reverse;
320         }
321
322
323         /**
324          * Human readable string
325          *
326          * @return java.lang.String
327          */

328         public String JavaDoc toString() {
329             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
330             buf.append("{ ValueComparator ");
331             buf.append(reverse ? "ascending" : "descending");
332             buf.append(" sort }");
333             return buf.toString();
334         }
335     }
336
337
338     /**
339      * A simple comparator for comparing the <code>description</code>
340      * attribute of <code>ValidValue</code> records.
341      */

342     public static class DescriptionComparator
343             implements java.util.Comparator JavaDoc {
344         boolean reverse;
345
346         /**
347          * Creates an ascending comparator
348          */

349         public DescriptionComparator() {
350             this(false);
351         }
352
353         /**
354          * Creates comparator with specified direction
355          *
356          * @param reverse true if should use reverse sort order.
357          */

358         public DescriptionComparator(boolean reverse) {
359             this.reverse = reverse;
360         }
361
362         /**
363          * Comparison method
364          * Compares its two arguments for order. Returns a negative
365          * integer, zero, or a positive integer as the first argument
366          * is less than, equal to, or greater than the second.
367          * <p/>
368          * The implementor must ensure that sgn(compare(x, y)) ==
369          * -sgn(compare(y, x)) for all x and y. (This implies that
370          * compare(x, y) must throw an exception if and only if
371          * compare(y, x) throws an exception.)
372          * <p/>
373          * The implementor must also ensure that the relation is
374          * transitive: ((compare(x, y)>0) && (compare(y, z)>0))
375          * implies compare(x, z)>0.
376          * <p/>
377          * Finally, the implementer must ensure that compare(x, y)==0
378          * implies that sgn(compare(x, z))==sgn(compare(y, z)) for all
379          * z.
380          * <p/>
381          * It is generally the case, but not strictly required that
382          * (compare(x, y)==0) == (x.equals(y)). Generally speaking,
383          * any comparator that violates this condition should clearly
384          * indicate this fact. The recommended language is "Note: this
385          * comparator imposes orderings that inconsistent with equals."
386          *
387          * @param o1 the first <code>ValidValue</code> object
388          * @param o2 the second <code>ValidValue</code> object
389          * @return a negative integer, zero, or a positive integer as
390          * the first argument is less than, equal to, or greater than
391          * the second.
392          * @throws ClassCastException if the arguments' types prevent
393          * them from being compared by this Comparator.
394          */

395         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
396             ValidValue ref1 = (ValidValue) o1;
397             ValidValue ref2 = (ValidValue) o2;
398             int result = ref2.getDescription().compareTo(ref1.getDescription());
399             return result * (reverse ? 1 : -1);
400         }
401
402         /**
403          * Equivalence method
404          *
405          * @param o the Other object
406          * @return true if the objects are equal.
407          */

408         public boolean equals(Object JavaDoc o) {
409             if (!(o instanceof DescriptionComparator)) {
410                 return false;
411             }
412             DescriptionComparator ref = (DescriptionComparator) o;
413             return reverse == ref.reverse;
414         }
415
416         /**
417          * Human readable string
418          *
419          * @return java.lang.String
420          */

421         public String JavaDoc toString() {
422             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
423             buf.append("{ DescriptionComparator ");
424             buf.append(reverse ? "ascending" : "descending");
425             buf.append(" sort }");
426             return buf.toString();
427         }
428     }
429
430
431 }
432
433 /* ValidValue */
434
Popular Tags