KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > MuCharacter


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9
10 package org.jboss.util;
11
12 import java.io.Serializable JavaDoc;
13
14 /**
15  * A mutable character class.
16  *
17  * @version <tt>$Revision: 1.1.28.1 $</tt>
18  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
19  */

20 public class MuCharacter
21    implements Comparable JavaDoc, Cloneable JavaDoc, Serializable JavaDoc, Mutable
22 {
23    static final long serialVersionUID = 1397485614608419257L;
24    /** <code>char</code> value */
25    private char value = 0;
26
27    /**
28     * Construct a new mutable character.
29     */

30    public MuCharacter() {}
31
32    /**
33     * Construct a new mutable character.
34     *
35     * @param c <code>char</code> value.
36     */

37    public MuCharacter(char c) {
38       value = c;
39    }
40
41    /**
42     * Construct a new mutable character.
43     *
44     * @param obj Object to convert to a <code>char</code>.
45     */

46    public MuCharacter(Object JavaDoc obj) {
47       setValue(obj);
48    }
49
50    /**
51     * Set the value.
52     *
53     * @param c <code>char</code> value.
54     * @return The previous value.
55     */

56    public char set(char c) {
57       char old = value;
58       value = c;
59       return old;
60    }
61
62    /**
63     * Get the current value.
64     *
65     * @return The current value.
66     */

67    public char get() {
68       return value;
69    }
70
71    /**
72     * Return the <code>char</code> value of this mutable character.
73     *
74     * @return <code>char</code> value.
75     */

76    public char charValue() {
77       return value;
78    }
79
80    /**
81     * Compares this object with the specified long for order.
82     *
83     * @param other Value to compare with.
84     * @return A negative integer, zero, or a positive integer as
85     * this object is less than, equal to, or greater than
86     * the specified object.
87     */

88    public int compareTo(char other) {
89       return (value < other) ? -1 : (value == other) ? 0 : 1;
90    }
91
92    /**
93     * Compares this object with the specified object for order.
94     *
95     * @param other Value to compare with.
96     * @return A negative integer, zero, or a positive integer as
97     * this object is less than, equal to, or greater than
98     * the specified object.
99     *
100     * @throws ClassCastException Object is not a MuCharacter.
101     */

102    public int compareTo(Object JavaDoc obj) {
103       return compareTo((MuCharacter)obj);
104    }
105
106    /**
107     * Convert this mutable character to a string.
108     *
109     * @return String value.
110     */

111    public String JavaDoc toString() {
112       return String.valueOf(value);
113    }
114
115    /**
116     * Get the hash code of this mutable character.
117     *
118     * @return Hash code.
119     */

120    public int hashCode() {
121       return value;
122    }
123
124    /**
125     * Test the equality of this mutable character and another object.
126     *
127     * @param obj Qbject to test equality with.
128     * @return True if object is equal.
129     */

130    public boolean equals(Object JavaDoc obj) {
131       if (obj == this) return true;
132
133       if (obj != null && obj.getClass() == getClass()) {
134          return value == ((MuCharacter)obj).charValue();
135       }
136
137       return false;
138    }
139
140    /**
141     * Return a cloned copy of this mutable character.
142     *
143     * @return Cloned mutable character.
144     */

145    public Object JavaDoc clone() {
146       try {
147          return super.clone();
148       }
149       catch (CloneNotSupportedException JavaDoc e) {
150          throw new InternalError JavaDoc();
151       }
152    }
153
154
155    /////////////////////////////////////////////////////////////////////////
156
// Mutable Support //
157
/////////////////////////////////////////////////////////////////////////
158

159    /**
160     * Set the value of this mutable character.
161     *
162     * @param obj Object to convert to a <code>char</code>.
163     *
164     * @throws NotCoercibleException Can not convert to <code>char</code>.
165     */

166    public void setValue(Object JavaDoc obj) {
167       if (obj instanceof MuCharacter) {
168          value = ((MuCharacter)obj).value;
169       }
170       else if (obj instanceof Character JavaDoc) {
171          value = ((Character JavaDoc)obj).charValue();
172       }
173       else if (obj instanceof Number JavaDoc) {
174          value = (char)((Number JavaDoc)obj).intValue();
175       }
176       else {
177          throw new NotCoercibleException("can not convert to 'char': " + obj);
178       }
179    }
180
181    /**
182     * Return the char value of this mutable character.
183     *
184     * @return <code>java.lang.Character</code> value.
185     */

186    public Object JavaDoc getValue() {
187       return new Character JavaDoc(value);
188    }
189 }
190
191
Popular Tags