1 7 8 package java.lang; 9 10 35 public final class Integer extends Number implements Comparable <Integer > { 36 40 public static final int MIN_VALUE = 0x80000000; 41 42 46 public static final int MAX_VALUE = 0x7fffffff; 47 48 54 public static final Class <Integer > TYPE = (Class <Integer >) Class.getPrimitiveClass("int"); 55 56 59 final static char[] digits = { 60 '0' , '1' , '2' , '3' , '4' , '5' , 61 '6' , '7' , '8' , '9' , 'a' , 'b' , 62 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 63 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 64 'o' , 'p' , 'q' , 'r' , 's' , 't' , 65 'u' , 'v' , 'w' , 'x' , 'y' , 'z' 66 }; 67 68 109 public static String toString(int i, int radix) { 110 111 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 112 radix = 10; 113 114 115 if (radix == 10) { 116 return toString(i); 117 } 118 119 char buf[] = new char[33]; 120 boolean negative = (i < 0); 121 int charPos = 32; 122 123 if (!negative) { 124 i = -i; 125 } 126 127 while (i <= -radix) { 128 buf[charPos--] = digits[-(i % radix)]; 129 i = i / radix; 130 } 131 buf[charPos] = digits[-i]; 132 133 if (negative) { 134 buf[--charPos] = '-'; 135 } 136 137 return new String (buf, charPos, (33 - charPos)); 138 } 139 140 171 public static String toHexString(int i) { 172 return toUnsignedString(i, 4); 173 } 174 175 201 public static String toOctalString(int i) { 202 return toUnsignedString(i, 3); 203 } 204 205 226 public static String toBinaryString(int i) { 227 return toUnsignedString(i, 1); 228 } 229 230 233 private static String toUnsignedString(int i, int shift) { 234 char[] buf = new char[32]; 235 int charPos = 32; 236 int radix = 1 << shift; 237 int mask = radix - 1; 238 do { 239 buf[--charPos] = digits[i & mask]; 240 i >>>= shift; 241 } while (i != 0); 242 243 return new String (buf, charPos, (32 - charPos)); 244 } 245 246 247 final static char [] DigitTens = { 248 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 249 '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', 250 '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', 251 '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', 252 '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', 253 '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', 254 '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', 255 '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', 256 '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', 257 '9', '9', '9', '9', '9', '9', '9', '9', '9', '9', 258 } ; 259 260 final static char [] DigitOnes = { 261 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 262 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 263 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 264 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 265 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 266 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 267 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 268 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 269 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 270 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 271 } ; 272 273 292 302 public static String toString(int i) { 303 if (i == Integer.MIN_VALUE) 304 return "-2147483648"; 305 int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); 306 char[] buf = new char[size]; 307 getChars(i, size, buf); 308 return new String (0, size, buf); 309 } 310 311 320 static void getChars(int i, int index, char[] buf) { 321 int q, r; 322 int charPos = index; 323 char sign = 0; 324 325 if (i < 0) { 326 sign = '-'; 327 i = -i; 328 } 329 330 while (i >= 65536) { 332 q = i / 100; 333 r = i - ((q << 6) + (q << 5) + (q << 2)); 335 i = q; 336 buf [--charPos] = DigitOnes[r]; 337 buf [--charPos] = DigitTens[r]; 338 } 339 340 for (;;) { 343 q = (i * 52429) >>> (16+3); 344 r = i - ((q << 3) + (q << 1)); buf [--charPos] = digits [r]; 346 i = q; 347 if (i == 0) break; 348 } 349 if (sign != 0) { 350 buf [--charPos] = sign; 351 } 352 } 353 354 final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 355 99999999, 999999999, Integer.MAX_VALUE }; 356 357 static int stringSize(int x) { 359 for (int i=0; ; i++) 360 if (x <= sizeTable[i]) 361 return i+1; 362 } 363 364 411 public static int parseInt(String s, int radix) 412 throws NumberFormatException 413 { 414 if (s == null) { 415 throw new NumberFormatException ("null"); 416 } 417 418 if (radix < Character.MIN_RADIX) { 419 throw new NumberFormatException ("radix " + radix + 420 " less than Character.MIN_RADIX"); 421 } 422 423 if (radix > Character.MAX_RADIX) { 424 throw new NumberFormatException ("radix " + radix + 425 " greater than Character.MAX_RADIX"); 426 } 427 428 int result = 0; 429 boolean negative = false; 430 int i = 0, max = s.length(); 431 int limit; 432 int multmin; 433 int digit; 434 435 if (max > 0) { 436 if (s.charAt(0) == '-') { 437 negative = true; 438 limit = Integer.MIN_VALUE; 439 i++; 440 } else { 441 limit = -Integer.MAX_VALUE; 442 } 443 multmin = limit / radix; 444 if (i < max) { 445 digit = Character.digit(s.charAt(i++),radix); 446 if (digit < 0) { 447 throw NumberFormatException.forInputString(s); 448 } else { 449 result = -digit; 450 } 451 } 452 while (i < max) { 453 digit = Character.digit(s.charAt(i++),radix); 455 if (digit < 0) { 456 throw NumberFormatException.forInputString(s); 457 } 458 if (result < multmin) { 459 throw NumberFormatException.forInputString(s); 460 } 461 result *= radix; 462 if (result < limit + digit) { 463 throw NumberFormatException.forInputString(s); 464 } 465 result -= digit; 466 } 467 } else { 468 throw NumberFormatException.forInputString(s); 469 } 470 if (negative) { 471 if (i > 1) { 472 return result; 473 } else { 474 throw NumberFormatException.forInputString(s); 475 } 476 } else { 477 return -result; 478 } 479 } 480 481 496 public static int parseInt(String s) throws NumberFormatException { 497 return parseInt(s,10); 498 } 499 500 525 public static Integer valueOf(String s, int radix) throws NumberFormatException { 526 return new Integer (parseInt(s,radix)); 527 } 528 529 551 public static Integer valueOf(String s) throws NumberFormatException 552 { 553 return new Integer (parseInt(s, 10)); 554 } 555 556 private static class IntegerCache { 557 private IntegerCache(){} 558 559 static final Integer cache[] = new Integer [-(-128) + 127 + 1]; 560 561 static { 562 for(int i = 0; i < cache.length; i++) 563 cache[i] = new Integer (i - 128); 564 } 565 } 566 567 580 public static Integer valueOf(int i) { 581 final int offset = 128; 582 if (i >= -128 && i <= 127) { return IntegerCache.cache[i + offset]; 584 } 585 return new Integer (i); 586 } 587 588 593 private final int value; 594 595 602 public Integer(int value) { 603 this.value = value; 604 } 605 606 619 public Integer(String s) throws NumberFormatException { 620 this.value = parseInt(s, 10); 621 } 622 623 627 public byte byteValue() { 628 return (byte)value; 629 } 630 631 635 public short shortValue() { 636 return (short)value; 637 } 638 639 643 public int intValue() { 644 return value; 645 } 646 647 651 public long longValue() { 652 return (long)value; 653 } 654 655 659 public float floatValue() { 660 return (float)value; 661 } 662 663 667 public double doubleValue() { 668 return (double)value; 669 } 670 671 681 public String toString() { 682 return String.valueOf(value); 683 } 684 685 692 public int hashCode() { 693 return value; 694 } 695 696 706 public boolean equals(Object obj) { 707 if (obj instanceof Integer ) { 708 return value == ((Integer )obj).intValue(); 709 } 710 return false; 711 } 712 713 741 public static Integer getInteger(String nm) { 742 return getInteger(nm, null); 743 } 744 745 782 public static Integer getInteger(String nm, int val) { 783 Integer result = getInteger(nm, null); 784 return (result == null) ? new Integer (val) : result; 785 } 786 787 822 public static Integer getInteger(String nm, Integer val) { 823 String v = null; 824 try { 825 v = System.getProperty(nm); 826 } catch (IllegalArgumentException e) { 827 } catch (NullPointerException e) { 828 } 829 if (v != null) { 830 try { 831 return Integer.decode(v); 832 } catch (NumberFormatException e) { 833 } 834 } 835 return val; 836 } 837 838 881 public static Integer decode(String nm) throws NumberFormatException { 882 int radix = 10; 883 int index = 0; 884 boolean negative = false; 885 Integer result; 886 887 if (nm.startsWith("-")) { 889 negative = true; 890 index++; 891 } 892 893 if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { 895 index += 2; 896 radix = 16; 897 } 898 else if (nm.startsWith("#", index)) { 899 index ++; 900 radix = 16; 901 } 902 else if (nm.startsWith("0", index) && nm.length() > 1 + index) { 903 index ++; 904 radix = 8; 905 } 906 907 if (nm.startsWith("-", index)) 908 throw new NumberFormatException ("Negative sign in wrong position"); 909 910 try { 911 result = Integer.valueOf(nm.substring(index), radix); 912 result = negative ? new Integer (-result.intValue()) : result; 913 } catch (NumberFormatException e) { 914 String constant = negative ? new String ("-" + nm.substring(index)) 918 : nm.substring(index); 919 result = Integer.valueOf(constant, radix); 920 } 921 return result; 922 } 923 924 937 public int compareTo(Integer anotherInteger) { 938 int thisVal = this.value; 939 int anotherVal = anotherInteger.value; 940 return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1)); 941 } 942 943 944 946 952 public static final int SIZE = 32; 953 954 966 public static int highestOneBit(int i) { 967 i |= (i >> 1); 969 i |= (i >> 2); 970 i |= (i >> 4); 971 i |= (i >> 8); 972 i |= (i >> 16); 973 return i - (i >>> 1); 974 } 975 976 988 public static int lowestOneBit(int i) { 989 return i & -i; 991 } 992 993 1013 public static int numberOfLeadingZeros(int i) { 1014 if (i == 0) 1016 return 32; 1017 int n = 1; 1018 if (i >>> 16 == 0) { n += 16; i <<= 16; } 1019 if (i >>> 24 == 0) { n += 8; i <<= 8; } 1020 if (i >>> 28 == 0) { n += 4; i <<= 4; } 1021 if (i >>> 30 == 0) { n += 2; i <<= 2; } 1022 n -= i >>> 31; 1023 return n; 1024 } 1025 1026 1039 public static int numberOfTrailingZeros(int i) { 1040 int y; 1042 if (i == 0) return 32; 1043 int n = 31; 1044 y = i <<16; if (y != 0) { n = n -16; i = y; } 1045 y = i << 8; if (y != 0) { n = n - 8; i = y; } 1046 y = i << 4; if (y != 0) { n = n - 4; i = y; } 1047 y = i << 2; if (y != 0) { n = n - 2; i = y; } 1048 return n - ((i << 1) >>> 31); 1049 } 1050 1051 1060 public static int bitCount(int i) { 1061 i = i - ((i >>> 1) & 0x55555555); 1063 i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); 1064 i = (i + (i >>> 4)) & 0x0f0f0f0f; 1065 i = i + (i >>> 8); 1066 i = i + (i >>> 16); 1067 return i & 0x3f; 1068 } 1069 1070 1088 public static int rotateLeft(int i, int distance) { 1089 return (i << distance) | (i >>> -distance); 1090 } 1091 1092 1110 public static int rotateRight(int i, int distance) { 1111 return (i >>> distance) | (i << -distance); 1112 } 1113 1114 1123 public static int reverse(int i) { 1124 i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555; 1126 i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333; 1127 i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f; 1128 i = (i << 24) | ((i & 0xff00) << 8) | 1129 ((i >>> 8) & 0xff00) | (i >>> 24); 1130 return i; 1131 } 1132 1133 1141 public static int signum(int i) { 1142 return (i >> 31) | (-i >>> 31); 1144 } 1145 1146 1154 public static int reverseBytes(int i) { 1155 return ((i >>> 24) ) | 1156 ((i >> 8) & 0xFF00) | 1157 ((i << 8) & 0xFF0000) | 1158 ((i << 24)); 1159 } 1160 1161 1162 private static final long serialVersionUID = 1360826667806852920L; 1163} 1164 | Popular Tags |