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