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   * CharIdentity.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.spi.I18NHelper;
29  
30  /*** This class is for identity with a single character field.
31   * @version 2.0
32   */
33  public class CharIdentity extends SingleFieldIdentity {
34  
35      /*** The Internationalization message helper.
36       */
37      private static I18NHelper msg = I18NHelper.getInstance ("javax.jdo.Bundle"); //NOI18N
38  
39      /*** The key.
40       */
41      private char key;
42  
43      private void construct(char key) {
44          this.key = key;
45          hashCode = hashClassName() ^ key;
46      }
47  
48      /*** Constructor with class and key.
49       * @param pcClass the target class
50       * @param key the key
51       */
52      public CharIdentity (Class pcClass, char key) {
53          super (pcClass);
54          construct(key);
55      }
56  
57      /*** Constructor with class and key.
58       * @param pcClass the target class
59       * @param key the key
60       */
61      public CharIdentity (Class pcClass, Character key) {
62          super (pcClass);
63          setKeyAsObject(key);
64          construct(key.charValue());
65      }
66  
67      /*** Constructor with class and key. The String must have exactly one
68       * character.
69       * @param pcClass the target class
70       * @param str the key
71       */
72      public CharIdentity (Class pcClass, String str) {
73          super(pcClass);
74          assertKeyNotNull(str);
75          if (str.length() != 1) 
76              throw new IllegalArgumentException(
77                  msg.msg("EXC_StringWrongLength")); //NOI18N
78          construct(str.charAt(0));
79      }
80  
81      /*** Constructor only for Externalizable.
82       */
83      public CharIdentity () {
84      }
85  
86      /*** Return the key.
87       * @return the key
88       */
89      public char getKey () {
90          return key;
91      }
92  
93      /*** Return the String form of the key.
94       * @return the String form of the key
95       */
96      public String toString () {
97          return String.valueOf(key);
98      }
99  
100     /*** Determine if the other object represents the same object id.
101      * @param obj the other object
102      * @return true if both objects represent the same object id
103      */
104     public boolean equals (Object obj) {
105         if (this == obj) {
106             return true;
107         } else if (!super.equals (obj)) {
108             return false;
109         } else {
110             CharIdentity other = (CharIdentity) obj;
111             return key == other.key;
112         }
113     }
114 
115     /*** Create the key as an Object.
116      * @return the key as an Object
117      * @since 2.0
118      */
119     protected Object createKeyAsObject() {
120         return new Character(key);
121     }
122 
123     /*** Write this object. Write the superclass first.
124      * @param out the output
125      */
126     public void writeExternal(ObjectOutput out) throws IOException {
127         super.writeExternal (out);
128         out.writeChar(key);
129     }
130 
131     /*** Read this object. Read the superclass first.
132      * @param in the input
133      */
134     public void readExternal(ObjectInput in)
135 		throws IOException, ClassNotFoundException {
136         super.readExternal (in);
137         key = in.readChar();
138     }
139     
140     private void computeHashCode() {
141         hashCode = hashClassName() ^ key;
142     }
143 }