001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.dbobj;
066:
067: import com.jcorporate.expresso.core.cache.Cacheable;
068: import com.jcorporate.expresso.core.misc.StringUtil;
069:
070: /**
071: * A valid value is a enumerated collection very similar to a pure
072: * Struts label and value bean. It typically represent an item of data
073: * that is displayed in a drop-down list box or a menu selection.
074: * A valid value has a real value as known as a key and description.
075: * <p/>
076: * <p/>
077: * <p/>
078: * <p/>
079: * To support internationalisation (i18n) the look at the subclass
080: * <code>ISOValidValue</code>.
081: * <p/>
082: * </p>
083: * <p/>
084: * <p/>
085: * <p/>
086: * <p/>
087: * This class also contains two very useful static inner classes
088: * <code>ValueComparator</code> and
089: * <code>DescriptionComparator</code>, which are useful for supporting
090: * Java collection objects that contain <code>ValidValue</code> types.
091: * <p/>
092: * </p>
093: *
094: * @author Peter A. Pilgrim, Fri Dec 27 22:33:27 GMT 2002
095: * @version $Id: ValidValue.java,v 1.14 2004/11/18 02:03:27 lhamel Exp $
096: * @see ISOValidValue
097: */
098: public class ValidValue implements Cacheable, Cloneable {
099:
100: protected String value = "";
101: protected String description = "";
102:
103: /**
104: * Default constructor for creating a valid value.
105: * Please note: no canonization takes place within this method.
106: */
107: public ValidValue() {
108: // No Operation
109: }
110:
111: /**
112: * Original constructor for creating a valid value.
113: * Please note: no canonization takes place within this method.
114: *
115: * @param newValue the real value of the enumeration
116: * @param newDescrip the description of the enumeration
117: */
118: public ValidValue(String newValue, String newDescrip) {
119: setValue(newValue);
120: setDescription(newDescrip);
121: } /* ValidValue(String, String) */
122:
123: /**
124: * Gets the real value of the valid value
125: *
126: * @return the value string
127: * @see #setValue
128: */
129: public String getValue() {
130: return value;
131: } /* getValue() */
132:
133: /**
134: * Sets the real value of the valid value
135: *
136: * @param newValue the new value string
137: * @see #getValue
138: */
139: public void setValue(String newValue) {
140: this .value = StringUtil.notNull(newValue);
141: } /* getValue() */
142:
143: /**
144: * Gets the real value of the valid value as a cache key
145: *
146: * @return the value string as a cache key
147: * @see #getValue
148: */
149: public String getKey() {
150: return getValue();
151: } /* getKey() */
152:
153: /**
154: * Hashcode since we store by key
155: *
156: * @return integer
157: */
158: public int hashCode() {
159: return this .value.hashCode();
160: }
161:
162: /**
163: * Gets the description of the valid value
164: *
165: * @return the description
166: */
167: public String getDescription() {
168: return description;
169: } /* getDescription() */
170:
171: /**
172: * Sets the real description of the valid value
173: *
174: * @param newDescription the new description string
175: * @see #getDescription
176: */
177: public void setDescription(String newDescription) {
178: this .description = StringUtil.notNull(newDescription);
179: } /* getDescription() */
180:
181: /**
182: * Human readable string for debugging
183: *
184: * @return String
185: */
186: public String toString() {
187: StringBuffer buf = new StringBuffer();
188: buf.append("ValidValue@" + Integer.toHexString(this .hashCode())
189: + "{");
190: buf.append("value:`" + value + "' (`" + description + "')");
191: buf.append("}");
192: return buf.toString();
193: }
194:
195: /**
196: * {@inheritDoc}
197: *
198: * @return a clone of this instance.
199: * @throws CloneNotSupportedException if the object's class does not
200: * support the <code>Cloneable</code> interface. Subclasses that
201: * override the <code>clone</code> method can also throw this
202: * exception to indicate that an instance cannot be cloned.
203: * @todo Implement this java.lang.Object method
204: */
205: protected Object clone() throws CloneNotSupportedException {
206: ValidValue v = (ValidValue) super .clone();
207: v.setDescription(this .getDescription());
208: v.setValue(this .getValue());
209: return v;
210: }
211:
212: /**
213: * Indicates whether some other object is "equal to" this one.
214: *
215: * @param obj the reference object with which to compare.
216: * @return <code>true</code> if this object is the same as the obj
217: * argument; <code>false</code> otherwise.
218: * @todo Implement this java.lang.Object method
219: */
220: public boolean equals(Object obj) {
221: if (!(obj instanceof ValidValue)) {
222: return false;
223: }
224:
225: ValidValue vv = (ValidValue) obj;
226: boolean returnVal = true;
227:
228: returnVal &= (this .getValue().equals(vv.getValue()));
229: returnVal &= (this .getDescription().equals(vv.getDescription()));
230: return returnVal;
231: }
232:
233: ////////////////////////////////////////////////////////////////////////////////
234: // I N N E R C L A S S
235: ////////////////////////////////////////////////////////////////////////////////
236:
237: /**
238: * A simple comparator for comparing the <code>value</code>
239: * attribute of <code>ValidValue</code> records.
240: */
241: public static class ValueComparator implements java.util.Comparator {
242: boolean reverse;
243:
244: /**
245: * Creates an ascending comparator
246: */
247: public ValueComparator() {
248: this (false);
249: }
250:
251: /**
252: * Creates comparator with specified direction
253: *
254: * @param reverse true if we should use reverse sort order.
255: */
256: public ValueComparator(boolean reverse) {
257: this .reverse = reverse;
258: }
259:
260: /**
261: * Comparison method
262: * Compares its two arguments for order. Returns a negative
263: * integer, zero, or a positive integer as the first argument
264: * is less than, equal to, or greater than the second.
265: * <p/>
266: * The implementor must ensure that sgn(compare(x, y)) ==
267: * -sgn(compare(y, x)) for all x and y. (This implies that
268: * compare(x, y) must throw an exception if and only if
269: * compare(y, x) throws an exception.)
270: * <p/>
271: * The implementor must also ensure that the relation is
272: * transitive: ((compare(x, y)>0) && (compare(y, z)>0))
273: * implies compare(x, z)>0.
274: * <p/>
275: * Finally, the implementer must ensure that compare(x, y)==0
276: * implies that sgn(compare(x, z))==sgn(compare(y, z)) for all
277: * z.
278: * <p/>
279: * It is generally the case, but not strictly required that
280: * (compare(x, y)==0) == (x.equals(y)). Generally speaking,
281: * any comparator that violates this condition should clearly
282: * indicate this fact. The recommended language is "Note: this
283: * comparator imposes orderings that inconsistent with equals."
284: *
285: * @param o1 the first <code>ValidValue</code> object
286: * @param o2 the second <code>ValidValue</code> object
287: * @return a negative integer, zero, or a positive integer as
288: * the first argument is less than, equal to, or greater than
289: * the second.
290: * @throws ClassCastException if the arguments' types prevent
291: * them from being compared by this Comparator.
292: */
293: public int compare(Object o1, Object o2) {
294: ValidValue ref1 = (ValidValue) o1;
295: ValidValue ref2 = (ValidValue) o2;
296: int result = ref2.getValue().compareTo(ref1.getValue());
297: return result * (reverse ? 1 : -1);
298: }
299:
300: /**
301: * Equivalence method
302: * {@inheritDoc}
303: *
304: * @param o the other object
305: * @return true if the two objects are equal
306: */
307: public boolean equals(Object o) {
308: if (!(o instanceof ValueComparator)) {
309: return false;
310: }
311: ValueComparator ref = (ValueComparator) o;
312: return reverse == ref.reverse;
313: }
314:
315: /**
316: * Human readable string
317: *
318: * @return java.lang.String
319: */
320: public String toString() {
321: StringBuffer buf = new StringBuffer();
322: buf.append("{ ValueComparator ");
323: buf.append(reverse ? "ascending" : "descending");
324: buf.append(" sort }");
325: return buf.toString();
326: }
327: }
328:
329: /**
330: * A simple comparator for comparing the <code>description</code>
331: * attribute of <code>ValidValue</code> records.
332: */
333: public static class DescriptionComparator implements
334: java.util.Comparator {
335: boolean reverse;
336:
337: /**
338: * Creates an ascending comparator
339: */
340: public DescriptionComparator() {
341: this (false);
342: }
343:
344: /**
345: * Creates comparator with specified direction
346: *
347: * @param reverse true if should use reverse sort order.
348: */
349: public DescriptionComparator(boolean reverse) {
350: this .reverse = reverse;
351: }
352:
353: /**
354: * Comparison method
355: * Compares its two arguments for order. Returns a negative
356: * integer, zero, or a positive integer as the first argument
357: * is less than, equal to, or greater than the second.
358: * <p/>
359: * The implementor must ensure that sgn(compare(x, y)) ==
360: * -sgn(compare(y, x)) for all x and y. (This implies that
361: * compare(x, y) must throw an exception if and only if
362: * compare(y, x) throws an exception.)
363: * <p/>
364: * The implementor must also ensure that the relation is
365: * transitive: ((compare(x, y)>0) && (compare(y, z)>0))
366: * implies compare(x, z)>0.
367: * <p/>
368: * Finally, the implementer must ensure that compare(x, y)==0
369: * implies that sgn(compare(x, z))==sgn(compare(y, z)) for all
370: * z.
371: * <p/>
372: * It is generally the case, but not strictly required that
373: * (compare(x, y)==0) == (x.equals(y)). Generally speaking,
374: * any comparator that violates this condition should clearly
375: * indicate this fact. The recommended language is "Note: this
376: * comparator imposes orderings that inconsistent with equals."
377: *
378: * @param o1 the first <code>ValidValue</code> object
379: * @param o2 the second <code>ValidValue</code> object
380: * @return a negative integer, zero, or a positive integer as
381: * the first argument is less than, equal to, or greater than
382: * the second.
383: * @throws ClassCastException if the arguments' types prevent
384: * them from being compared by this Comparator.
385: */
386: public int compare(Object o1, Object o2) {
387: ValidValue ref1 = (ValidValue) o1;
388: ValidValue ref2 = (ValidValue) o2;
389: int result = ref2.getDescription().compareTo(
390: ref1.getDescription());
391: return result * (reverse ? 1 : -1);
392: }
393:
394: /**
395: * Equivalence method
396: *
397: * @param o the Other object
398: * @return true if the objects are equal.
399: */
400: public boolean equals(Object o) {
401: if (!(o instanceof DescriptionComparator)) {
402: return false;
403: }
404: DescriptionComparator ref = (DescriptionComparator) o;
405: return reverse == ref.reverse;
406: }
407:
408: /**
409: * Human readable string
410: *
411: * @return java.lang.String
412: */
413: public String toString() {
414: StringBuffer buf = new StringBuffer();
415: buf.append("{ DescriptionComparator ");
416: buf.append(reverse ? "ascending" : "descending");
417: buf.append(" sort }");
418: return buf.toString();
419: }
420: }
421:
422: }
423:
424: /* ValidValue */
|