View Javadoc

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   * StringIdentity.java
19   *
20   */
21   
22  package javax.jdo.identity;
23  
24  import java.io.IOException;
25  import java.io.ObjectInput;
26  import java.io.ObjectOutput;
27  
28  /*** This class is for identity with a single String field.
29   * @version 2.0
30   */
31  public class StringIdentity extends SingleFieldIdentity {
32      
33      /*** The key is stored in the superclass field keyAsObject.
34       */
35      
36      /*** Constructor with class and key.
37       * @param pcClass the class
38       * @param key the key
39       */
40      public StringIdentity (Class pcClass, String key) {
41          super (pcClass);
42          setKeyAsObject(key);
43          hashCode = hashClassName() ^ key.hashCode();
44      }
45  
46      /*** Constructor only for Externalizable.
47       */
48      public StringIdentity () {
49      }
50  
51      /*** Return the key.
52       * @return the key
53       */
54      public String getKey () {
55          return (String)keyAsObject;
56      }
57  
58      /*** Return the String form of the key.
59       * @return the String form of the key
60       */
61      public String toString () {
62          return (String)keyAsObject;
63      }
64  
65      /*** Determine if the other object represents the same object id.
66       * @param obj the other object
67       * @return true if both objects represent the same object id
68       */
69      public boolean equals (Object obj) {
70          if (this == obj) {
71              return true;
72          } else if (!super.equals (obj)) {
73              return false;
74          } else {
75              StringIdentity other = (StringIdentity) obj;
76              return keyAsObject.equals(other.keyAsObject);
77          }
78      }
79  
80      /*** Write this object. Write the superclass first.
81       * @param out the output
82       */
83      public void writeExternal(ObjectOutput out) throws IOException {
84          super.writeExternal (out);
85          out.writeObject(keyAsObject);
86      }
87  
88      /*** Read this object. Read the superclass first.
89       * @param in the input
90       */
91      public void readExternal(ObjectInput in)
92  		throws IOException, ClassNotFoundException {
93          super.readExternal (in);
94          keyAsObject = (String)in.readObject();
95      }
96  }