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   * ShortIdentity.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 short field.
29   * @version 2.0
30   */
31  public class ShortIdentity
32  	extends SingleFieldIdentity
33  {
34  	private short key;
35  
36      private void construct(short key) {
37          this.key = key;
38          hashCode = hashClassName() ^ key;
39      }
40  
41      /*** Constructor with class and key.
42       * @param pcClass the class
43       * @param key the key
44       */
45      public ShortIdentity (Class pcClass, short key) {
46          super(pcClass);
47          construct(key);
48      }
49  
50      /*** Constructor with class and key.
51       * @param pcClass the class
52       * @param key the key
53       */
54      public ShortIdentity (Class pcClass, Short key) {
55          super(pcClass);
56          setKeyAsObject(key);
57          construct(key.shortValue());
58      }
59  
60      /*** Constructor with class and key.
61       * @param pcClass the class
62       * @param str the key
63       */
64      public ShortIdentity (Class pcClass, String str) {
65          super(pcClass);
66          assertKeyNotNull(str);
67          construct(Short.parseShort (str));
68      }
69  
70      /*** Constructor only for Externalizable.
71       */
72      public ShortIdentity () {
73      }
74  
75      /*** Return the key.
76       * @return the key
77       */
78      public short getKey () {
79          return key;
80      }
81  
82      /*** Return the String form of the key.
83       * @return the String form of the key
84       */
85      public String toString () {
86          return Short.toString(key);
87      }
88  
89      /*** Determine if the other object represents the same object id.
90       * @param obj the other object
91       * @return true if both objects represent the same object id
92       */
93      public boolean equals (Object obj) {
94          if (this == obj) {
95              return true;
96          } else if (!super.equals (obj)) {
97              return false;
98          } else {
99              ShortIdentity other = (ShortIdentity) obj;
100             return key == other.key;
101         }
102     }
103 
104     /*** Create the key as an Object.
105      * @return the key as an Object
106      * @since 2.0
107      */
108     protected Object createKeyAsObject() {
109         return new Short(key);
110     }
111 
112     /*** Write this object. Write the superclass first.
113      * @param out the output
114      */
115     public void writeExternal(ObjectOutput out) throws IOException {
116         super.writeExternal (out);
117         out.writeShort(key);
118     }
119 
120     /*** Read this object. Read the superclass first.
121      * @param in the input
122      */
123     public void readExternal(ObjectInput in)
124 		throws IOException, ClassNotFoundException {
125         super.readExternal (in);
126         key = in.readShort();
127     }
128 }