1. /*
  2. * @(#)Long.java 1.66 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. /**
  9. * The <code>Long</code> class wraps a value of the primitive type
  10. * <code>long</code> in an object. An object of type <code>Long</code>
  11. * contains a single field whose type is <code>long</code>.
  12. *
  13. * <p>
  14. *
  15. * In addition, this class provides several methods for converting a
  16. * <code>long</code> to a <code>String</code> and a
  17. * <code>String</code> to a <code>long</code>, as well as other
  18. * constants and methods useful when dealing with a <code>long</code>.
  19. *
  20. * @author Lee Boynton
  21. * @author Arthur van Hoff
  22. * @version 1.66, 01/23/03
  23. * @since JDK1.0
  24. */
  25. public final class Long extends Number implements Comparable {
  26. /**
  27. * A constant holding the minimum value a <code>long</code> can
  28. * have, -2<sup>63</sup>.
  29. */
  30. public static final long MIN_VALUE = 0x8000000000000000L;
  31. /**
  32. * A constant holding the maximum value a <code>long</code> can
  33. * have, 2<sup>63</sup>-1.
  34. */
  35. public static final long MAX_VALUE = 0x7fffffffffffffffL;
  36. /**
  37. * The <code>Class</code> instance representing the primitive type
  38. * <code>long</code>.
  39. *
  40. * @since JDK1.1
  41. */
  42. public static final Class TYPE = Class.getPrimitiveClass("long");
  43. /**
  44. * Returns a string representation of the first argument in the
  45. * radix specified by the second argument.
  46. * <p>
  47. * If the radix is smaller than <code>Character.MIN_RADIX</code>
  48. * or larger than <code>Character.MAX_RADIX</code>, then the radix
  49. * <code>10</code> is used instead.
  50. * <p>
  51. * If the first argument is negative, the first element of the
  52. * result is the ASCII minus sign <code>'-'</code>
  53. * (<code>'\u002d'</code>). If the first argument is not
  54. * negative, no sign character appears in the result.
  55. * <p>
  56. * The remaining characters of the result represent the magnitude
  57. * of the first argument. If the magnitude is zero, it is
  58. * represented by a single zero character <code>'0'</code>
  59. * (<code>'\u0030'</code>); otherwise, the first character of
  60. * the representation of the magnitude will not be the zero
  61. * character. The following ASCII characters are used as digits:
  62. * <blockquote><pre>
  63. * 0123456789abcdefghijklmnopqrstuvwxyz
  64. * </pre></blockquote>
  65. * These are <code>'\u0030'</code> through
  66. * <code>'\u0039'</code> and <code>'\u0061'</code> through
  67. * <code>'\u007a'</code>. If <code>radix</code> is
  68. * <var>N</var>, then the first <var>N</var> of these characters
  69. * are used as radix-<var>N</var> digits in the order shown. Thus,
  70. * the digits for hexadecimal (radix 16) are
  71. * <code>0123456789abcdef</code>. If uppercase letters are
  72. * desired, the {@link java.lang.String#toUpperCase()} method may
  73. * be called on the result:
  74. * <blockquote><pre>
  75. * Long.toString(n, 16).toUpperCase()
  76. * </pre></blockquote>
  77. *
  78. * @param i a <code>long</code>to be converted to a string.
  79. * @param radix the radix to use in the string representation.
  80. * @return a string representation of the argument in the specified radix.
  81. * @see java.lang.Character#MAX_RADIX
  82. * @see java.lang.Character#MIN_RADIX
  83. */
  84. public static String toString(long i, int radix) {
  85. if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  86. radix = 10;
  87. if (radix == 10)
  88. return toString(i);
  89. char[] buf = new char[65];
  90. int charPos = 64;
  91. boolean negative = (i < 0);
  92. if (!negative) {
  93. i = -i;
  94. }
  95. while (i <= -radix) {
  96. buf[charPos--] = Integer.digits[(int)(-(i % radix))];
  97. i = i / radix;
  98. }
  99. buf[charPos] = Integer.digits[(int)(-i)];
  100. if (negative) {
  101. buf[--charPos] = '-';
  102. }
  103. return new String(buf, charPos, (65 - charPos));
  104. }
  105. /**
  106. * Returns a string representation of the <code>long</code>
  107. * argument as an unsigned integer in base 16.
  108. * <p>
  109. * The unsigned <code>long</code> value is the argument plus
  110. * 2<sup>64</sup> if the argument is negative; otherwise, it is
  111. * equal to the argument. This value is converted to a string of
  112. * ASCII digits in hexadecimal (base 16) with no extra
  113. * leading <code>0</code>s. If the unsigned magnitude is zero, it
  114. * is represented by a single zero character <code>'0'</code>
  115. * (<code>'\u0030'</code>); otherwise, the first character of
  116. * the representation of the unsigned magnitude will not be the
  117. * zero character. The following characters are used as
  118. * hexadecimal digits:
  119. * <blockquote><pre>
  120. * 0123456789abcdef
  121. * </pre></blockquote>
  122. * These are the characters <code>'\u0030'</code> through
  123. * <code>'\u0039'</code> and <code>'\u0061'</code> through
  124. * <code>'\u0066'</code>. If uppercase letters are desired,
  125. * the {@link java.lang.String#toUpperCase()} method may be called
  126. * on the result:
  127. * <blockquote><pre>
  128. * Long.toHexString(n).toUpperCase()
  129. * </pre></blockquote>
  130. *
  131. * @param i a <code>long</code> to be converted to a string.
  132. * @return the string representation of the unsigned <code>long</code>
  133. * value represented by the argument in hexadecimal
  134. * (base 16).
  135. * @since JDK 1.0.2
  136. */
  137. public static String toHexString(long i) {
  138. return toUnsignedString(i, 4);
  139. }
  140. /**
  141. * Returns a string representation of the <code>long</code>
  142. * argument as an unsigned integer in base 8.
  143. * <p>
  144. * The unsigned <code>long</code> value is the argument plus
  145. * 2<sup>64</sup> if the argument is negative; otherwise, it is
  146. * equal to the argument. This value is converted to a string of
  147. * ASCII digits in octal (base 8) with no extra leading
  148. * <code>0</code>s.
  149. * <p>
  150. * If the unsigned magnitude is zero, it is represented by a
  151. * single zero character <code>'0'</code>
  152. * (<code>'\u0030'</code>); otherwise, the first character of
  153. * the representation of the unsigned magnitude will not be the
  154. * zero character. The following characters are used as octal
  155. * digits:
  156. * <blockquote><pre>
  157. * 01234567
  158. * </pre></blockquote>
  159. * These are the characters <code>'\u0030'</code> through
  160. * <code>'\u0037'</code>.
  161. *
  162. * @param i a <code>long</code> to be converted to a string.
  163. * @return the string representation of the unsigned <code>long</code>
  164. * value represented by the argument in octal (base 8).
  165. * @since JDK 1.0.2
  166. */
  167. public static String toOctalString(long i) {
  168. return toUnsignedString(i, 3);
  169. }
  170. /**
  171. * Returns a string representation of the <code>long</code>
  172. * argument as an unsigned integer in base 2.
  173. * <p>
  174. * The unsigned <code>long</code> value is the argument plus
  175. * 2<sup>64</sup> if the argument is negative; otherwise, it is
  176. * equal to the argument. This value is converted to a string of
  177. * ASCII digits in binary (base 2) with no extra leading
  178. * <code>0</code>s. If the unsigned magnitude is zero, it is
  179. * represented by a single zero character <code>'0'</code>
  180. * (<code>'\u0030'</code>); otherwise, the first character of
  181. * the representation of the unsigned magnitude will not be the
  182. * zero character. The characters <code>'0'</code>
  183. * (<code>'\u0030'</code>) and <code>'1'</code>
  184. * (<code>'\u0031'</code>) are used as binary digits.
  185. *
  186. * @param i a <code>long</code> to be converted to a string.
  187. * @return the string representation of the unsigned <code>long</code>
  188. * value represented by the argument in binary (base 2).
  189. * @since JDK 1.0.2
  190. */
  191. public static String toBinaryString(long i) {
  192. return toUnsignedString(i, 1);
  193. }
  194. /**
  195. * Convert the integer to an unsigned number.
  196. */
  197. private static String toUnsignedString(long i, int shift) {
  198. char[] buf = new char[64];
  199. int charPos = 64;
  200. int radix = 1 << shift;
  201. long mask = radix - 1;
  202. do {
  203. buf[--charPos] = Integer.digits[(int)(i & mask)];
  204. i >>>= shift;
  205. } while (i != 0);
  206. return new String(buf, charPos, (64 - charPos));
  207. }
  208. /**
  209. * Returns a <code>String</code> object representing the specified
  210. * <code>long</code>. The argument is converted to signed decimal
  211. * representation and returned as a string, exactly as if the
  212. * argument and the radix 10 were given as arguments to the {@link
  213. * #toString(long, int)} method.
  214. *
  215. * @param i a <code>long</code> to be converted.
  216. * @return a string representation of the argument in base 10.
  217. */
  218. public static String toString(long i) {
  219. if (i == Long.MIN_VALUE)
  220. return "-9223372036854775808";
  221. char[] buf = (char[])(perThreadBuffer.get());
  222. int charPos = getChars(i, buf);
  223. return new String(buf, charPos, (20 - charPos));
  224. }
  225. // Per-thread buffer for string/stringbuffer conversion
  226. private static ThreadLocal perThreadBuffer = new ThreadLocal() {
  227. protected synchronized Object initialValue() {
  228. return new char[20];
  229. }
  230. };
  231. private static int getChars(long i, char[] buf) {
  232. long q;
  233. int r;
  234. int charPos = 20;
  235. char sign = 0;
  236. if (i < 0) {
  237. sign = '-';
  238. i = -i;
  239. }
  240. // Get 2 digits/iteration using longs until quotient fits into an int
  241. while (i > Integer.MAX_VALUE) {
  242. q = i / 100;
  243. // really: r = i - (q * 100);
  244. r = (int)(i - ((q << 6) + (q << 5) + (q << 2)));
  245. i = q;
  246. buf[--charPos] = Integer.DigitOnes[r];
  247. buf[--charPos] = Integer.DigitTens[r];
  248. }
  249. // Get 2 digits/iteration using ints
  250. int q2;
  251. int i2 = (int)i;
  252. while (i2 >= 65536) {
  253. q2 = i2 / 100;
  254. // really: r = i2 - (q * 100);
  255. r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
  256. i2 = q2;
  257. buf[--charPos] = Integer.DigitOnes[r];
  258. buf[--charPos] = Integer.DigitTens[r];
  259. }
  260. // Fall thru to fast mode for smaller numbers
  261. // assert(i2 <= 65536, i2);
  262. for (;;) {
  263. q2 = (i2 * 52429) >>> (16+3);
  264. r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
  265. buf[--charPos] = Integer.digits[r];
  266. i2 = q2;
  267. if (i2 == 0) break;
  268. }
  269. if (sign != 0) {
  270. buf[--charPos] = sign;
  271. }
  272. return charPos;
  273. }
  274. static void appendTo(long i, StringBuffer sb) {
  275. if (i == Long.MIN_VALUE) {
  276. sb.append("-9223372036854775808");
  277. return;
  278. }
  279. char[] buf = (char[])(perThreadBuffer.get());
  280. int charPos = getChars(i, buf);
  281. sb.append(buf, charPos, (20 - charPos));
  282. return;
  283. }
  284. /**
  285. * Parses the string argument as a signed <code>long</code> in the
  286. * radix specified by the second argument. The characters in the
  287. * string must all be digits of the specified radix (as determined
  288. * by whether {@link java.lang.Character#digit(char, int)} returns
  289. * a nonnegative value), except that the first character may be an
  290. * ASCII minus sign <code>'-'</code> (<code>'\u002D'</code>) to
  291. * indicate a negative value. The resulting <code>long</code>
  292. * value is returned.
  293. * <p>
  294. * Note that neither the character <code>L</code>
  295. * (<code>'\u004C'</code>) nor <code>l</code>
  296. * (<code>'\u006C'</code>) is permitted to appear at the end
  297. * of the string as a type indicator, as would be permitted in
  298. * Java programming language source code - except that either
  299. * <code>L</code> or <code>l</code> may appear as a digit for a
  300. * radix greater than 22.
  301. * <p>
  302. * An exception of type <code>NumberFormatException</code> is
  303. * thrown if any of the following situations occurs:
  304. * <ul>
  305. * <li>The first argument is <code>null</code> or is a string of
  306. * length zero.
  307. * <li>The <code>radix</code> is either smaller than {@link
  308. * java.lang.Character#MIN_RADIX} or larger than {@link
  309. * java.lang.Character#MAX_RADIX}.
  310. * <li>Any character of the string is not a digit of the specified
  311. * radix, except that the first character may be a minus sign
  312. * <code>'-'</code> (<code>'\u002d'</code>) provided that the
  313. * string is longer than length 1.
  314. * <li>The value represented by the string is not a value of type
  315. * <code>long</code>.
  316. * </ul><p>
  317. * Examples:
  318. * <blockquote><pre>
  319. * parseLong("0", 10) returns 0L
  320. * parseLong("473", 10) returns 473L
  321. * parseLong("-0", 10) returns 0L
  322. * parseLong("-FF", 16) returns -255L
  323. * parseLong("1100110", 2) returns 102L
  324. * parseLong("99", 8) throws a NumberFormatException
  325. * parseLong("Hazelnut", 10) throws a NumberFormatException
  326. * parseLong("Hazelnut", 36) returns 1356099454469L
  327. * </pre></blockquote>
  328. *
  329. * @param s the <code>String</code> containing the
  330. * <code>long</code> representation to be parsed.
  331. * @param radix the radix to be used while parsing <code>s</code>.
  332. * @return the <code>long</code> represented by the string argument in
  333. * the specified radix.
  334. * @exception NumberFormatException if the string does not contain a
  335. * parsable <code>long</code>.
  336. */
  337. public static long parseLong(String s, int radix)
  338. throws NumberFormatException
  339. {
  340. if (s == null) {
  341. throw new NumberFormatException("null");
  342. }
  343. if (radix < Character.MIN_RADIX) {
  344. throw new NumberFormatException("radix " + radix +
  345. " less than Character.MIN_RADIX");
  346. }
  347. if (radix > Character.MAX_RADIX) {
  348. throw new NumberFormatException("radix " + radix +
  349. " greater than Character.MAX_RADIX");
  350. }
  351. long result = 0;
  352. boolean negative = false;
  353. int i = 0, max = s.length();
  354. long limit;
  355. long multmin;
  356. int digit;
  357. if (max > 0) {
  358. if (s.charAt(0) == '-') {
  359. negative = true;
  360. limit = Long.MIN_VALUE;
  361. i++;
  362. } else {
  363. limit = -Long.MAX_VALUE;
  364. }
  365. multmin = limit / radix;
  366. if (i < max) {
  367. digit = Character.digit(s.charAt(i++),radix);
  368. if (digit < 0) {
  369. throw NumberFormatException.forInputString(s);
  370. } else {
  371. result = -digit;
  372. }
  373. }
  374. while (i < max) {
  375. // Accumulating negatively avoids surprises near MAX_VALUE
  376. digit = Character.digit(s.charAt(i++),radix);
  377. if (digit < 0) {
  378. throw NumberFormatException.forInputString(s);
  379. }
  380. if (result < multmin) {
  381. throw NumberFormatException.forInputString(s);
  382. }
  383. result *= radix;
  384. if (result < limit + digit) {
  385. throw NumberFormatException.forInputString(s);
  386. }
  387. result -= digit;
  388. }
  389. } else {
  390. throw NumberFormatException.forInputString(s);
  391. }
  392. if (negative) {
  393. if (i > 1) {
  394. return result;
  395. } else { /* Only got "-" */
  396. throw NumberFormatException.forInputString(s);
  397. }
  398. } else {
  399. return -result;
  400. }
  401. }
  402. /**
  403. * Parses the string argument as a signed decimal
  404. * <code>long</code>. The characters in the string must all be
  405. * decimal digits, except that the first character may be an ASCII
  406. * minus sign <code>'-'</code> (<code>\u002D'</code>) to
  407. * indicate a negative value. The resulting <code>long</code>
  408. * value is returned, exactly as if the argument and the radix
  409. * <code>10</code> were given as arguments to the {@link
  410. * #parseLong(java.lang.String, int)} method.
  411. * <p>
  412. * Note that neither the character <code>L</code>
  413. * (<code>'\u004C'</code>) nor <code>l</code>
  414. * (<code>'\u006C'</code>) is permitted to appear at the end
  415. * of the string as a type indicator, as would be permitted in
  416. * Java programming language source code.
  417. *
  418. * @param s a <code>String</code> containing the <code>long</code>
  419. * representation to be parsed
  420. * @return the <code>long</code> represented by the argument in
  421. * decimal.
  422. * @exception NumberFormatException if the string does not contain a
  423. * parsable <code>long</code>.
  424. */
  425. public static long parseLong(String s) throws NumberFormatException {
  426. return parseLong(s, 10);
  427. }
  428. /**
  429. * Returns a <code>Long</code> object holding the value
  430. * extracted from the specified <code>String</code> when parsed
  431. * with the radix given by the second argument. The first
  432. * argument is interpreted as representing a signed
  433. * <code>long</code> in the radix specified by the second
  434. * argument, exactly as if the arguments were given to the {@link
  435. * #parseLong(java.lang.String, int)} method. The result is a
  436. * <code>Long</code> object that represents the <code>long</code>
  437. * value specified by the string.
  438. * <p>
  439. * In other words, this method returns a <code>Long</code> object equal
  440. * to the value of:
  441. *
  442. * <blockquote><code>
  443. * new Long(Long.parseLong(s, radix))
  444. * </code></blockquote>
  445. *
  446. * @param s the string to be parsed
  447. * @param radix the radix to be used in interpreting <code>s</code>
  448. * @return a <code>Long</code> object holding the value
  449. * represented by the string argument in the specified
  450. * radix.
  451. * @exception NumberFormatException If the <code>String</code> does not
  452. * contain a parsable <code>long</code>.
  453. */
  454. public static Long valueOf(String s, int radix) throws NumberFormatException {
  455. return new Long(parseLong(s, radix));
  456. }
  457. /**
  458. * Returns a <code>Long</code> object holding the value
  459. * of the specified <code>String</code>. The argument is
  460. * interpreted as representing a signed decimal <code>long</code>,
  461. * exactly as if the argument were given to the {@link
  462. * #parseLong(java.lang.String)} method. The result is a
  463. * <code>Long</code> object that represents the integer value
  464. * specified by the string.
  465. * <p>
  466. * In other words, this method returns a <code>Long</code> object
  467. * equal to the value of:
  468. *
  469. * <blockquote><pre>
  470. * new Long(Long.parseLong(s))
  471. * </pre></blockquote>
  472. *
  473. * @param s the string to be parsed.
  474. * @return a <code>Long</code> object holding the value
  475. * represented by the string argument.
  476. * @exception NumberFormatException If the string cannot be parsed
  477. * as a <code>long</code>.
  478. */
  479. public static Long valueOf(String s) throws NumberFormatException
  480. {
  481. return new Long(parseLong(s, 10));
  482. }
  483. /**
  484. * Decodes a <code>String</code> into a <code>Long</code>.
  485. * Accepts decimal, hexadecimal, and octal numbers given by the
  486. * following grammar:
  487. *
  488. * <blockquote>
  489. * <dl>
  490. * <dt><i>DecodableString:</i>
  491. * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
  492. * <dd><i>Sign<sub>opt</sub></i> <code>0x</code> <i>HexDigits</i>
  493. * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i>
  494. * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i>
  495. * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i>
  496. * <p>
  497. * <dt><i>Sign:</i>
  498. * <dd><code>-</code>
  499. * </dl>
  500. * </blockquote>
  501. *
  502. * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
  503. * are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">§3.10.1</a>
  504. * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
  505. * Language Specification</a>.
  506. * <p>
  507. * The sequence of characters following an (optional) negative
  508. * sign and/or radix specifier ("<code>0x</code>",
  509. * "<code>0X</code>", "<code>#</code>", or
  510. * leading zero) is parsed as by the <code>Long.parseLong</code>
  511. * method with the indicated radix (10, 16, or 8). This sequence
  512. * of characters must represent a positive value or a {@link
  513. * NumberFormatException} will be thrown. The result is negated
  514. * if first character of the specified <code>String</code> is the
  515. * minus sign. No whitespace characters are permitted in the
  516. * <code>String</code>.
  517. *
  518. * @param nm the <code>String</code> to decode.
  519. * @return a <code>Long</code> object holding the <code>long</code>
  520. * value represented by <code>nm</code>
  521. * @exception NumberFormatException if the <code>String</code> does not
  522. * contain a parsable <code>long</code>.
  523. * @see java.lang.Long#parseLong(String, int)
  524. * @since 1.2
  525. */
  526. public static Long decode(String nm) throws NumberFormatException {
  527. int radix = 10;
  528. int index = 0;
  529. boolean negative = false;
  530. Long result;
  531. // Handle minus sign, if present
  532. if (nm.startsWith("-")) {
  533. negative = true;
  534. index++;
  535. }
  536. // Handle radix specifier, if present
  537. if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
  538. index += 2;
  539. radix = 16;
  540. }
  541. else if (nm.startsWith("#", index)) {
  542. index ++;
  543. radix = 16;
  544. }
  545. else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
  546. index ++;
  547. radix = 8;
  548. }
  549. if (nm.startsWith("-", index))
  550. throw new NumberFormatException("Negative sign in wrong position");
  551. try {
  552. result = Long.valueOf(nm.substring(index), radix);
  553. result = negative ? new Long((long)-result.longValue()) : result;
  554. } catch (NumberFormatException e) {
  555. // If number is Long.MIN_VALUE, we'll end up here. The next line
  556. // handles this case, and causes any genuine format error to be
  557. // rethrown.
  558. String constant = negative ? new String("-" + nm.substring(index))
  559. : nm.substring(index);
  560. result = Long.valueOf(constant, radix);
  561. }
  562. return result;
  563. }
  564. /**
  565. * The value of the <code>Long</code>.
  566. *
  567. * @serial
  568. */
  569. private long value;
  570. /**
  571. * Constructs a newly allocated <code>Long</code> object that
  572. * represents the specified <code>long</code> argument.
  573. *
  574. * @param value the value to be represented by the
  575. * <code>Long</code> object.
  576. */
  577. public Long(long value) {
  578. this.value = value;
  579. }
  580. /**
  581. * Constructs a newly allocated <code>Long</code> object that
  582. * represents the <code>long</code> value indicated by the
  583. * <code>String</code> parameter. The string is converted to a
  584. * <code>long</code> value in exactly the manner used by the
  585. * <code>parseLong</code> method for radix 10.
  586. *
  587. * @param s the <code>String</code> to be converted to a
  588. * <code>Long</code>.
  589. * @exception NumberFormatException if the <code>String</code> does not
  590. * contain a parsable <code>long</code>.
  591. * @see java.lang.Long#parseLong(java.lang.String, int)
  592. */
  593. public Long(String s) throws NumberFormatException {
  594. this.value = parseLong(s, 10);
  595. }
  596. /**
  597. * Returns the value of this <code>Long</code> as a
  598. * <code>byte</code>.
  599. */
  600. public byte byteValue() {
  601. return (byte)value;
  602. }
  603. /**
  604. * Returns the value of this <code>Long</code> as a
  605. * <code>short</code>.
  606. */
  607. public short shortValue() {
  608. return (short)value;
  609. }
  610. /**
  611. * Returns the value of this <code>Long</code> as an
  612. * <code>int</code>.
  613. */
  614. public int intValue() {
  615. return (int)value;
  616. }
  617. /**
  618. * Returns the value of this <code>Long</code> as a
  619. * <code>long</code> value.
  620. */
  621. public long longValue() {
  622. return (long)value;
  623. }
  624. /**
  625. * Returns the value of this <code>Long</code> as a
  626. * <code>float</code>.
  627. */
  628. public float floatValue() {
  629. return (float)value;
  630. }
  631. /**
  632. * Returns the value of this <code>Long</code> as a
  633. * <code>double</code>.
  634. */
  635. public double doubleValue() {
  636. return (double)value;
  637. }
  638. /**
  639. * Returns a <code>String</code> object representing this
  640. * <code>Long</code>'s value. The value is converted to signed
  641. * decimal representation and returned as a string, exactly as if
  642. * the <code>long</code> value were given as an argument to the
  643. * {@link java.lang.Long#toString(long)} method.
  644. *
  645. * @return a string representation of the value of this object in
  646. * base 10.
  647. */
  648. public String toString() {
  649. return String.valueOf(value);
  650. }
  651. /**
  652. * Returns a hash code for this <code>Long</code>. The result is
  653. * the exclusive OR of the two halves of the primitive
  654. * <code>long</code> value held by this <code>Long</code>
  655. * object. That is, the hashcode is the value of the expression:
  656. * <blockquote><pre>
  657. * (int)(this.longValue()^(this.longValue()>>>32))
  658. * </pre></blockquote>
  659. *
  660. * @return a hash code value for this object.
  661. */
  662. public int hashCode() {
  663. return (int)(value ^ (value >>> 32));
  664. }
  665. /**
  666. * Compares this object to the specified object. The result is
  667. * <code>true</code> if and only if the argument is not
  668. * <code>null</code> and is a <code>Long</code> object that
  669. * contains the same <code>long</code> value as this object.
  670. *
  671. * @param obj the object to compare with.
  672. * @return <code>true</code> if the objects are the same;
  673. * <code>false</code> otherwise.
  674. */
  675. public boolean equals(Object obj) {
  676. if (obj instanceof Long) {
  677. return value == ((Long)obj).longValue();
  678. }
  679. return false;
  680. }
  681. /**
  682. * Determines the <code>long</code> value of the system property
  683. * with the specified name.
  684. * <p>
  685. * The first argument is treated as the name of a system property.
  686. * System properties are accessible through the {@link
  687. * java.lang.System#getProperty(java.lang.String)} method. The
  688. * string value of this property is then interpreted as a
  689. * <code>long</code> value and a <code>Long</code> object
  690. * representing this value is returned. Details of possible
  691. * numeric formats can be found with the definition of
  692. * <code>getProperty</code>.
  693. * <p>
  694. * If there is no property with the specified name, if the
  695. * specified name is empty or <code>null</code>, or if the
  696. * property does not have the correct numeric format, then
  697. * <code>null</code> is returned.
  698. * <p>
  699. * In other words, this method returns a <code>Long</code> object equal to
  700. * the value of:
  701. * <blockquote><code>
  702. * getLong(nm, null)
  703. * </code></blockquote>
  704. *
  705. * @param nm property name.
  706. * @return the <code>Long</code> value of the property.
  707. * @see java.lang.System#getProperty(java.lang.String)
  708. * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  709. */
  710. public static Long getLong(String nm) {
  711. return getLong(nm, null);
  712. }
  713. /**
  714. * Determines the <code>long</code> value of the system property
  715. * with the specified name.
  716. * <p>
  717. * The first argument is treated as the name of a system property.
  718. * System properties are accessible through the {@link
  719. * java.lang.System#getProperty(java.lang.String)} method. The
  720. * string value of this property is then interpreted as a
  721. * <code>long</code> value and a <code>Long</code> object
  722. * representing this value is returned. Details of possible
  723. * numeric formats can be found with the definition of
  724. * <code>getProperty</code>.
  725. * <p>
  726. * The second argument is the default value. A <code>Long</code> object
  727. * that represents the value of the second argument is returned if there
  728. * is no property of the specified name, if the property does not have
  729. * the correct numeric format, or if the specified name is empty or null.
  730. * <p>
  731. * In other words, this method returns a <code>Long</code> object equal
  732. * to the value of:
  733. * <blockquote><code>
  734. * getLong(nm, new Long(val))
  735. * </code></blockquote>
  736. * but in practice it may be implemented in a manner such as:
  737. * <blockquote><pre>
  738. * Long result = getLong(nm, null);
  739. * return (result == null) ? new Long(val) : result;
  740. * </pre></blockquote>
  741. * to avoid the unnecessary allocation of a <code>Long</code> object when
  742. * the default value is not needed.
  743. *
  744. * @param nm property name.
  745. * @param val default value.
  746. * @return the <code>Long</code> value of the property.
  747. * @see java.lang.System#getProperty(java.lang.String)
  748. * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  749. */
  750. public static Long getLong(String nm, long val) {
  751. Long result = Long.getLong(nm, null);
  752. return (result == null) ? new Long(val) : result;
  753. }
  754. /**
  755. * Returns the <code>long</code> value of the system property with
  756. * the specified name. The first argument is treated as the name
  757. * of a system property. System properties are accessible through
  758. * the {@link java.lang.System#getProperty(java.lang.String)}
  759. * method. The string value of this property is then interpreted
  760. * as a <code>long</code> value, as per the
  761. * <code>Long.decode</code> method, and a <code>Long</code> object
  762. * representing this value is returned.
  763. * <p><ul>
  764. * <li>If the property value begins with the two ASCII characters
  765. * <code>0x</code> or the ASCII character <code>#</code>, not followed by
  766. * a minus sign, then the rest of it is parsed as a hexadecimal integer
  767. * exactly as for the method {@link #valueOf(java.lang.String, int)}
  768. * with radix 16.
  769. * <li>If the property value begins with the ASCII character
  770. * <code>0</code> followed by another character, it is parsed as
  771. * an octal integer exactly as by the method {@link
  772. * #valueOf(java.lang.String, int)} with radix 8.
  773. * <li>Otherwise the property value is parsed as a decimal
  774. * integer exactly as by the method
  775. * {@link #valueOf(java.lang.String, int)} with radix 10.
  776. * </ul>
  777. * <p>
  778. * Note that, in every case, neither <code>L</code>
  779. * (<code>'\u004C'</code>) nor <code>l</code>
  780. * (<code>'\u006C'</code>) is permitted to appear at the end
  781. * of the property value as a type indicator, as would be
  782. * permitted in Java programming language source code.
  783. * <p>
  784. * The second argument is the default value. The default value is
  785. * returned if there is no property of the specified name, if the
  786. * property does not have the correct numeric format, or if the
  787. * specified name is empty or <code>null</code>.
  788. *
  789. * @param nm property name.
  790. * @param val default value.
  791. * @return the <code>Long</code> value of the property.
  792. * @see java.lang.System#getProperty(java.lang.String)
  793. * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  794. * @see java.lang.Long#decode
  795. */
  796. public static Long getLong(String nm, Long val) {
  797. String v = null;
  798. try {
  799. v = System.getProperty(nm);
  800. } catch (IllegalArgumentException e) {
  801. } catch (NullPointerException e) {
  802. }
  803. if (v != null) {
  804. try {
  805. return Long.decode(v);
  806. } catch (NumberFormatException e) {
  807. }
  808. }
  809. return val;
  810. }
  811. /**
  812. * Compares two <code>Long</code> objects numerically.
  813. *
  814. * @param anotherLong the <code>Long</code> to be compared.
  815. * @return the value <code>0</code> if this <code>Long</code> is
  816. * equal to the argument <code>Long</code> a value less than
  817. * <code>0</code> if this <code>Long</code> is numerically less
  818. * than the argument <code>Long</code> and a value greater
  819. * than <code>0</code> if this <code>Long</code> is numerically
  820. * greater than the argument <code>Long</code> (signed
  821. * comparison).
  822. * @since 1.2
  823. */
  824. public int compareTo(Long anotherLong) {
  825. long thisVal = this.value;
  826. long anotherVal = anotherLong.value;
  827. return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
  828. }
  829. /**
  830. * Compares this <code>Long</code> object to another object. If
  831. * the object is a <code>Long</code>, this function behaves like
  832. * <code>compareTo(Long)</code>. Otherwise, it throws a
  833. * <code>ClassCastException</code> (as <code>Long</code> objects
  834. * are comparable only to other <code>Long</code> objects).
  835. *
  836. * @param o the <code>Object</code> to be compared.
  837. * @return the value <code>0</code> if the argument is a
  838. * <code>Long</code> numerically equal to this
  839. * <code>Long</code> a value less than <code>0</code>
  840. * if the argument is a <code>Long</code> numerically
  841. * greater than this <code>Long</code> and a value
  842. * greater than <code>0</code> if the argument is a
  843. * <code>Long</code> numerically less than this
  844. * <code>Long</code>.
  845. * @exception <code>ClassCastException</code> if the argument is not a
  846. * <code>Long</code>.
  847. * @see java.lang.Comparable
  848. * @since 1.2
  849. */
  850. public int compareTo(Object o) {
  851. return compareTo((Long)o);
  852. }
  853. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  854. private static final long serialVersionUID = 4290774380558885855L;
  855. }