1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package javax.jdo.identity;
24
25 import java.io.IOException;
26 import java.io.ObjectInput;
27 import java.io.ObjectOutput;
28
29 import javax.jdo.spi.I18NHelper;
30
31 /** This class is for identity with a single character field.
32 * @version 2.0
33 */
34 public class CharIdentity extends SingleFieldIdentity {
35
36 /** The Internationalization message helper.
37 */
38 private static I18NHelper msg = I18NHelper.getInstance ("javax.jdo.Bundle");
39
40 /** The key.
41 */
42 private char key;
43
44 private void construct(char key) {
45 this.key = key;
46 hashCode = hashClassName() ^ key;
47 }
48
49 /** Constructor with class and key.
50 * @param pcClass the target class
51 * @param key the key
52 */
53 public CharIdentity (Class pcClass, char key) {
54 super (pcClass);
55 construct(key);
56 }
57
58 /** Constructor with class and key.
59 * @param pcClass the target class
60 * @param key the key
61 */
62 public CharIdentity (Class pcClass, Character key) {
63 super (pcClass);
64 setKeyAsObject(key);
65 construct(key.charValue());
66 }
67
68 /** Constructor with class and key. The String must have exactly one
69 * character.
70 * @param pcClass the target class
71 * @param str the key
72 */
73 public CharIdentity (Class pcClass, String str) {
74 super(pcClass);
75 assertKeyNotNull(str);
76 if (str.length() != 1)
77 throw new IllegalArgumentException(
78 msg.msg("EXC_StringWrongLength"));
79 construct(str.charAt(0));
80 }
81
82 /** Constructor only for Externalizable.
83 */
84 public CharIdentity () {
85 }
86
87 /** Return the key.
88 * @return the key
89 */
90 public char getKey () {
91 return key;
92 }
93
94 /** Return the String form of the key.
95 * @return the String form of the key
96 */
97 public String toString () {
98 return String.valueOf(key);
99 }
100
101 /** Determine if the other object represents the same object id.
102 * @param obj the other object
103 * @return true if both objects represent the same object id
104 */
105 public boolean equals (Object obj) {
106 if (this == obj) {
107 return true;
108 } else if (!super.equals (obj)) {
109 return false;
110 } else {
111 CharIdentity other = (CharIdentity) obj;
112 return key == other.key;
113 }
114 }
115
116 /** Determine the ordering of identity objects.
117 * @param o Other identity
118 * @return The relative ordering between the objects
119 * @since 2.2
120 */
121 public int compareTo(Object o) {
122 if (o instanceof CharIdentity) {
123 CharIdentity other = (CharIdentity)o;
124 int result = super.compare(other);
125 if (result == 0) {
126 return (key - other.key);
127 } else {
128 return result;
129 }
130 }
131 else if (o == null) {
132 throw new ClassCastException("object is null");
133 }
134 throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
135 }
136
137 /** Create the key as an Object.
138 * @return the key as an Object
139 * @since 2.0
140 */
141 protected Object createKeyAsObject() {
142 return new Character(key);
143 }
144
145 /** Write this object. Write the superclass first.
146 * @param out the output
147 */
148 public void writeExternal(ObjectOutput out) throws IOException {
149 super.writeExternal (out);
150 out.writeChar(key);
151 }
152
153 /** Read this object. Read the superclass first.
154 * @param in the input
155 */
156 public void readExternal(ObjectInput in)
157 throws IOException, ClassNotFoundException {
158 super.readExternal (in);
159 key = in.readChar();
160 }
161 }