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