View Javadoc

1   package org.apache.torque.om;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Serializable;
23  
24  import org.apache.torque.TorqueException;
25  
26  /**
27   * This class can be used to uniquely identify an object within
28   * an application.  There are four subclasses: StringKey, NumberKey,
29   * and DateKey, and ComboKey which is a Key made up of a combination
30   * ofthe first three.
31   *
32   * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
33   * @version $Id: ObjectKey.java 1206841 2011-11-27 20:46:17Z tfischer $
34   */
35  public abstract class ObjectKey implements Serializable, Comparable<Object>
36  {
37      /** Version id for serializing. */
38      private static final long serialVersionUID = 1L;
39  
40      /**
41       * The underlying key value.
42       */
43      protected Object key;
44  
45      /**
46       * Initializes the internal key value to <code>null</code>.
47       */
48      protected ObjectKey()
49      {
50          key = null;
51      }
52  
53      /**
54       * Returns the hashcode of the underlying value (key), if key is
55       * not null. Otherwise calls Object.hashCode()
56       *
57       * @return an <code>int</code> value
58       */
59      public int hashCode()
60      {
61          if (key == null)
62          {
63              return super.hashCode();
64          }
65          return key.hashCode();
66      }
67  
68      /**
69       * Returns whether this ObjekctKey is equal to another Object.
70       * obj is equal to this ObjectKey if obj has the same class
71       * as this ObjectKey and contains the same information
72       * this key contains.
73       * Two ObjectKeys that both contain null values are not considered equal.
74       *
75       * @param obj the comparison value.
76       *
77       * @return whether the two objects are equal.
78       */
79      @Override
80      public boolean equals(Object obj)
81      {
82          if (this == obj)
83          {
84              return true;
85          }
86          if (obj == null)
87          {
88              return false;
89          }
90          if (obj.getClass() != this.getClass())
91          {
92              return false;
93          }
94  
95          ObjectKey objectKey = (ObjectKey) obj;
96          if (key == null)
97          {
98              return false;
99          }
100         return key.equals(objectKey.getValue());
101     }
102 
103     /**
104      * Get the underlying object.
105      *
106      * @return the underlying object
107      */
108     public Object getValue()
109     {
110         return key;
111     }
112 
113     /**
114      * Returns the JDBC type of the key
115      * as defined in <code>java.sql.Types</code>.
116      *
117      * @return the JDBC type of the key.
118      */
119     public abstract int getJdbcType();
120 
121     /**
122      * Appends a String representation of the key to a buffer.
123      *
124      * @param sb a <code>StringBuffer</code>
125      */
126     public void appendTo(StringBuffer sb)
127     {
128         sb.append(this.toString());
129     }
130 
131     /**
132      * Implements the compareTo method.
133      *
134      * @param obj the object to compare to this object
135      * @return a numeric comparison of the two values
136      */
137     public int compareTo(Object obj)
138     {
139         return toString().compareTo(obj.toString());
140     }
141 
142     /**
143      * Reset the underlying object using a String.
144      *
145      * @param s a <code>String</code> value
146      * @exception TorqueException if an error occurs
147      */
148     public abstract void setValue(String s) throws TorqueException;
149 }