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