Changeset 1391 for branches/GNU/src/gcc/libjava/java/lang/Long.java
- Timestamp:
- Apr 27, 2004, 8:39:34 PM (22 years ago)
- Location:
- branches/GNU/src/gcc
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
libjava/java/lang/Long.java (modified) (10 diffs, 1 prop)
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/gcc
- Property svn:ignore
-
old new 26 26 configure.vr 27 27 configure.vrs 28 28 29 Makefile 29 dir.info30 30 lost+found 31 31 update.out
-
- Property svn:ignore
-
branches/GNU/src/gcc/libjava/java/lang/Long.java
-
Property cvs2svn:cvs-rev
changed from
1.1to1.1.1.2
r1390 r1391 1 /* java.lang.Long2 Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.1 /* ong 2 Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc. 3 3 4 4 This file is part of GNU Classpath. … … 8 8 the Free Software Foundation; either version 2, or (at your option) 9 9 any later version. 10 10 11 11 GNU Classpath is distributed in the hope that it will be useful, but 12 12 WITHOUT ANY WARRANTY; without even the implied warranty of … … 40 40 41 41 /** 42 * Instances of class <code> Double</code> represent primitive43 * <code> double</code> values.42 * Instances of class <code></code> represent primitive 43 * <code></code> values. 44 44 * 45 45 * Additionally, this class provides various helper functions and variables … … 49 49 * @author John Keiser 50 50 * @author Warren Levy 51 * @since JDK 1.0 51 * @author Eric Blake <[email protected]> 52 * @since 1.0 53 * @status updated to 1.4 52 54 */ 53 55 public final class Long extends Number implements Comparable 54 56 { 55 // compatible with JDK 1.0.2+ 56 static final long serialVersionUID = 4290774380558885855L; 57 /** 58 * Compatible with JDK 1.0.2+. 59 */ 60 private static final long serialVersionUID = 4290774380558885855L; 57 61 58 62 /** 59 63 * The minimum value a <code>long</code> can represent is 60 * -9223372036854775808 .64 * -9223372036854775808. 61 65 */ 62 66 public static final long MIN_VALUE = 0x8000000000000000L; … … 64 68 /** 65 69 * The maximum value a <code>long</code> can represent is 66 * 9223372036854775807 .70 * 9223372036854775807. 67 71 */ 68 72 public static final long MAX_VALUE = 0x7fffffffffffffffL; 69 73 70 74 /** 71 * The primitive type <code>long</code> is represented by this 75 * The primitive type <code>long</code> is represented by this 72 76 * <code>Class</code> object. 77 73 78 */ 74 79 public static final Class TYPE = VMClassLoader.getPrimitiveClass ('J'); … … 76 81 /** 77 82 * The immutable value of this Long. 83 84 78 85 */ 79 86 private final long value; 80 87 81 88 /** 82 * Create a <code>Long</code> object representing the value of the 89 * Create a <code>Long</code> object representing the value of the 83 90 * <code>long</code> argument. 84 91 * … … 91 98 92 99 /** 93 * Create a <code>Long</code> object representing the value of the 100 * Create a <code>Long</code> object representing the value of the 94 101 * argument after conversion to a <code>long</code>. 95 102 * 96 * @param s the string to convert. 97 */ 98 public Long(String s) throws NumberFormatException 99 { 100 value = parseLong(s, 10); 101 } 102 103 /** 104 * If the <code>Object</code> is not <code>null</code>, is an 105 * <code>instanceof</code> <code>Long</code>, and represents 106 * the same primitive <code>long</code> value return 107 * <code>true</code>. Otherwise <code>false</code> is returned. 108 */ 109 public boolean equals(Object obj) 110 { 111 return obj instanceof Long && ((Long)obj).value == value; 112 } 113 114 /** 115 * Return a hashcode representing this Object. 116 * 117 * <code>Long</code>'s hash code is calculated by simply returning its 118 * value. 119 * 120 * @return this Object's hash code. 121 */ 122 public int hashCode() 123 { 124 return (int)(value^(value>>>32)); 125 } 126 127 /** 128 * Get the specified system property as a <code>Long</code>. 129 * 130 * A method similar to <code>Integer</code>'s <code>decode()</code> will be 131 * used to interpret the value of the property. 132 * 133 * @param nm the name of the system property 134 * @return the system property as an <code>Long</code>, or 135 * <code>null</code> if the property is not found or cannot be 136 * decoded as a <code>Long</code>. 137 * @see java.lang.System#getProperty(java.lang.String) 138 * @see java.lang.Integer#decode(int) 139 */ 140 public static Long getLong(String nm) 141 { 142 return getLong(nm, null); 143 } 144 145 /** 146 * Get the specified system property as an <code>Long</code>, or use a 147 * default <code>long</code> value if the property is not found or is not 148 * decodable. 149 * 150 * A method similar to <code>Integer</code>'s <code>decode()</code> will be 151 * used to interpret the value of the property. 152 * 153 * @param nm the name of the system property 154 * @param val the default value to use if the property is not found or not 155 * a number. 156 * @return the system property as a <code>Long</code>, or the default 157 * value if the property is not found or cannot be decoded as a 158 * <code>Long</code>. 159 * @see java.lang.System#getProperty(java.lang.String) 160 * @see java.lang.Integer#decode(int) 161 * @see #getLong(java.lang.String,java.lang.Long) 162 */ 163 public static Long getLong(String nm, long val) 164 { 165 Long result = getLong(nm, null); 166 return (result == null) ? new Long(val) : result; 167 } 168 169 /** 170 * Get the specified system property as an <code>Long</code>, or use a 171 * default <code>Long</code> value if the property is not found or is 172 * not decodable. 173 * 174 * The <code>decode()</code> method will be used to interpret the value of 175 * the property. 176 * 177 * @param nm the name of the system property 178 * @param val the default value to use if the property is not found or not 179 * a number. 180 * @return the system property as an <code>Long</code>, or the default 181 * value if the property is not found or cannot be decoded as an 182 * <code>Long</code>. 183 * @see java.lang.System#getProperty(java.lang.String) 184 * @see java.lang.Integer#decode(int) 185 * @see #getLong(java.lang.String,long) 186 */ 187 public static Long getLong(String nm, Long def) 188 { 189 nm = System.getProperty(nm); 190 if (nm == null || "".equals(nm)) 191 return def; 192 try 193 { 194 return decode(nm); 195 } 196 catch (NumberFormatException e) 197 { 198 return def; 199 } 200 } 201 202 private static String toUnsignedString(long num, int exp) 203 { 204 // Use an array large enough for a binary number. 205 int radix = 1 << exp; 206 int mask = radix - 1; 207 char[] buffer = new char[64]; 208 int i = 64; 209 do 210 { 211 buffer[--i] = Character.forDigit((int) num & mask, radix); 212 num = num >>> exp; 213 } 214 while (num != 0); 215 216 return String.valueOf(buffer, i, 64-i); 217 } 218 219 /** 220 * Converts the <code>long</code> to a <code>String</code> assuming it is 221 * unsigned in base 16. 222 * @param i the <code>long</code> to convert to <code>String</code> 223 * @return the <code>String</code> representation of the argument. 224 */ 225 public static String toHexString(long i) 226 { 227 return toUnsignedString(i, 4); 228 } 229 230 /** 231 * Converts the <code>long</code> to a <code>String</code> assuming it is 232 * unsigned in base 8. 233 * @param i the <code>long</code> to convert to <code>String</code> 234 * @return the <code>String</code> representation of the argument. 235 */ 236 public static String toOctalString(long i) 237 { 238 return toUnsignedString(i, 3); 239 } 240 241 /** 242 * Converts the <code>long</code> to a <code>String</code> assuming it is 243 * unsigned in base 2. 244 * @param i the <code>long</code> to convert to <code>String</code> 245 * @return the <code>String</code> representation of the argument. 246 */ 247 public static String toBinaryString(long i) { 248 return toUnsignedString(i, 1); 249 } 250 251 /** 252 * Converts the <code>long</code> to a <code>String</code> and assumes 253 * a radix of 10. 103 * @param s the string to convert 104 * @throws NumberFormatException if the String does not contain a long 105 * @see #valueOf(String) 106 */ 107 public Long(String s) 108 { 109 value = parseLong(s, 10, false); 110 } 111 112 /** 113 * Converts the <code>long</code> to a <code>String</code> using 114 * the specified radix (base). If the radix exceeds 115 * <code>Character.MIN_RADIX</code> or <code>Character.MAX_RADIX</code>, 10 116 * is used instead. If the result is negative, the leading character is 117 * '-' ('\\u002D'). The remaining characters come from 118 * <code>Character.forDigit(digit, radix)</code> ('0'-'9','a'-'z'). 119 * 254 120 * @param num the <code>long</code> to convert to <code>String</code> 255 * @return the <code>String</code> representation of the argument. 256 */ 257 public static String toString(long num) 121 * @param radix the radix (base) to use in the conversion 122 * @return the <code>String</code> representation of the argument 123 */ 124 public static String toString(long num, int radix) 258 125 { 259 126 // Use the Integer toString for efficiency if possible. 260 if (num <= Integer.MAX_VALUE && num >= Integer.MIN_VALUE) 261 return Integer.toString((int) num); 262 263 // Use an array large enough for "-9223372036854775808"; i.e. 20 chars. 264 char[] buffer = new char[20]; 265 int i = 20; 266 boolean isNeg; 267 if (num < 0) 268 { 269 isNeg = true; 270 num = -(num); 271 if (num < 0) 272 { 273 // Must be MIN_VALUE, so handle this special case. 274 buffer[--i] = '8'; 275 num = 922337203685477580L; 276 } 277 } 278 else 279 isNeg = false; 280 281 do 282 { 283 buffer[--i] = (char) ((int) '0' + (num % 10)); 284 num /= 10; 285 } 286 while (num > 0); 287 288 if (isNeg) 289 buffer[--i] = '-'; 290 291 return String.valueOf(buffer, i, 20-i); 292 } 293 294 /** 295 * Converts the <code>Long</code> value to a <code>String</code> and 296 * assumes a radix of 10. 297 * @return the <code>String</code> representation of this <code>Long</code>. 298 */ 299 public String toString() 300 { 301 return toString(value); 302 } 303 304 /** 305 * Converts the <code>long</code> to a <code>String</code> using 306 * the specified radix (base). 307 * @param num the <code>long</code> to convert to <code>String</code>. 308 * @param radix the radix (base) to use in the conversion. 309 * @return the <code>String</code> representation of the argument. 310 */ 311 public static String toString(long num, int radix) 312 { 313 // Use optimized method for the typical case. 314 if (radix == 10 || 315 radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 316 return toString(num); 317 318 // Use the Integer toString for efficiency if possible. 319 if (num <= Integer.MAX_VALUE && num >= Integer.MIN_VALUE) 127 if ((int) num == num) 320 128 return Integer.toString((int) num, radix); 129 130 131 321 132 322 133 // For negative numbers, print out the absolute value w/ a leading '-'. … … 324 135 char[] buffer = new char[65]; 325 136 int i = 65; 326 boolean isNeg ;137 boolean isNeg; 327 138 if (num < 0) 328 139 { 329 140 isNeg = true; 330 num = - (num);141 num = -; 331 142 332 143 // When the value is MIN_VALUE, it overflows when made positive 333 144 if (num < 0) 334 { 335 buffer[--i] = Character.forDigit((int) (-(num + radix) % radix), 336 radix); 337 num = -(num / radix); 338 } 339 } 340 else 341 isNeg = false; 145 { 146 buffer[--i] = digits[(int) (-(num + radix) % radix)]; 147 num = -(num / radix); 148 } 149 } 342 150 343 151 do 344 152 { 345 buffer[--i] = Character.forDigit((int) (num % radix), radix);153 buffer[--i] = ; 346 154 num /= radix; 347 155 } … … 351 159 buffer[--i] = '-'; 352 160 353 return String.valueOf(buffer, i, 65-i); 354 } 355 161 // Package constructor avoids an array copy. 162 return new String(buffer, i, 65 - i, true); 163 } 164 165 /** 166 * Converts the <code>long</code> to a <code>String</code> assuming it is 167 * unsigned in base 16. 168 * 169 * @param l the <code>long</code> to convert to <code>String</code> 170 * @return the <code>String</code> representation of the argument 171 */ 172 public static String toHexString(long l) 173 { 174 return toUnsignedString(l, 4); 175 } 176 177 /** 178 * Converts the <code>long</code> to a <code>String</code> assuming it is 179 * unsigned in base 8. 180 * 181 * @param l the <code>long</code> to convert to <code>String</code> 182 * @return the <code>String</code> representation of the argument 183 */ 184 public static String toOctalString(long l) 185 { 186 return toUnsignedString(l, 3); 187 } 188 189 /** 190 * Converts the <code>long</code> to a <code>String</code> assuming it is 191 * unsigned in base 2. 192 * 193 * @param l the <code>long</code> to convert to <code>String</code> 194 * @return the <code>String</code> representation of the argument 195 */ 196 public static String toBinaryString(long l) 197 { 198 return toUnsignedString(l, 1); 199 } 200 201 /** 202 * Converts the <code>long</code> to a <code>String</code> and assumes 203 * a radix of 10. 204 * 205 * @param num the <code>long</code> to convert to <code>String</code> 206 * @return the <code>String</code> representation of the argument 207 * @see #toString(long, int) 208 */ 209 public static String toString(long num) 210 { 211 return toString(num, 10); 212 } 213 214 /** 215 * Converts the specified <code>String</code> into an <code>int</code> 216 * using the specified radix (base). The string must not be <code>null</code> 217 * or empty. It may begin with an optional '-', which will negate the answer, 218 * provided that there are also valid digits. Each digit is parsed as if by 219 * <code>Character.digit(d, radix)</code>, and must be in the range 220 * <code>0</code> to <code>radix - 1</code>. Finally, the result must be 221 * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive. 222 * Unlike Double.parseDouble, you may not have a leading '+'; and 'l' or 223 * 'L' as the last character is only valid in radices 22 or greater, where 224 * it is a digit and not a type indicator. 225 * 226 * @param s the <code>String</code> to convert 227 * @param radix the radix (base) to use in the conversion 228 * @return the <code>String</code> argument converted to </code>long</code> 229 * @throws NumberFormatException if <code>s</code> cannot be parsed as a 230 * <code>long</code> 231 */ 232 public static long parseLong(String str, int radix) 233 { 234 return parseLong(str, radix, false); 235 } 236 237 /** 238 * Converts the specified <code>String</code> into a <code>long</code>. 239 * This function assumes a radix of 10. 240 * 241 * @param s the <code>String</code> to convert 242 * @return the <code>int</code> value of <code>s</code> 243 * @throws NumberFormatException if <code>s</code> cannot be parsed as a 244 * <code>long</code> 245 * @see #parseLong(String, int) 246 */ 247 public static long parseLong(String s) 248 { 249 return parseLong(s, 10, false); 250 } 251 252 /** 253 * Creates a new <code>Long</code> object using the <code>String</code> 254 * and specified radix (base). 255 * 256 * @param s the <code>String</code> to convert 257 * @param radix the radix (base) to convert with 258 * @return the new <code>Long</code> 259 * @throws NumberFormatException if <code>s</code> cannot be parsed as a 260 * <code>long</code> 261 * @see #parseLong(String, int) 262 */ 263 public static Long valueOf(String s, int radix) 264 { 265 return new Long(parseLong(s, radix, false)); 266 } 267 356 268 /** 357 269 * Creates a new <code>Long</code> object using the <code>String</code>, 358 270 * assuming a radix of 10. 359 * @param s the <code>String</code> to convert.360 * @return the new <code>Long</code>.361 * @see #Long(java.lang.String)362 * @see #parseLong(java.lang.String)363 * @exception NumberFormatException thrown if the <code>String</code>364 * cannot be parsed as a <code>long</code>.365 */366 public static Long valueOf(String s) throws NumberFormatException367 {368 return new Long(parseLong(s));369 }370 371 /**372 * Creates a new <code>Long</code> object using the <code>String</code>373 * and specified radix (base).374 * @param s the <code>String</code> to convert.375 * @param radix the radix (base) to convert with.376 * @return the new <code>Long</code>.377 * @see #parseLong(java.lang.String,int)378 * @exception NumberFormatException thrown if the <code>String</code>379 * cannot be parsed as a <code>long</code>.380 */381 public static Long valueOf(String s, int radix) throws NumberFormatException382 {383 return new Long(parseLong(s, radix));384 }385 386 /**387 * Converts the specified <code>String</code> into a <code>long</code>.388 * This function assumes a radix of 10.389 271 * 390 272 * @param s the <code>String</code> to convert 391 * @return the <code>long</code> value of the <code>String</code> 392 * argument. 393 * @exception NumberFormatException thrown if the <code>String</code> 394 * cannot be parsed as a <code>long</code>. 395 */ 396 public static long parseLong(String s) throws NumberFormatException 397 { 398 return parseLong(s, 10); 399 } 400 401 /** 402 * Converts the specified <code>String</code> into a <code>long</code> 403 * using the specified radix (base). 404 * 405 * @param s the <code>String</code> to convert 406 * @param radix the radix (base) to use in the conversion 407 * @return the <code>String</code> argument converted to </code>long</code>. 408 * @exception NumberFormatException thrown if the <code>String</code> 409 * cannot be parsed as a <code>long</code>. 410 */ 411 public static long parseLong(String str, int radix) 412 throws NumberFormatException 413 { 414 final int len; 415 416 if ((len = str.length()) == 0 || radix < Character.MIN_RADIX 417 || radix > Character.MAX_RADIX) 273 * @return the new <code>Long</code> 274 * @throws NumberFormatException if <code>s</code> cannot be parsed as a 275 * <code>long</code> 276 * @see #Long(String) 277 * @see #parseLong(String) 278 */ 279 public static Long valueOf(String s) 280 { 281 return new Long(parseLong(s, 10, false)); 282 } 283 284 /** 285 * Convert the specified <code>String</code> into a <code>Long</code>. 286 * The <code>String</code> may represent decimal, hexadecimal, or 287 * octal numbers. 288 * 289 * <p>The extended BNF grammar is as follows:<br> 290 * <pre> 291 * <em>DecodableString</em>: 292 * ( [ <code>-</code> ] <em>DecimalNumber</em> ) 293 * | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code> 294 * | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } ) 295 * | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } ) 296 * <em>DecimalNumber</em>: 297 * <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> } 298 * <em>DecimalDigit</em>: 299 * <em>Character.digit(d, 10) has value 0 to 9</em> 300 * <em>OctalDigit</em>: 301 * <em>Character.digit(d, 8) has value 0 to 7</em> 302 * <em>DecimalDigit</em>: 303 * <em>Character.digit(d, 16) has value 0 to 15</em> 304 * </pre> 305 * Finally, the value must be in the range <code>MIN_VALUE</code> to 306 * <code>MAX_VALUE</code>, or an exception is thrown. Note that you cannot 307 * use a trailing 'l' or 'L', unlike in Java source code. 308 * 309 * @param s the <code>String</code> to interpret 310 * @return the value of the String as a <code>Long</code> 311 * @throws NumberFormatException if <code>s</code> cannot be parsed as a 312 * <code>long</code> 313 * @throws NullPointerException if <code>s</code> is null 314 * @since 1.2 315 */ 316 public static Long decode(String str) 317 { 318 return new Long(parseLong(str, 10, true)); 319 } 320 321 /** 322 * Return the value of this <code>Long</code> as a <code>byte</code>. 323 * 324 * @return the byte value 325 */ 326 public byte byteValue() 327 { 328 return (byte) value; 329 } 330 331 /** 332 * Return the value of this <code>Long</code> as a <code>short</code>. 333 * 334 * @return the short value 335 */ 336 public short shortValue() 337 { 338 return (short) value; 339 } 340 341 /** 342 * Return the value of this <code>Long</code> as an <code>int</code>. 343 * 344 * @return the int value 345 */ 346 public int intValue() 347 { 348 return (int) value; 349 } 350 351 /** 352 * Return the value of this <code>Long</code>. 353 * 354 * @return the long value 355 */ 356 public long longValue() 357 { 358 return value; 359 } 360 361 /** 362 * Return the value of this <code>Long</code> as a <code>float</code>. 363 * 364 * @return the float value 365 */ 366 public float floatValue() 367 { 368 return value; 369 } 370 371 /** 372 * Return the value of this <code>Long</code> as a <code>double</code>. 373 * 374 * @return the double value 375 */ 376 public double doubleValue() 377 { 378 return value; 379 } 380 381 /** 382 * Converts the <code>Long</code> value to a <code>String</code> and 383 * assumes a radix of 10. 384 * 385 * @return the <code>String</code> representation 386 */ 387 public String toString() 388 { 389 return toString(value, 10); 390 } 391 392 /** 393 * Return a hashcode representing this Object. <code>Long</code>'s hash 394 * code is calculated by <code>(int) (value ^ (value >> 32))</code>. 395 * 396 * @return this Object's hash code 397 */ 398 public int hashCode() 399 { 400 return (int) (value ^ (value >>> 32)); 401 } 402 403 /** 404 * Returns <code>true</code> if <code>obj</code> is an instance of 405 * <code>Long</code> and represents the same long value. 406 * 407 * @param obj the object to compare 408 * @return whether these Objects are semantically equal 409 */ 410 public boolean equals(Object obj) 411 { 412 return obj instanceof Long && value == ((Long) obj).value; 413 } 414 415 /** 416 * Get the specified system property as a <code>Long</code>. The 417 * <code>decode()</code> method will be used to interpret the value of 418 * the property. 419 * 420 * @param nm the name of the system property 421 * @return the system property as a <code>Long</code>, or null if the 422 * property is not found or cannot be decoded 423 * @throws SecurityException if accessing the system property is forbidden 424 * @see System#getProperty(String) 425 * @see #decode(String) 426 */ 427 public static Long getLong(String nm) 428 { 429 return getLong(nm, null); 430 } 431 432 /** 433 * Get the specified system property as a <code>Long</code>, or use a 434 * default <code>long</code> value if the property is not found or is not 435 * decodable. The <code>decode()</code> method will be used to interpret 436 * the value of the property. 437 * 438 * @param nm the name of the system property 439 * @param val the default value 440 * @return the value of the system property, or the default 441 * @throws SecurityException if accessing the system property is forbidden 442 * @see System#getProperty(String) 443 * @see #decode(String) 444 */ 445 public static Long getLong(String nm, long val) 446 { 447 Long result = getLong(nm, null); 448 return result == null ? new Long(val) : result; 449 } 450 451 /** 452 * Get the specified system property as a <code>Long</code>, or use a 453 * default <code>Long</code> value if the property is not found or is 454 * not decodable. The <code>decode()</code> method will be used to 455 * interpret the value of the property. 456 * 457 * @param nm the name of the system property 458 * @param val the default value 459 * @return the value of the system property, or the default 460 * @throws SecurityException if accessing the system property is forbidden 461 * @see System#getProperty(String) 462 * @see #decode(String) 463 */ 464 public static Long getLong(String nm, Long def) 465 { 466 if (nm == null || "".equals(nm)) 467 return def; 468 nm = System.getProperty(nm); 469 if (nm == null) 470 return def; 471 try 472 { 473 return decode(nm); 474 } 475 catch (NumberFormatException e) 476 { 477 return def; 478 } 479 } 480 481 /** 482 * Compare two Longs numerically by comparing their <code>long</code> 483 * values. The result is positive if the first is greater, negative if the 484 * second is greater, and 0 if the two are equal. 485 * 486 * @param l the Long to compare 487 * @return the comparison 488 * @since 1.2 489 */ 490 public int compareTo(Long l) 491 { 492 if (value == l.value) 493 return 0; 494 // Returns just -1 or 1 on inequality; doing math might overflow the long. 495 return value > l.value ? 1 : -1; 496 } 497 498 /** 499 * Behaves like <code>compareTo(Long)</code> unless the Object 500 * is not a <code>Long</code>. 501 * 502 * @param o the object to compare 503 * @return the comparison 504 * @throws ClassCastException if the argument is not a <code>Long</code> 505 * @see #compareTo(Long) 506 * @see Comparable 507 * @since 1.2 508 */ 509 public int compareTo(Object o) 510 { 511 return compareTo((Long) o); 512 } 513 514 /** 515 * Helper for converting unsigned numbers to String. 516 * 517 * @param num the number 518 * @param exp log2(digit) (ie. 1, 3, or 4 for binary, oct, hex) 519 */ 520 private static String toUnsignedString(long num, int exp) 521 { 522 // Use the Integer toUnsignedString for efficiency if possible. 523 // If NUM<0 then this particular optimization doesn't work 524 // properly. 525 if (num >= 0 && (int) num == num) 526 return Integer.toUnsignedString((int) num, exp); 527 528 // Use an array large enough for a binary number. 529 int mask = (1 << exp) - 1; 530 char[] buffer = new char[64]; 531 int i = 64; 532 do 533 { 534 buffer[--i] = digits[(int) num & mask]; 535 num >>>= exp; 536 } 537 while (num != 0); 538 539 // Package constructor avoids an array copy. 540 return new String(buffer, i, 64 - i, true); 541 } 542 543 /** 544 * Helper for parsing longs. 545 * 546 * @param str the string to parse 547 * @param radix the radix to use, must be 10 if decode is true 548 * @param decode if called from decode 549 * @return the parsed long value 550 * @throws NumberFormatException if there is an error 551 * @throws NullPointerException if decode is true and str is null 552 * @see #parseLong(String, int) 553 * @see #decode(String) 554 */ 555 private static long parseLong(String str, int radix, boolean decode) 556 { 557 if (! decode && str == null) 418 558 throw new NumberFormatException(); 419 559 int index = 0; 560 int len = str.length(); 420 561 boolean isNeg = false; 421 int index = 0; 422 if (str.charAt(index) == '-') 423 if (len > 1) 424 { 425 isNeg = true; 426 index++; 427 } 428 else 429 throw new NumberFormatException(); 430 431 return parseLong(str, index, len, isNeg, radix); 432 } 433 434 public static Long decode(String str) throws NumberFormatException 435 { 436 boolean isNeg = false; 437 int index = 0; 438 int radix = 10; 439 final int len; 440 441 if ((len = str.length()) == 0) 562 if (len == 0) 442 563 throw new NumberFormatException(); 443 444 // Negative numbers are always radix 10. 445 if (str.charAt(0) == '-') 446 { 447 radix = 10; 448 index++; 564 int ch = str.charAt(index); 565 if (ch == '-') 566 { 567 if (len == 1) 568 throw new NumberFormatException(); 449 569 isNeg = true; 450 } 451 else if (str.charAt(index) == '#') 452 { 453 radix = 16; 454 index++; 455 } 456 else if (str.charAt(index) == '0') 457 { 458 // Check if str is just "0" 459 if (len == 1) 460 return new Long(0L); 461 462 index++; 463 if (str.charAt(index) == 'x') 570 ch = str.charAt(++index); 571 } 572 if (decode) 573 { 574 if (ch == '0') 575 { 576 if (++index == len) 577 return 0; 578 if ((str.charAt(index) & ~('x' ^ 'X')) == 'X') 579 { 580 radix = 16; 581 index++; 582 } 583 else 584 radix = 8; 585 } 586 else if (ch == '#') 464 587 { 465 588 radix = 16; 466 589 index++; 467 590 } 468 else 469 radix = 8; 470 } 471 472 if (index >= len) 591 } 592 if (index == len) 473 593 throw new NumberFormatException(); 474 475 return new Long(parseLong(str, index, len, isNeg, radix));476 }477 478 private static long parseLong(String str, int index, int len, boolean isNeg,479 int radix) throws NumberFormatException480 {481 long val = 0;482 int digval;483 594 484 595 long max = MAX_VALUE / radix; … … 488 599 ++max; 489 600 490 for ( ; index < len; index++) 601 long val = 0; 602 while (index < len) 491 603 { 492 604 if (val < 0 || val > max) 493 605 throw new NumberFormatException(); 494 606 495 if ((digval = Character.digit(str.charAt(index), radix)) < 0) 607 ch = Character.digit(str.charAt(index++), radix); 608 val = val * radix + ch; 609 if (ch < 0 || (val < 0 && (! isNeg || val != MIN_VALUE))) 496 610 throw new NumberFormatException(); 497 498 // Throw an exception for overflow if result is negative. 499 // However, we special-case the most negative value. 500 val = val * radix + digval; 501 if (val < 0 && (! isNeg || val != MIN_VALUE)) 502 throw new NumberFormatException(); 503 } 504 505 return isNeg ? -(val) : val; 506 } 507 508 /** Return the value of this <code>Long</code> as an <code>short</code>. 509 ** @return the value of this <code>Long</code> as an <code>short</code>. 510 **/ 511 public byte byteValue() 512 { 513 return (byte) value; 514 } 515 516 /** Return the value of this <code>Long</code> as an <code>short</code>. 517 ** @return the value of this <code>Long</code> as an <code>short</code>. 518 **/ 519 public short shortValue() 520 { 521 return (short) value; 522 } 523 524 /** Return the value of this <code>Long</code> as an <code>int</code>. 525 ** @return the value of this <code>Long</code> as an <code>int</code>. 526 **/ 527 public int intValue() 528 { 529 return (int) value; 530 } 531 532 /** Return the value of this <code>Long</code> as a <code>long</code>. 533 ** @return the value of this <code>Long</code> as a <code>long</code>. 534 **/ 535 public long longValue() 536 { 537 return value; 538 } 539 540 /** Return the value of this <code>Long</code> as a <code>float</code>. 541 ** @return the value of this <code>Long</code> as a <code>float</code>. 542 **/ 543 public float floatValue() 544 { 545 return value; 546 } 547 548 /** Return the value of this <code>Long</code> as a <code>double</code>. 549 ** @return the value of this <code>Long</code> as a <code>double</code>. 550 **/ 551 public double doubleValue() 552 { 553 return value; 554 } 555 556 /** 557 * Compare two Longs numerically by comparing their 558 * <code>long</code> values. 559 * @return a positive value if this <code>Long</code> is greater 560 * in value than the argument <code>Long</code>; a negative value 561 * if this <code>Long</code> is smaller in value than the argument 562 * <code>Long</code>; and <code>0</code>, zero, if this 563 * <code>Long</code> is equal in value to the argument 564 * <code>Long</code>. 565 * 566 * @since 1.2 567 */ 568 public int compareTo(Long l) 569 { 570 if (this.value == l.value) 571 return 0; 572 573 // Returns just -1 or 1 on inequality; doing math might overflow the long. 574 if (this.value > l.value) 575 return 1; 576 577 return -1; 578 } 579 580 /** 581 * Behaves like <code>compareTo(java.lang.Long)</code> unless the Object 582 * is not a <code>Long</code>. Then it throws a 583 * <code>ClassCastException</code>. 584 * @exception ClassCastException if the argument is not a 585 * <code>Long</code>. 586 * 587 * @since 1.2 588 */ 589 public int compareTo(Object o) 590 { 591 return compareTo((Long)o); 611 } 612 return isNeg ? -val : val; 592 613 } 593 614 } -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.
