KickJava   Java API By Example, From Geeks To Geeks.

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


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  * IntIdentity.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 int field.
29  * @version 2.0
30  */

31 public class IntIdentity extends SingleFieldIdentity {
32
33     private int key;
34
35     private void construct(int key) {
36         this.key = key;
37         hashCode = hashClassName() ^ key;
38     }
39
40     /** Constructor with class and key.
41      * @param pcClass the class
42      * @param key the key
43      */

44     public IntIdentity (Class JavaDoc pcClass, int key) {
45         super(pcClass);
46         construct(key);
47     }
48
49     /** Constructor with class and key.
50      * @param pcClass the class
51      * @param key the key
52      */

53     public IntIdentity (Class JavaDoc pcClass, Integer JavaDoc key) {
54         super(pcClass);
55         setKeyAsObject(key);
56         construct(key.intValue ());
57     }
58
59
60     /** Constructor with class and key.
61      * @param pcClass the class
62      * @param str the key
63      */

64     public IntIdentity (Class JavaDoc pcClass, String JavaDoc str) {
65         super(pcClass);
66         assertKeyNotNull(str);
67         construct(Integer.parseInt(str));
68     }
69
70     /** Constructor only for Externalizable.
71      */

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

78     public int getKey () {
79         return key;
80     }
81
82     /** Return the String form of the key.
83      * @return the String form of the key
84      */

85     public String JavaDoc toString () {
86         return Integer.toString(key);
87     }
88
89     /** Determine if the other object represents the same object id.
90      * @param obj the other object
91      * @return true if both objects represent the same object id
92      */

93     public boolean equals (Object JavaDoc obj) {
94         if (this == obj) {
95             return true;
96         } else if (!super.equals (obj)) {
97             return false;
98         } else {
99             IntIdentity other = (IntIdentity) obj;
100             return key == other.key;
101         }
102     }
103
104     /** Create the key as an Object.
105      * @return the key as an Object
106      * @since 2.0
107      */

108     protected Object JavaDoc createKeyAsObject() {
109         return new Integer JavaDoc(key);
110     }
111
112     /** Write this object. Write the superclass first.
113      * @param out the output
114      */

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

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