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