View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software 
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License.
16   */
17  
18  /*
19   * StringIdentity.java
20   *
21   */
22   
23  package javax.jdo.identity;
24  
25  import java.io.IOException;
26  import java.io.ObjectInput;
27  import java.io.ObjectOutput;
28  
29  /*** This class is for identity with a single String field.
30   * @version 2.0
31   */
32  public class StringIdentity extends SingleFieldIdentity {
33      
34      /*** The key is stored in the superclass field keyAsObject.
35       */
36      
37      /*** Constructor with class and key.
38       * @param pcClass the class
39       * @param key the key
40       */
41      public StringIdentity (Class pcClass, String key) {
42          super (pcClass);
43          setKeyAsObject(key);
44          hashCode = hashClassName() ^ key.hashCode();
45      }
46  
47      /*** Constructor only for Externalizable.
48       */
49      public StringIdentity () {
50      }
51  
52      /*** Return the key.
53       * @return the key
54       */
55      public String getKey () {
56          return (String)keyAsObject;
57      }
58  
59      /*** Return the String form of the key.
60       * @return the String form of the key
61       */
62      public String toString () {
63          return (String)keyAsObject;
64      }
65  
66      /*** Determine if the other object represents the same object id.
67       * @param obj the other object
68       * @return true if both objects represent the same object id
69       */
70      public boolean equals (Object obj) {
71          if (this == obj) {
72              return true;
73          } else if (!super.equals (obj)) {
74              return false;
75          } else {
76              StringIdentity other = (StringIdentity) obj;
77              return keyAsObject.equals(other.keyAsObject);
78          }
79      }
80  
81      /*** Write this object. Write the superclass first.
82       * @param out the output
83       */
84      public void writeExternal(ObjectOutput out) throws IOException {
85          super.writeExternal (out);
86          out.writeObject(keyAsObject);
87      }
88  
89      /*** Read this object. Read the superclass first.
90       * @param in the input
91       */
92      public void readExternal(ObjectInput in)
93  		throws IOException, ClassNotFoundException {
94          super.readExternal (in);
95          keyAsObject = (String)in.readObject();
96      }
97  }