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