KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jdo > identity > LongIdentity


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 /*
18  * LongIdentity.java
19  *
20  */

21  
22 package javax.jdo.identity;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInput JavaDoc;
26 import java.io.ObjectOutput JavaDoc;
27
28 /** This class is for identity with a single long field.
29  * @version 2.0
30  */

31 public class LongIdentity extends SingleFieldIdentity {
32     
33     /** The key.
34      */

35     private long key;
36
37     private void construct(long key) {
38         this.key = key;
39         hashCode = hashClassName() ^ (int)key;
40     }
41
42     /** Constructor with class and key.
43      * @param pcClass the class
44      * @param key the key
45      */

46     public LongIdentity (Class JavaDoc pcClass, long 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 LongIdentity (Class JavaDoc pcClass, Long JavaDoc key) {
56         super(pcClass);
57         setKeyAsObject(key);
58         construct(key.longValue());
59     }
60
61     /** Constructor with class and key.
62      * @param pcClass the class
63      * @param str the key
64      */

65     public LongIdentity (Class JavaDoc pcClass, String JavaDoc str) {
66         super(pcClass);
67         assertKeyNotNull(str);
68         construct(Long.parseLong(str));
69     }
70
71     /** Constructor only for Externalizable.
72      */

73     public LongIdentity () {
74     }
75
76     /** Return the key.
77      * @return the key
78      */

79     public long 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 JavaDoc toString () {
87         return Long.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 JavaDoc obj) {
95         if (this == obj) {
96             return true;
97         } else if (!super.equals (obj)) {
98             return false;
99         } else {
100             LongIdentity other = (LongIdentity) 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 JavaDoc createKeyAsObject() {
110         return new Long JavaDoc(key);
111     }
112
113     /** Write this object. Write the superclass first.
114      * @param out the output
115      */

116     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
117         super.writeExternal (out);
118         out.writeLong(key);
119     }
120
121     /** Read this object. Read the superclass first.
122      * @param in the input
123      */

124     public void readExternal(ObjectInput JavaDoc in)
125         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
126         super.readExternal (in);
127         key = in.readLong();
128     }
129
130 }
131
Popular Tags