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.math.BigDecimal;
23  import java.sql.Types;
24  
25  /**
26   * This class can be used as an ObjectKey to uniquely identify an
27   * object within an application where the id  consists
28   * of a single entity such a GUID or the value of a db row's primary key.
29   *
30   * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
31   * @author <a href="mailto:stephenh@chase3000.com">Stephen Haberman</a>
32   * @author <a href="mailto:rg@onepercentsoftware.com">Runako Godfrey</a>
33   * @version $Id: NumberKey.java 1351125 2012-06-17 16:51:03Z tv $
34   */
35  public class NumberKey extends SimpleKey
36  {
37      /**
38       * Serial version
39       */
40      private static final long serialVersionUID = -5566819786708264162L;
41  
42      /**
43       * Creates a NumberKey whose internal representation will be
44       * set later, through a set method
45       */
46      public NumberKey()
47      {
48          // empty
49      }
50  
51      /**
52       * Creates a NumberKey equivalent to <code>key</code>.
53       *
54       * @param key the key value
55       */
56      public NumberKey(String key)
57      {
58          if (key != null)
59          {
60              this.key = new BigDecimal(key);
61          }
62          else
63          {
64              this.key = null;
65          }
66      }
67  
68      /**
69       * Creates a NumberKey equivalent to <code>key</code>.
70       *
71       * @param key the key value
72       */
73      public NumberKey(BigDecimal key)
74      {
75          this.key = key;
76      }
77  
78      /**
79       * Creates a NumberKey equivalent to <code>key</code>.
80       *
81       * @param key the key value
82       */
83      public NumberKey(NumberKey key)
84      {
85          if (key != null)
86          {
87              this.key = key.getValue();
88          }
89          else
90          {
91              this.key = null;
92          }
93      }
94  
95      /**
96       * Creates a NumberKey equivalent to <code>key</code>.
97       *
98       * @param key the key value
99       */
100     public NumberKey(long key)
101     {
102         this.key = BigDecimal.valueOf(key);
103     }
104 
105     /**
106      * Creates a NumberKey equivalent to <code>key</code>.
107      *
108      * @param key the key value
109      */
110     public NumberKey(double key)
111     {
112         this.key = new BigDecimal(key);
113     }
114 
115     /**
116      * Creates a NumberKey equivalent to <code>key</code>.
117      * Convenience only.
118      *
119      * @param key the key value
120      */
121     public NumberKey(int key)
122     {
123         this((long) key);
124     }
125 
126     /**
127      * Creates a NumberKey equivalent to <code>key</code>.
128      * Convenience only.
129      *
130      * @param key the key value
131      */
132     public NumberKey(Number key)
133     {
134         if (key != null)
135         {
136             this.key = new BigDecimal(key.toString());
137         }
138         else
139         {
140             this.key = null;
141         }
142     }
143 
144     /**
145      * Sets the internal representation using a String representation
146      * of a number.
147      *
148      * @param key the key value
149      * @throws NumberFormatException if key is not a valid number
150      */
151     public void setValue(String key)
152     {
153         if (key != null)
154         {
155             this.key = new BigDecimal(key);
156         }
157         else
158         {
159             this.key = null;
160         }
161     }
162 
163     /**
164      * Sets the underlying object.
165      *
166      * @param key the key value.
167      */
168     public void setValue(BigDecimal key)
169     {
170         this.key = key;
171     }
172 
173     /**
174      * Sets the internal representation to the same object used by key.
175      *
176      * @param key the key value
177      */
178     public void setValue(NumberKey key)
179     {
180         this.key = (key == null ? null : key.getValue());
181     }
182 
183     /**
184      * Returns the JDBC type of the key
185      * as defined in <code>java.sql.Types</code>.
186      *
187      * @return <code>Types.NUMERIC</code>.
188      */
189     public int getJdbcType()
190     {
191         return Types.NUMERIC;
192     }
193 
194     /**
195      * Access the underlying BigDecimal object.
196      *
197      * @return a <code>BigDecimal</code> value
198      */
199     public BigDecimal getBigDecimal()
200     {
201         return (BigDecimal) key;
202     }
203 
204     /**
205      * @return a hash code based on the value
206      */
207     public int hashCode()
208     {
209         if (getValue() == null)
210         {
211             return super.hashCode();
212         }
213         else
214         {
215             return getValue().hashCode();
216         }
217     }
218 
219     /**
220      * @param o the comparison value
221      * @return a numeric comparison of the two values
222      */
223     public int compareTo(Object o)
224     {
225         return getBigDecimal().compareTo(((NumberKey) o).getBigDecimal());
226     }
227 
228     /**
229      * Get a String representation of this key.
230      *
231      * @return a String representation of this key,
232      *         or an empty String if the value is null.
233      */
234     @Override
235     public String toString()
236     {
237         if (key != null)
238         {
239             return key.toString();
240         }
241         return "";
242     }
243 
244     /**
245      * Returns the value of this NumberKey as a byte. This value is subject
246      * to the conversion rules set out in
247      * {@link java.math.BigDecimal#byteValue()}
248      *
249      * @return the NumberKey converted to a byte
250      */
251     public byte byteValue()
252     {
253         return getBigDecimal().byteValue();
254     }
255 
256     /**
257      * Returns the value of this NumberKey as an int. This value is subject
258      * to the conversion rules set out in
259      * {@link java.math.BigDecimal#intValue()}, importantly any fractional part
260      * will be discarded and if the underlying value is too big to fit in an
261      * int, only the low-order 32 bits are returned. Note that this
262      * conversion can lose information about the overall magnitude and
263      * precision of the NumberKey value as well as return a result with the
264      * opposite sign.
265      *
266      * @return the NumberKey converted to an int
267      */
268     public int intValue()
269     {
270         return getBigDecimal().intValue();
271     }
272 
273     /**
274      * Returns the value of this NumberKey as a short. This value is subject
275      * to the conversion rules set out in
276      * {@link java.math.BigDecimal#intValue()}, importantly any fractional part
277      *  will be discarded and if the underlying value is too big to fit
278      * in a long, only the low-order 64 bits are returned. Note that this
279      * conversion can lose information about the overall magnitude and
280      * precision of the NumberKey value as well as return a result with the
281      * opposite sign.
282      *
283      * @return the NumberKey converted to a short
284      */
285     public short shortValue()
286     {
287         return getBigDecimal().shortValue();
288     }
289 
290     /**
291      * Returns the value of this NumberKey as a long. This value is subject
292      * to the conversion rules set out in
293      * {@link java.math.BigDecimal#intValue()}
294      *
295      * @return the NumberKey converted to a long
296      */
297     public long longValue()
298     {
299         return getBigDecimal().longValue();
300     }
301 
302     /**
303      * Returns the value of this NumberKey as a float. This value is subject to
304      * the conversion rules set out in
305      * {@link java.math.BigDecimal#floatValue()}, most importantly if the
306      * underlying value has too great a magnitude to represent as a
307      * float, it will be converted to Float.NEGATIVE_INFINITY
308      * or Float.POSITIVE_INFINITY as appropriate.
309      *
310      * @return the NumberKey converted to a float
311      */
312     public float floatValue()
313     {
314         return getBigDecimal().floatValue();
315     }
316 
317     /**
318      * Returns the value of this NumberKey as a double. This value is subject
319      * to the conversion rules set out in
320      * {@link java.math.BigDecimal#doubleValue()}, most importantly if the
321      * underlying value has too great a magnitude to represent as a
322      * double, it will be converted to Double.NEGATIVE_INFINITY
323      * or Double.POSITIVE_INFINITY as appropriate.
324      *
325      * @return the NumberKey converted to a double
326      */
327     public double doubleValue()
328     {
329         return getBigDecimal().doubleValue();
330     }
331 }