001: package clime.messadmin.utils;
002:
003: import java.io.Serializable;
004:
005: /**
006: * Comparable Boolean for JDK <= 1.4. Useless for JDK 5.
007: * Note that some static methods have been removed, as they where already defined in Boolean.
008: * This class is not intended as a replacement for java.lang.Boolean!
009: *
010: * (From the JDK documentation:)
011: * The Boolean class wraps a value of the primitive type
012: * <code>boolean</code> in an object. An object of type
013: * <code>Boolean</code> contains a single field whose type is
014: * <code>boolean</code>.
015: * <p>
016: * In addition, this class provides many methods for
017: * converting a <code>boolean</code> to a <code>String</code> and a
018: * <code>String</code> to a <code>boolean</code>, as well as other
019: * constants and methods useful when dealing with a
020: * <code>boolean</code>.
021: *
022: * @author Arthur van Hoff, Cédrik LIME
023: * @version 1.51, 05/11/04
024: * @since JDK1.0
025: */
026: public final class ComparableBoolean implements Serializable,
027: Comparable {//extends Boolean
028:
029: /**
030: * The <code>ComparableBoolean</code> object corresponding to the primitive
031: * value <code>true</code>.
032: */
033: public static final ComparableBoolean TRUE = new ComparableBoolean(
034: true);
035:
036: /**
037: * The <code>ComparableBoolean</code> object corresponding to the primitive
038: * value <code>false</code>.
039: */
040: public static final ComparableBoolean FALSE = new ComparableBoolean(
041: false);
042:
043: /**
044: * The Class object representing the primitive type boolean.
045: *
046: * @since JDK1.1
047: */
048: public static final Class TYPE = Boolean.TYPE;
049:
050: /**
051: * The value of the Boolean.
052: *
053: * @serial
054: */
055: private final boolean value;
056:
057: /** use serialVersionUID from JDK 1.0.2 for interoperability */
058: //private static final long serialVersionUID = -3665804199014368530L;
059: /**
060: * Allocates a <code>ComparableBoolean</code> object representing the
061: * <code>value</code> argument.
062: *
063: * <p><b>Note: It is rarely appropriate to use this constructor.
064: * Unless a <i>new</i> instance is required, the static factory
065: * {@link #valueOf(boolean)} is generally a better choice. It is
066: * likely to yield significantly better space and time performance.</b>
067: *
068: * @param value the value of the <code>Boolean</code>.
069: */
070: private ComparableBoolean(boolean value) {
071: this .value = value;
072: }
073:
074: /**
075: * Returns the value of this <tt>ComparableBoolean</tt> object as a boolean
076: * primitive.
077: *
078: * @return the primitive <code>boolean</code> value of this object.
079: */
080: public boolean booleanValue() {
081: return value;
082: }
083:
084: /**
085: * Returns a <tt>ComparableBoolean</tt> instance representing the specified
086: * <tt>boolean</tt> value. If the specified <tt>boolean</tt> value
087: * is <tt>true</tt>, this method returns <tt>ComparableBoolean.TRUE</tt>;
088: * if it is <tt>false</tt>, this method returns <tt>ComparableBoolean.FALSE</tt>.
089: * If a new <tt>ComparableBoolean</tt> instance is not required, this method
090: * should generally be used in preference to the constructor
091: * {@link #ComparableBoolean(boolean)}, as this method is likely to yield
092: * significantly better space and time performance.
093: *
094: * @param b a boolean value.
095: * @return a <tt>ComparableBoolean</tt> instance representing <tt>b</tt>.
096: * @since 1.4
097: */
098: public static ComparableBoolean valueOf(boolean b) {
099: return (b ? TRUE : FALSE);
100: }
101:
102: /**
103: * Returns a <tt>String</tt> object representing this ComparableBoolean's
104: * value. If this object represents the value <code>true</code>,
105: * a string equal to <code>"true"</code> is returned. Otherwise, a
106: * string equal to <code>"false"</code> is returned.
107: *
108: * @return a string representation of this object.
109: */
110: public String toString() {
111: return value ? "true" : "false";//Boolean.toString(value);//$NON-NLS-1$//$NON-NLS-2$
112: }
113:
114: /**
115: * Returns a hash code for this <tt>ComparableBoolean</tt> object.
116: *
117: * @return the integer <tt>1231</tt> if this object represents
118: * <tt>true</tt>; returns the integer <tt>1237</tt> if this
119: * object represents <tt>false</tt>.
120: */
121: public int hashCode() {
122: return value ? 1231 : 1237;//Boolean.valueOf(value).hashCode();
123: }
124:
125: /**
126: * Returns <code>true</code> if and only if the argument is not
127: * <code>null</code> and is a <code>ComparableBoolean</code> object that
128: * represents the same <code>boolean</code> value as this object.
129: *
130: * @param obj the object to compare with.
131: * @return <code>true</code> if the Boolean objects represent the
132: * same value; <code>false</code> otherwise.
133: */
134: public boolean equals(Object obj) {
135: if (obj instanceof ComparableBoolean) {
136: return value == ((ComparableBoolean) obj).booleanValue();
137: }
138: if (obj instanceof Boolean) {
139: return value == ((Boolean) obj).booleanValue();
140: }
141: return false;
142: }
143:
144: /**
145: * Compares this <tt>ComparableBoolean</tt> instance with another.
146: *
147: * @param o the <tt>ComparableBoolean</tt> or <tt>Boolean</tt> instance to be compared
148: * @return zero if this object represents the same boolean value as the
149: * argument; a positive value if this object represents true
150: * and the argument represents false; and a negative value if
151: * this object represents false and the argument represents true
152: * @throws NullPointerException if the argument is <tt>null</tt>
153: * @see Comparable
154: * @since 1.5
155: */
156: public int compareTo(Object o) {
157: if (o instanceof ComparableBoolean) {
158: Boolean bool = ((ComparableBoolean) o).value ? Boolean.TRUE
159: : Boolean.FALSE;//Boolean.valueOf(((ComparableBoolean)o).value);
160: return compareTo(bool);
161: } else if (o instanceof Boolean) {
162: return (((Boolean) o).booleanValue() == value ? 0
163: : (value ? 1 : -1));
164: }
165: throw new ClassCastException(o.getClass().getName());
166: }
167:
168: }
|