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