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   * StringIdentity.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 String field.
    30   * @version 2.0
    31   */
    32  public class StringIdentity extends SingleFieldIdentity {
    33
    34      /** The key is stored in the superclass field keyAsObject.
    35       */
    36
    37      /** Constructor with class and key.
    38       * @param pcClass the class
    39       * @param key the key
    40       */
    41      public StringIdentity (Class pcClass, String key) {
    42          super (pcClass);
    43          setKeyAsObject(key);
    44          hashCode = hashClassName() ^ key.hashCode();
    45      }
    46
    47      /** Constructor only for Externalizable.
    48       */
    49      public StringIdentity () {
    50      }
    51
    52      /** Return the key.
    53       * @return the key
    54       */
    55      public String getKey () {
    56          return (String)keyAsObject;
    57      }
    58
    59      /** Return the String form of the key.
    60       * @return the String form of the key
    61       */
    62      public String toString () {
    63          return (String)keyAsObject;
    64      }
    65
    66      /** Determine if the other object represents the same object id.
    67       * @param obj the other object
    68       * @return true if both objects represent the same object id
    69       */
    70      public boolean equals (Object obj) {
    71          if (this == obj) {
    72              return true;
    73          } else if (!super.equals (obj)) {
    74              return false;
    75          } else {
    76              StringIdentity other = (StringIdentity) obj;
    77              return keyAsObject.equals(other.keyAsObject);
    78          }
    79      }
    80
    81      /** Determine the ordering of identity objects.
    82       * @param o Other identity
    83       * @return The relative ordering between the objects
    84       * @since 2.2
    85       */
    86      public int compareTo(Object o) {
    87          if (o instanceof StringIdentity) {
    88              StringIdentity other = (StringIdentity)o;
    89              int result = super.compare(other);
    90              if (result == 0) {
    91                  return ((String)keyAsObject).compareTo((String)other.keyAsObject);
    92              } else {
    93                  return result;
    94              }
    95          }
    96          else if (o == null) {
    97              throw new ClassCastException("object is null");
    98          }
    99          throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
    100     }
    101
    102     /** Write this object. Write the superclass first.
    103      * @param out the output
    104      */
    105     public void writeExternal(ObjectOutput out) throws IOException {
    106         super.writeExternal (out);
    107         out.writeObject(keyAsObject);
    108     }
    109
    110     /** Read this object. Read the superclass first.
    111      * @param in the input
    112      */
    113     public void readExternal(ObjectInput in)
    114 		throws IOException, ClassNotFoundException {
    115         super.readExternal (in);
    116         keyAsObject = (String)in.readObject();
    117     }
    118 }