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     /** Determine the ordering of identity objects.
109      * @param o Other identity
110      * @return The relative ordering between the objects
111      * @since 2.2
112      */
113     public int compareTo(Object o) {
114         if (o instanceof ByteIdentity) {
115         	ByteIdentity other = (ByteIdentity)o;
116             int result = super.compare(other);
117             if (result == 0) {
118                 return (key - other.key);
119             } else {
120                 return result;
121             }
122         }
123         else if (o == null) {
124             throw new ClassCastException("object is null");
125         }
126         throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
127     }
128 
129     /** Create the key as an Object.
130      * @return the key as an Object
131      * @since 2.0
132      */
133     protected Object createKeyAsObject() {
134         return new Byte(key);
135     }
136 
137     /** Write this object. Write the superclass first.
138      * @param out the output
139      */
140     public void writeExternal(ObjectOutput out) throws IOException {
141         super.writeExternal (out);
142         out.writeByte (key);
143     }
144 
145     /** Read this object. Read the superclass first.
146      * @param in the input
147      */
148     public void readExternal(ObjectInput in)
149 		throws IOException, ClassNotFoundException {
150         super.readExternal (in);
151         key = in.readByte ();
152     }
153 }