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 /*** This class is for identity with a single short field.
30 * @version 2.0
31 */
32 public class ShortIdentity
33 extends SingleFieldIdentity
34 {
35 private short key;
36
37 private void construct(short key) {
38 this.key = key;
39 hashCode = hashClassName() ^ key;
40 }
41
42 /*** Constructor with class and key.
43 * @param pcClass the class
44 * @param key the key
45 */
46 public ShortIdentity (Class pcClass, short key) {
47 super(pcClass);
48 construct(key);
49 }
50
51 /*** Constructor with class and key.
52 * @param pcClass the class
53 * @param key the key
54 */
55 public ShortIdentity (Class pcClass, Short key) {
56 super(pcClass);
57 setKeyAsObject(key);
58 construct(key.shortValue());
59 }
60
61 /*** Constructor with class and key.
62 * @param pcClass the class
63 * @param str the key
64 */
65 public ShortIdentity (Class pcClass, String str) {
66 super(pcClass);
67 assertKeyNotNull(str);
68 construct(Short.parseShort (str));
69 }
70
71 /*** Constructor only for Externalizable.
72 */
73 public ShortIdentity () {
74 }
75
76 /*** Return the key.
77 * @return the key
78 */
79 public short getKey () {
80 return key;
81 }
82
83 /*** Return the String form of the key.
84 * @return the String form of the key
85 */
86 public String toString () {
87 return Short.toString(key);
88 }
89
90 /*** Determine if the other object represents the same object id.
91 * @param obj the other object
92 * @return true if both objects represent the same object id
93 */
94 public boolean equals (Object obj) {
95 if (this == obj) {
96 return true;
97 } else if (!super.equals (obj)) {
98 return false;
99 } else {
100 ShortIdentity other = (ShortIdentity) obj;
101 return key == other.key;
102 }
103 }
104
105 /*** Create the key as an Object.
106 * @return the key as an Object
107 * @since 2.0
108 */
109 protected Object createKeyAsObject() {
110 return new Short(key);
111 }
112
113 /*** Write this object. Write the superclass first.
114 * @param out the output
115 */
116 public void writeExternal(ObjectOutput out) throws IOException {
117 super.writeExternal (out);
118 out.writeShort(key);
119 }
120
121 /*** Read this object. Read the superclass first.
122 * @param in the input
123 */
124 public void readExternal(ObjectInput in)
125 throws IOException, ClassNotFoundException {
126 super.readExternal (in);
127 key = in.readShort();
128 }
129 }