1 9 package javolution.lang; 10 11 import j2me.lang.CharSequence; 12 13 import java.io.IOException; 14 15 31 public final class TypeFormat { 32 33 36 private final static char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', 37 '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 38 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 39 'x', 'y', 'z' }; 40 41 44 private TypeFormat() { 45 } 46 47 60 public static int indexOf(CharSequence pattern, CharSequence chars, 61 int fromIndex) { 62 int patternLength = pattern.length(); 63 fromIndex = Math.max(0, fromIndex); 64 if (patternLength != 0) { char firstChar = pattern.charAt(0); 66 int last = chars.length() - patternLength; 67 for (int i = fromIndex; i <= last; i++) { 68 if (chars.charAt(i) == firstChar) { 69 boolean match = true; 70 for (int j = 1; j < patternLength; j++) { 71 if (chars.charAt(i + j) != pattern.charAt(j)) { 72 match = false; 73 break; 74 } 75 } 76 if (match) { 77 return i; 78 } 79 } 80 } 81 return -1; 82 } else { 83 return Math.min(0, fromIndex); 84 } 85 } 86 87 100 public static int lastIndexOf(CharSequence pattern, CharSequence chars, 101 int fromIndex) { 102 int patternLength = pattern.length(); 103 fromIndex = Math.min(chars.length() - pattern.length(), fromIndex); 104 if (patternLength != 0) { char firstChar = pattern.charAt(0); 106 for (int i = fromIndex; i >= 0; i--) { 107 if (chars.charAt(i) == firstChar) { 108 boolean match = true; 109 for (int j = 1; j < patternLength; j++) { 110 if (chars.charAt(i + j) != pattern.charAt(j)) { 111 match = false; 112 break; 113 } 114 } 115 if (match) { 116 return i; 117 } 118 } 119 } 120 return -1; 121 } else { 122 return Math.max(-1, fromIndex); 123 } 124 } 125 126 132 public static boolean parseBoolean(CharSequence chars) { 133 return (chars.length() == 4) 134 && (chars.charAt(0) == 't' || chars.charAt(0) == 'T') 135 && (chars.charAt(1) == 'r' || chars.charAt(1) == 'R') 136 && (chars.charAt(2) == 'u' || chars.charAt(2) == 'U') 137 && (chars.charAt(3) == 'e' || chars.charAt(3) == 'E'); 138 } 139 140 150 public static byte parseByte(CharSequence chars) { 151 return parseByte(chars, 10); 152 } 153 154 167 public static byte parseByte(CharSequence chars, int radix) { 168 try { 169 boolean isNegative = (chars.charAt(0) == '-') ? true : false; 170 int result = 0; 171 int limit = (isNegative) ? Byte.MIN_VALUE : -Byte.MAX_VALUE; 172 int multmin = limit / radix; 173 int length = chars.length(); 174 int i = (isNegative || (chars.charAt(0) == '+')) ? 1 : 0; 175 while (true) { 176 int digit = Character.digit(chars.charAt(i), radix); 177 int tmp = result * radix; 178 if ((digit < 0) || (result < multmin) || (tmp < limit + digit)) { throw new NumberFormatException("For input characters: \"" 180 + chars.toString() + "\""); 181 } 182 result = tmp - digit; 184 if (++i >= length) { 185 break; 186 } 187 } 188 return (byte) (isNegative ? result : -result); 189 } catch (IndexOutOfBoundsException e) { 190 throw new NumberFormatException("For input characters: \"" 191 + chars.toString() + "\""); 192 } 193 } 194 195 205 public static short parseShort(CharSequence chars) { 206 return parseShort(chars, 10); 207 } 208 209 222 public static short parseShort(CharSequence chars, int radix) { 223 try { 224 boolean isNegative = (chars.charAt(0) == '-') ? true : false; 225 int result = 0; 226 int limit = (isNegative) ? Short.MIN_VALUE : -Short.MAX_VALUE; 227 int multmin = limit / radix; 228 int length = chars.length(); 229 int i = (isNegative || (chars.charAt(0) == '+')) ? 1 : 0; 230 while (true) { 231 int digit = Character.digit(chars.charAt(i), radix); 232 int tmp = result * radix; 233 if ((digit < 0) || (result < multmin) || (tmp < limit + digit)) { throw new NumberFormatException("For input characters: \"" 235 + chars.toString() + "\""); 236 } 237 result = tmp - digit; 239 if (++i >= length) { 240 break; 241 } 242 } 243 return (short) (isNegative ? result : -result); 244 } catch (IndexOutOfBoundsException e) { 245 throw new NumberFormatException("For input characters: \"" 246 + chars.toString() + "\""); 247 } 248 } 249 250 260 public static int parseInt(CharSequence chars) { 261 return parseInt(chars, 10); 262 } 263 264 277 public static int parseInt(CharSequence chars, int radix) { 278 try { 279 boolean isNegative = (chars.charAt(0) == '-') ? true : false; 280 int result = 0; 281 int limit = (isNegative) ? Integer.MIN_VALUE : -Integer.MAX_VALUE; 282 int multmin = limit / radix; 283 int length = chars.length(); 284 int i = (isNegative || (chars.charAt(0) == '+')) ? 1 : 0; 285 while (true) { 286 int digit = Character.digit(chars.charAt(i), radix); 287 int tmp = result * radix; 288 if ((digit < 0) || (result < multmin) || (tmp < limit + digit)) { throw new NumberFormatException("For input characters: \"" 290 + chars.toString() + "\""); 291 } 292 result = tmp - digit; 294 if (++i >= length) { 295 break; 296 } 297 } 298 return isNegative ? result : -result; 299 } catch (IndexOutOfBoundsException e) { 300 throw new NumberFormatException("For input characters: \"" 301 + chars.toString() + "\""); 302 } 303 } 304 305 315 public static long parseLong(CharSequence chars) { 316 return parseLong(chars, 10); 317 } 318 319 332 public static long parseLong(CharSequence chars, int radix) { 333 try { 334 boolean isNegative = (chars.charAt(0) == '-') ? true : false; 335 long result = 0; 336 long limit = (isNegative) ? Long.MIN_VALUE : -Long.MAX_VALUE; 337 long multmin = limit / radix; 338 int length = chars.length(); 339 int i = (isNegative || (chars.charAt(0) == '+')) ? 1 : 0; 340 while (true) { 341 int digit = Character.digit(chars.charAt(i), radix); 342 long tmp = result * radix; 343 if ((digit < 0) || (result < multmin) || (tmp < limit + digit)) { throw new NumberFormatException("For input characters: \"" 345 + chars.toString() + "\""); 346 } 347 result = tmp - digit; 349 if (++i >= length) { 350 break; 351 } 352 } 353 return isNegative ? result : -result; 354 } catch (IndexOutOfBoundsException e) { 355 throw new NumberFormatException("For input characters: \"" 356 + chars.toString() + "\""); 357 } 358 } 359 360 379 380 465 466 476 public static Appendable format(boolean b, Appendable chars) 477 throws IOException { 478 return b ? append(chars, "true") : append(chars, "false"); 479 } 480 481 495 public static Appendable format(int i, Appendable chars) throws IOException { 496 if (i <= 0) { 497 if (i == Integer.MIN_VALUE) { return append(chars, "-2147483648"); } else if (i == 0) { 500 return chars.append('0'); 501 } 502 i = -i; 503 chars.append('-'); 504 } 505 int j = 1; 506 for (; (j < 10) && (i >= INT_POW_10[j]); j++) { 507 } 508 for (j--; j >= 0; j--) { 510 int pow10 = INT_POW_10[j]; 511 int digit = i / pow10; 512 i -= digit * pow10; 513 chars.append(DIGITS[digit]); 514 } 515 return chars; 516 } 517 518 private static final int[] INT_POW_10 = new int[10]; 519 static { 520 int pow = 1; 521 for (int i = 0; i < 10; i++) { 522 INT_POW_10[i] = pow; 523 pow *= 10; 524 } 525 } 526 527 539 public static Appendable format(int i, int radix, Appendable chars) 540 throws IOException { 541 if (radix == 10) { 542 return format(i, chars); } else if (radix < 2 || radix > 36) { 544 throw new IllegalArgumentException("radix: " + radix); 545 } 546 if (i < 0) { 547 chars.append('-'); 548 } else { 549 i = -i; 550 } 551 format2(i, radix, chars); 552 return chars; 553 } 554 555 private static void format2(int i, int radix, Appendable chars) 556 throws IOException { 557 if (i <= -radix) { 558 format2(i / radix, radix, chars); 559 chars.append(DIGITS[-(i % radix)]); 560 } else { 561 chars.append(DIGITS[-i]); 562 } 563 } 564 565 579 public static Appendable format(long l, Appendable chars) 580 throws IOException { 581 if (l <= 0) { 582 if (l == Long.MIN_VALUE) { return append(chars, "-9223372036854775808"); } else if (l == 0) { 585 return chars.append('0'); 586 } 587 l = -l; 588 chars.append('-'); 589 } 590 int j = 1; 591 for (; (j < 19) && (l >= LONG_POW_10[j]); j++) { 592 } 593 for (j--; j >= 0; j--) { 595 long pow10 = LONG_POW_10[j]; 596 int digit = (int) (l / pow10); 597 l -= digit * pow10; 598 chars.append(DIGITS[digit]); 599 } 600 return chars; 601 } 602 603 private static final long[] LONG_POW_10 = new long[19]; 604 static { 605 long pow = 1; 606 for (int i = 0; i < 19; i++) { 607 LONG_POW_10[i] = pow; 608 pow *= 10; 609 } 610 } 611 612 624 public static Appendable format(long l, int radix, Appendable chars) 625 throws IOException { 626 if (radix == 10) { 627 return format(l, chars); } else if (radix < 2 || radix > 36) { 629 throw new IllegalArgumentException("radix: " + radix); 630 } 631 if (l < 0) { 632 chars.append('-'); 633 } else { 634 l = -l; 635 } 636 format2(l, radix, chars); 637 return chars; 638 } 639 640 private static void format2(long l, int radix, Appendable chars) 641 throws IOException { 642 if (l <= -radix) { 643 format2(l / radix, radix, chars); 644 chars.append(DIGITS[(int) -(l % radix)]); 645 } else { 646 chars.append(DIGITS[(int) -l]); 647 } 648 } 649 650 664 665 685 686 784 785 795 private static Appendable append(Appendable to, String str) 796 throws IOException { 797 for (int i = 0; i < str.length(); i++) { 798 to.append(str.charAt(i)); 799 } 800 return to; 801 } 802 803 876 } | Popular Tags |