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