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 long field.
30 * @version 2.0
31 */
32 public class LongIdentity extends SingleFieldIdentity {
33
34 /** The key.
35 */
36 private long key;
37
38 private void construct(long key) {
39 this.key = key;
40 hashCode = hashClassName() ^ (int)key;
41 }
42
43 /** Constructor with class and key.
44 * @param pcClass the class
45 * @param key the key
46 */
47 public LongIdentity (Class pcClass, long key) {
48 super (pcClass);
49 construct(key);
50 }
51
52 /** Constructor with class and key.
53 * @param pcClass the class
54 * @param key the key
55 */
56 public LongIdentity (Class pcClass, Long key) {
57 super(pcClass);
58 setKeyAsObject(key);
59 construct(key.longValue());
60 }
61
62 /** Constructor with class and key.
63 * @param pcClass the class
64 * @param str the key
65 */
66 public LongIdentity (Class pcClass, String str) {
67 super(pcClass);
68 assertKeyNotNull(str);
69 construct(Long.parseLong(str));
70 }
71
72 /** Constructor only for Externalizable.
73 */
74 public LongIdentity () {
75 }
76
77 /** Return the key.
78 * @return the key
79 */
80 public long getKey () {
81 return key;
82 }
83
84 /** Return the String form of the key.
85 * @return the String form of the key
86 */
87 public String toString () {
88 return Long.toString(key);
89 }
90
91 /** Determine if the other object represents the same object id.
92 * @param obj the other object
93 * @return true if both objects represent the same object id
94 */
95 public boolean equals (Object obj) {
96 if (this == obj) {
97 return true;
98 } else if (!super.equals (obj)) {
99 return false;
100 } else {
101 LongIdentity other = (LongIdentity) obj;
102 return key == other.key;
103 }
104 }
105
106 /** Determine the ordering of identity objects.
107 * @param o Other identity
108 * @return The relative ordering between the objects
109 * @since 2.2
110 */
111 public int compareTo(Object o) {
112 if (o instanceof LongIdentity) {
113 LongIdentity other = (LongIdentity)o;
114 int result = super.compare(other);
115 if (result == 0) {
116 long diff = key - other.key;
117 if (diff == 0) {
118 return 0;
119 } else {
120 if (diff < 0) {
121 return -1;
122 } else {
123 return 1;
124 }
125 }
126 } else {
127 return result;
128 }
129 }
130 else if (o == null) {
131 throw new ClassCastException("object is null");
132 }
133 throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
134 }
135
136 /** Create the key as an Object.
137 * @return the key as an Object
138 * @since 2.0
139 */
140 protected Object createKeyAsObject() {
141 return new Long(key);
142 }
143
144 /** Write this object. Write the superclass first.
145 * @param out the output
146 */
147 public void writeExternal(ObjectOutput out) throws IOException {
148 super.writeExternal (out);
149 out.writeLong(key);
150 }
151
152 /** Read this object. Read the superclass first.
153 * @param in the input
154 */
155 public void readExternal(ObjectInput in)
156 throws IOException, ClassNotFoundException {
157 super.readExternal (in);
158 key = in.readLong();
159 }
160
161 }