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