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   * IntIdentity.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 int field.
    30   * @version 2.0
    31   */
    32  public class IntIdentity extends SingleFieldIdentity {
    33
    34      private int key;
    35
    36      private void construct(int 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 IntIdentity (Class pcClass, int 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 IntIdentity (Class pcClass, Integer key) {
    55          super(pcClass);
    56          setKeyAsObject(key);
    57          construct(key.intValue ());
    58      }
    59
    60
    61      /** Constructor with class and key.
    62       * @param pcClass the class
    63       * @param str the key
    64       */
    65      public IntIdentity (Class pcClass, String str) {
    66          super(pcClass);
    67          assertKeyNotNull(str);
    68          construct(Integer.parseInt(str));
    69      }
    70
    71      /** Constructor only for Externalizable.
    72       */
    73      public IntIdentity () {
    74      }
    75
    76      /** Return the key.
    77       * @return the key
    78       */
    79      public int getKey () {
    80          return key;
    81      }
    82
    83      /** Return the String form of the key.
    84       * @return the String form of the key
    85       */
    86      public String toString () {
    87          return Integer.toString(key);
    88      }
    89
    90      /** Determine if the other object represents the same object id.
    91       * @param obj the other object
    92       * @return true if both objects represent the same object id
    93       */
    94      public boolean equals (Object obj) {
    95          if (this == obj) {
    96              return true;
    97          } else if (!super.equals (obj)) {
    98              return false;
    99          } else {
    100             IntIdentity other = (IntIdentity) obj;
    101             return key == other.key;
    102         }
    103     }
    104
    105     /** Determine the ordering of identity objects.
    106      * @param o Other identity
    107      * @return The relative ordering between the objects
    108      * @since 2.2
    109      */
    110     public int compareTo(Object o) {
    111         if (o instanceof IntIdentity) {
    112         	IntIdentity other = (IntIdentity)o;
    113             int result = super.compare(other);
    114             if (result == 0) {
    115                 return (key - other.key);
    116             } else {
    117                 return result;
    118             }
    119         }
    120        else if (o == null) {
    121             throw new ClassCastException("object is null");
    122         }
    123         throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
    124     }
    125
    126     /** Create the key as an Object.
    127      * @return the key as an Object
    128      * @since 2.0
    129      */
    130     protected Object createKeyAsObject() {
    131         return new Integer(key);
    132     }
    133
    134     /** Write this object. Write the superclass first.
    135      * @param out the output
    136      */
    137     public void writeExternal(ObjectOutput out) throws IOException {
    138         super.writeExternal (out);
    139         out.writeInt(key);
    140     }
    141
    142     /** Read this object. Read the superclass first.
    143      * @param in the input
    144      */
    145     public void readExternal(ObjectInput in)
    146 		throws IOException, ClassNotFoundException {
    147         super.readExternal (in);
    148         key = in.readInt();
    149     }
    150 }