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