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   * CharIdentity.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  import javax.jdo.spi.I18NHelper;
    30
    31  /** This class is for identity with a single character field.
    32   * @version 2.0
    33   */
    34  public class CharIdentity extends SingleFieldIdentity {
    35
    36      /** The Internationalization message helper.
    37       */
    38      private static I18NHelper msg = I18NHelper.getInstance ("javax.jdo.Bundle"); //NOI18N
    39
    40      /** The key.
    41       */
    42      private char key;
    43
    44      private void construct(char key) {
    45          this.key = key;
    46          hashCode = hashClassName() ^ key;
    47      }
    48
    49      /** Constructor with class and key.
    50       * @param pcClass the target class
    51       * @param key the key
    52       */
    53      public CharIdentity (Class pcClass, char key) {
    54          super (pcClass);
    55          construct(key);
    56      }
    57
    58      /** Constructor with class and key.
    59       * @param pcClass the target class
    60       * @param key the key
    61       */
    62      public CharIdentity (Class pcClass, Character key) {
    63          super (pcClass);
    64          setKeyAsObject(key);
    65          construct(key.charValue());
    66      }
    67
    68      /** Constructor with class and key. The String must have exactly one
    69       * character.
    70       * @param pcClass the target class
    71       * @param str the key
    72       */
    73      public CharIdentity (Class pcClass, String str) {
    74          super(pcClass);
    75          assertKeyNotNull(str);
    76          if (str.length() != 1)
    77              throw new IllegalArgumentException(
    78                  msg.msg("EXC_StringWrongLength")); //NOI18N
    79          construct(str.charAt(0));
    80      }
    81
    82      /** Constructor only for Externalizable.
    83       */
    84      public CharIdentity () {
    85      }
    86
    87      /** Return the key.
    88       * @return the key
    89       */
    90      public char getKey () {
    91          return key;
    92      }
    93
    94      /** Return the String form of the key.
    95       * @return the String form of the key
    96       */
    97      public String toString () {
    98          return String.valueOf(key);
    99      }
    100
    101     /** Determine if the other object represents the same object id.
    102      * @param obj the other object
    103      * @return true if both objects represent the same object id
    104      */
    105     public boolean equals (Object obj) {
    106         if (this == obj) {
    107             return true;
    108         } else if (!super.equals (obj)) {
    109             return false;
    110         } else {
    111             CharIdentity other = (CharIdentity) obj;
    112             return key == other.key;
    113         }
    114     }
    115
    116     /** Determine the ordering of identity objects.
    117      * @param o Other identity
    118      * @return The relative ordering between the objects
    119      * @since 2.2
    120      */
    121     public int compareTo(Object o) {
    122         if (o instanceof CharIdentity) {
    123         	CharIdentity other = (CharIdentity)o;
    124             int result = super.compare(other);
    125             if (result == 0) {
    126                 return (key - other.key);
    127             } else {
    128                 return result;
    129             }
    130         }
    131         else if (o == null) {
    132             throw new ClassCastException("object is null");
    133         }
    134         throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
    135     }
    136
    137     /** Create the key as an Object.
    138      * @return the key as an Object
    139      * @since 2.0
    140      */
    141     protected Object createKeyAsObject() {
    142         return new Character(key);
    143     }
    144
    145     /** Write this object. Write the superclass first.
    146      * @param out the output
    147      */
    148     public void writeExternal(ObjectOutput out) throws IOException {
    149         super.writeExternal (out);
    150         out.writeChar(key);
    151     }
    152
    153     /** Read this object. Read the superclass first.
    154      * @param in the input
    155      */
    156     public void readExternal(ObjectInput in)
    157 		throws IOException, ClassNotFoundException {
    158         super.readExternal (in);
    159         key = in.readChar();
    160     }
    161 }