| 1 | /* java.lang.Long
|
|---|
| 2 | Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.lang;
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Instances of class <code>Double</code> represent primitive
|
|---|
| 43 | * <code>double</code> values.
|
|---|
| 44 | *
|
|---|
| 45 | * Additionally, this class provides various helper functions and variables
|
|---|
| 46 | * related to longs.
|
|---|
| 47 | *
|
|---|
| 48 | * @author Paul Fisher
|
|---|
| 49 | * @author John Keiser
|
|---|
| 50 | * @author Warren Levy
|
|---|
| 51 | * @since JDK 1.0
|
|---|
| 52 | */
|
|---|
| 53 | public final class Long extends Number implements Comparable
|
|---|
| 54 | {
|
|---|
| 55 | // compatible with JDK 1.0.2+
|
|---|
| 56 | static final long serialVersionUID = 4290774380558885855L;
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * The minimum value a <code>long</code> can represent is
|
|---|
| 60 | * -9223372036854775808.
|
|---|
| 61 | */
|
|---|
| 62 | public static final long MIN_VALUE = 0x8000000000000000L;
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * The maximum value a <code>long</code> can represent is
|
|---|
| 66 | * 9223372036854775807.
|
|---|
| 67 | */
|
|---|
| 68 | public static final long MAX_VALUE = 0x7fffffffffffffffL;
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * The primitive type <code>long</code> is represented by this
|
|---|
| 72 | * <code>Class</code> object.
|
|---|
| 73 | */
|
|---|
| 74 | public static final Class TYPE = VMClassLoader.getPrimitiveClass ('J');
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * The immutable value of this Long.
|
|---|
| 78 | */
|
|---|
| 79 | private final long value;
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Create a <code>Long</code> object representing the value of the
|
|---|
| 83 | * <code>long</code> argument.
|
|---|
| 84 | *
|
|---|
| 85 | * @param value the value to use
|
|---|
| 86 | */
|
|---|
| 87 | public Long(long value)
|
|---|
| 88 | {
|
|---|
| 89 | this.value = value;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /**
|
|---|
| 93 | * Create a <code>Long</code> object representing the value of the
|
|---|
| 94 | * argument after conversion to a <code>long</code>.
|
|---|
| 95 | *
|
|---|
| 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.
|
|---|
| 254 | * @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)
|
|---|
| 258 | {
|
|---|
| 259 | // 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)
|
|---|
| 320 | return Integer.toString((int) num, radix);
|
|---|
| 321 |
|
|---|
| 322 | // For negative numbers, print out the absolute value w/ a leading '-'.
|
|---|
| 323 | // Use an array large enough for a binary number.
|
|---|
| 324 | char[] buffer = new char[65];
|
|---|
| 325 | int i = 65;
|
|---|
| 326 | boolean isNeg;
|
|---|
| 327 | if (num < 0)
|
|---|
| 328 | {
|
|---|
| 329 | isNeg = true;
|
|---|
| 330 | num = -(num);
|
|---|
| 331 |
|
|---|
| 332 | // When the value is MIN_VALUE, it overflows when made positive
|
|---|
| 333 | 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;
|
|---|
| 342 |
|
|---|
| 343 | do
|
|---|
| 344 | {
|
|---|
| 345 | buffer[--i] = Character.forDigit((int) (num % radix), radix);
|
|---|
| 346 | num /= radix;
|
|---|
| 347 | }
|
|---|
| 348 | while (num > 0);
|
|---|
| 349 |
|
|---|
| 350 | if (isNeg)
|
|---|
| 351 | buffer[--i] = '-';
|
|---|
| 352 |
|
|---|
| 353 | return String.valueOf(buffer, i, 65-i);
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | /**
|
|---|
| 357 | * Creates a new <code>Long</code> object using the <code>String</code>,
|
|---|
| 358 | * 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 NumberFormatException
|
|---|
| 367 | {
|
|---|
| 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 NumberFormatException
|
|---|
| 382 | {
|
|---|
| 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 | *
|
|---|
| 390 | * @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)
|
|---|
| 418 | throw new NumberFormatException();
|
|---|
| 419 |
|
|---|
| 420 | 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)
|
|---|
| 442 | throw new NumberFormatException();
|
|---|
| 443 |
|
|---|
| 444 | // Negative numbers are always radix 10.
|
|---|
| 445 | if (str.charAt(0) == '-')
|
|---|
| 446 | {
|
|---|
| 447 | radix = 10;
|
|---|
| 448 | index++;
|
|---|
| 449 | 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')
|
|---|
| 464 | {
|
|---|
| 465 | radix = 16;
|
|---|
| 466 | index++;
|
|---|
| 467 | }
|
|---|
| 468 | else
|
|---|
| 469 | radix = 8;
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | if (index >= len)
|
|---|
| 473 | 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 NumberFormatException
|
|---|
| 480 | {
|
|---|
| 481 | long val = 0;
|
|---|
| 482 | int digval;
|
|---|
| 483 |
|
|---|
| 484 | long max = MAX_VALUE / radix;
|
|---|
| 485 | // We can't directly write `max = (MAX_VALUE + 1) / radix'.
|
|---|
| 486 | // So instead we fake it.
|
|---|
| 487 | if (isNeg && MAX_VALUE % radix == radix - 1)
|
|---|
| 488 | ++max;
|
|---|
| 489 |
|
|---|
| 490 | for ( ; index < len; index++)
|
|---|
| 491 | {
|
|---|
| 492 | if (val < 0 || val > max)
|
|---|
| 493 | throw new NumberFormatException();
|
|---|
| 494 |
|
|---|
| 495 | if ((digval = Character.digit(str.charAt(index), radix)) < 0)
|
|---|
| 496 | 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);
|
|---|
| 592 | }
|
|---|
| 593 | }
|
|---|