KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SingleFieldIdentity.java
19  *
20  */

21  
22 package javax.jdo.identity;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28
29 import javax.jdo.JDOFatalInternalException;
30 import javax.jdo.JDONullIdentityException;
31
32 import javax.jdo.spi.I18NHelper;
33
34 /** This class is the abstract base class for all single field identity
35  * classes. A common case of application identity uses exactly one
36  * persistent field in the class to represent identity. In this case,
37  * the application can use a standard JDO class instead of creating
38  * a new user-defined class for the purpose.
39  * @version 2.0
40  */

41 public abstract class SingleFieldIdentity
42     implements Externalizable JavaDoc {
43     
44     /** The Internationalization message helper.
45      */

46     protected static I18NHelper msg = I18NHelper.getInstance ("javax.jdo.Bundle"); //NOI18N
47

48     /** The class of the target object.
49      */

50     transient private Class JavaDoc targetClass;
51     
52     /** The name of the class of the target object.
53      */

54     private String JavaDoc targetClassName;
55
56     /** The hashCode.
57      */

58     protected int hashCode;
59     
60     /** The key as an Object.
61      */

62     protected Object JavaDoc keyAsObject;
63
64     /** Constructor with target class.
65      * @param pcClass the class of the target
66      * @since 2.0
67      */

68     protected SingleFieldIdentity(Class JavaDoc pcClass) {
69         if (pcClass == null)
70             throw new NullPointerException JavaDoc();
71         targetClass = pcClass;
72         targetClassName = pcClass.getName();
73     }
74
75     /** Constructor only for Externalizable.
76      */

77     public SingleFieldIdentity () {
78     }
79
80     /** Set the given key as the key for this instance.
81      * Compute the hash code for the instance.
82      */

83     protected void setKeyAsObject(Object JavaDoc key) {
84         assertKeyNotNull(key);
85         keyAsObject = key;
86     }
87
88     /** Assert that the key is not null. Throw a JDONullIdentityException
89      * if the given key is null.
90      */

91     protected void assertKeyNotNull(Object JavaDoc key) {
92         if (key == null) {
93             throw new JDONullIdentityException(
94                 msg.msg("EXC_SingleFieldIdentityNullParameter")); //NOI18N
95
}
96     }
97     
98     /** Return the target class.
99      * @return the target class.
100      * @since 2.0
101      */

102     public Class JavaDoc getTargetClass() {
103         return targetClass;
104     }
105
106     /** Return the target class name.
107      * @return the target class name.
108      * @since 2.0
109      */

110     public String JavaDoc getTargetClassName() {
111         return targetClassName;
112     }
113
114     /** Return the key as an Object. The method is synchronized to avoid
115      * race conditions in multi-threaded environments.
116      * @return the key as an Object.
117      * @since 2.0
118      */

119     public synchronized Object JavaDoc getKeyAsObject() {
120         if (keyAsObject == null) {
121             keyAsObject = createKeyAsObject();
122         }
123         return keyAsObject;
124     }
125     
126     /** Create the key as an Object.
127      * @return the key as an Object;
128      * @since 2.0
129      */

130     protected Object JavaDoc createKeyAsObject() {
131         throw new JDOFatalInternalException
132                 (msg.msg("EXC_CreateKeyAsObjectMustNotBeCalled"));
133     }
134     
135     /** Check the class and class name and object type. If restored
136      * from serialization, class will be null so compare class name.
137      * @param obj the other object
138      * @return true if the class or class name is the same
139      * @since 2.0
140      */

141     public boolean equals(Object JavaDoc obj) {
142         if (this == obj) {
143             return true;
144         } else if (obj == null || this.getClass() != obj.getClass()) {
145             return false;
146         } else {
147             SingleFieldIdentity other = (SingleFieldIdentity) obj;
148             if (targetClass != null && targetClass == other.targetClass)
149                 return true;
150             return targetClassName.equals (other.targetClassName);
151         }
152     }
153
154     /** Return the hash code of the class name.
155      * @return the hash code of the class name
156      */

157     protected int hashClassName() {
158         return targetClassName.hashCode();
159     }
160     
161     /** Return the cached hash code.
162      * @return the cached hash code.
163      */

164     public int hashCode() {
165         return hashCode;
166     }
167     
168     /** Write to the output stream.
169      * @param out the stream
170      */

171     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
172         out.writeObject(targetClassName);
173         out.writeInt(hashCode);
174     }
175
176     /** Read from the input stream.
177      * @return a new instance with the target class name set
178      * @since 2.0
179      */

180     public void readExternal(ObjectInput JavaDoc in)
181             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
182         targetClass = null;
183         targetClassName = (String JavaDoc)in.readObject();
184         hashCode = in.readInt();
185     }
186 }
187
Popular Tags