View Javadoc
1 package org.apache.torque.om; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001-2003 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>. 55 */ 56 57 import java.math.BigDecimal; 58 59 /*** 60 * This class can be used as an ObjectKey to uniquely identify an 61 * object within an application where the id consists 62 * of a single entity such a GUID or the value of a db row's primary key. 63 * 64 * @author <a href="mailto:jmcnally@apache.org">John McNally</a> 65 * @author <a href="mailto:stephenh@chase3000.com">Stephen Haberman</a> 66 * @author <a href="mailto:rg@onepercentsoftware.com">Runako Godfrey</a> 67 * @version $Id: NumberKey.java,v 1.19 2003/08/25 21:42:40 mpoeschl Exp $ 68 */ 69 public class NumberKey extends SimpleKey 70 { 71 /*** 72 * Creates a NumberKey whose internal representation will be 73 * set later, through a set method 74 */ 75 public NumberKey() 76 { 77 } 78 79 /*** 80 * Creates a NumberKey equivalent to <code>key</code>. 81 * 82 * @param key the key value 83 */ 84 public NumberKey(String key) 85 { 86 this.key = new BigDecimal(key); 87 } 88 89 /*** 90 * Creates a NumberKey equivalent to <code>key</code>. 91 * 92 * @param key the key value 93 */ 94 public NumberKey(BigDecimal key) 95 { 96 this.key = key; 97 } 98 99 /*** 100 * Creates a NumberKey equivalent to <code>key</code>. 101 * 102 * @param key the key value 103 */ 104 public NumberKey(NumberKey key) 105 { 106 if (key != null) 107 { 108 this.key = key.getValue(); 109 } 110 else 111 { 112 this.key = null; 113 } 114 } 115 116 /*** 117 * Creates a NumberKey equivalent to <code>key</code>. 118 * 119 * @param key the key value 120 */ 121 public NumberKey(long key) 122 { 123 this.key = new BigDecimal(key); 124 } 125 126 /*** 127 * Creates a NumberKey equivalent to <code>key</code>. 128 * 129 * @param key the key value 130 */ 131 public NumberKey(double key) 132 { 133 this.key = new BigDecimal(key); 134 } 135 136 /*** 137 * Creates a NumberKey equivalent to <code>key</code>. 138 * Convenience only. 139 * 140 * @param key the key value 141 */ 142 public NumberKey(int key) 143 { 144 this((long) key); 145 } 146 147 /*** 148 * Creates a NumberKey equivalent to <code>key</code>. 149 * Convenience only. 150 * 151 * @param key the key value 152 */ 153 public NumberKey(Number key) 154 { 155 if (key != null) 156 { 157 this.key = new BigDecimal(key.toString()); 158 } 159 else 160 { 161 this.key = null; 162 } 163 } 164 165 /*** 166 * Sets the internal representation using a String representation 167 * of a number 168 * 169 * @param key the key value 170 * @throws NumberFormatException if key is not a valid number 171 */ 172 public void setValue(String key) throws NumberFormatException 173 { 174 this.key = new BigDecimal(key); 175 } 176 177 /*** 178 * Sets the underlying object 179 * 180 * @param key the key value 181 */ 182 public void setValue(BigDecimal key) 183 { 184 this.key = key; 185 } 186 187 /*** 188 * Sets the internal representation to the same object used by key. 189 * 190 * @param key the key value 191 */ 192 public void setValue(NumberKey key) 193 { 194 this.key = (key == null ? null : key.getValue()); 195 } 196 197 /*** 198 * Access the underlying BigDecimal object. 199 * 200 * @return a <code>BigDecimal</code> value 201 */ 202 public BigDecimal getBigDecimal() 203 { 204 return (BigDecimal) key; 205 } 206 207 /*** 208 * Two ObjectKeys that both contain null values <strong>are not</strong> 209 * considered equal. 210 * 211 * @param keyObj the key to compare values to 212 * @return whether the two objects are equal 213 */ 214 public boolean equals(Object keyObj) 215 { 216 if (keyObj == this) 217 { 218 return true; 219 } 220 221 if (!(keyObj instanceof NumberKey)) 222 { 223 // NumberKeys used to be comparable to Strings. This behavior has 224 // been changed, I don't think it is a good idea to fail silently 225 // as code may be dependent on the old behavior. 226 if (keyObj instanceof String) 227 { 228 throw new IllegalArgumentException( 229 "NumberKeys are not comparable to Strings"); 230 } 231 232 return false; 233 } 234 235 if (getValue() != null) 236 { 237 return getValue().equals(((NumberKey) keyObj).getValue()); 238 } 239 else 240 { 241 // Even if they are both null...still return false. 242 return false; 243 } 244 } 245 246 /*** 247 * @return a hash code based on the value 248 */ 249 public int hashCode() 250 { 251 if (getValue() == null) 252 { 253 return super.hashCode(); 254 } 255 else 256 { 257 return getValue().hashCode(); 258 } 259 } 260 261 /*** 262 * @param o the comparison value 263 * @return a numeric comparison of the two values 264 */ 265 public int compareTo(Object o) 266 { 267 return getBigDecimal().compareTo(((NumberKey) o).getBigDecimal()); 268 } 269 270 /*** 271 * Invokes the toString() method on the object. An empty string 272 * is returned is the value is null. 273 * 274 * @return a String representation of the key value 275 */ 276 public String toString() 277 { 278 if (key != null) 279 { 280 return key.toString(); 281 } 282 return ""; 283 } 284 285 /*** 286 * Returns the value of this NumberKey as a byte. This value is subject 287 * to the conversion rules set out in 288 * {@link java.math.BigDecimal#byteValue()} 289 * 290 * @return the NumberKey converted to a byte 291 */ 292 public byte byteValue() 293 { 294 return getBigDecimal().byteValue(); 295 } 296 297 /*** 298 * Returns the value of this NumberKey as an int. This value is subject 299 * to the conversion rules set out in 300 * {@link java.math.BigDecimal#intValue()}, importantly any fractional part 301 * will be discarded and if the underlying value is too big to fit in an 302 * int, only the low-order 32 bits are returned. Note that this 303 * conversion can lose information about the overall magnitude and 304 * precision of the NumberKey value as well as return a result with the 305 * opposite sign. 306 * 307 * @return the NumberKey converted to an int 308 */ 309 public int intValue() 310 { 311 return getBigDecimal().intValue(); 312 } 313 314 /*** 315 * Returns the value of this NumberKey as a short. This value is subject 316 * to the conversion rules set out in 317 * {@link java.math.BigDecimal#intValue()}, importantly any fractional part 318 * will be discarded and if the underlying value is too big to fit 319 * in a long, only the low-order 64 bits are returned. Note that this 320 * conversion can lose information about the overall magnitude and 321 * precision of the NumberKey value as well as return a result with the 322 * opposite sign. 323 * 324 * @return the NumberKey converted to a short 325 */ 326 public short shortValue() 327 { 328 return getBigDecimal().shortValue(); 329 } 330 331 /*** 332 * Returns the value of this NumberKey as a long. This value is subject 333 * to the conversion rules set out in 334 * {@link java.math.BigDecimal#intValue()} 335 * 336 * @return the NumberKey converted to a long 337 */ 338 public long longValue() 339 { 340 return getBigDecimal().longValue(); 341 } 342 343 /*** 344 * Returns the value of this NumberKey as a float. This value is subject to 345 * the conversion rules set out in 346 * {@link java.math.BigDecimal#floatValue()}, most importantly if the 347 * underlying value has too great a magnitude to represent as a 348 * float, it will be converted to Float.NEGATIVE_INFINITY 349 * or Float.POSITIVE_INFINITY as appropriate. 350 * 351 * @return the NumberKey converted to a float 352 */ 353 public float floatValue() 354 { 355 return getBigDecimal().floatValue(); 356 } 357 358 /*** 359 * Returns the value of this NumberKey as a double. This value is subject 360 * to the conversion rules set out in 361 * {@link java.math.BigDecimal#doubleValue()}, most importantly if the 362 * underlying value has too great a magnitude to represent as a 363 * double, it will be converted to Double.NEGATIVE_INFINITY 364 * or Double.POSITIVE_INFINITY as appropriate. 365 * 366 * @return the NumberKey converted to a double 367 */ 368 public double doubleValue() 369 { 370 return getBigDecimal().doubleValue(); 371 } 372 }

This page was automatically generated by Maven