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