1 9 package javolution.text; 10 11 import j2me.lang.CharSequence; 12 import javolution.text.TextFormat.Cursor; 13 14 import java.io.IOException ; 15 16 44 public final class TypeFormat { 45 46 49 private TypeFormat() { 50 } 51 52 60 public static boolean parseBoolean(CharSequence csq) { 61 if ((csq.length() == 4) 62 && (csq.charAt(0) == 't' || csq.charAt(0) == 'T') 63 && (csq.charAt(1) == 'r' || csq.charAt(1) == 'R') 64 && (csq.charAt(2) == 'u' || csq.charAt(2) == 'U') 65 && (csq.charAt(3) == 'e' || csq.charAt(3) == 'E')) { 66 return true; 67 } else if ((csq.length() == 5) 68 && (csq.charAt(0) == 'f' || csq.charAt(0) == 'F') 69 && (csq.charAt(1) == 'a' || csq.charAt(1) == 'A') 70 && (csq.charAt(2) == 'l' || csq.charAt(2) == 'L') 71 && (csq.charAt(3) == 's' || csq.charAt(3) == 'S') 72 && (csq.charAt(4) == 'e' || csq.charAt(4) == 'E')) { 73 return false; 74 } 75 throw new IllegalArgumentException ("Cannot parse " + csq 76 + " as boolean"); 77 } 78 79 83 public static boolean parseBoolean(String csq) { 84 if ((csq.length() == 4) 85 && (csq.charAt(0) == 't' || csq.charAt(0) == 'T') 86 && (csq.charAt(1) == 'r' || csq.charAt(1) == 'R') 87 && (csq.charAt(2) == 'u' || csq.charAt(2) == 'U') 88 && (csq.charAt(3) == 'e' || csq.charAt(3) == 'E')) { 89 return true; 90 } else if ((csq.length() == 5) 91 && (csq.charAt(0) == 'f' || csq.charAt(0) == 'F') 92 && (csq.charAt(1) == 'a' || csq.charAt(1) == 'A') 93 && (csq.charAt(2) == 'l' || csq.charAt(2) == 'L') 94 && (csq.charAt(3) == 's' || csq.charAt(3) == 'S') 95 && (csq.charAt(4) == 'e' || csq.charAt(4) == 'E')) { 96 return false; 97 } 98 throw new IllegalArgumentException ("Cannot parse " + csq 99 + " as boolean"); 100 } 101 102 113 public static boolean parseBoolean(CharSequence csq, Cursor cursor) { 114 final int i = cursor.getIndex(); 115 if ((cursor.getEndIndex() > i + 4) 116 && (csq.charAt(i) == 't' || csq.charAt(i) == 'T') 117 && (csq.charAt(i + 1) == 'r' || csq.charAt(i + 1) == 'R') 118 && (csq.charAt(i + 2) == 'u' || csq.charAt(i + 2) == 'U') 119 && (csq.charAt(i + 3) == 'e' || csq.charAt(i + 3) == 'E')) { 120 cursor.increment(4); 121 return true; 122 } 123 if ((cursor.getEndIndex() > i + 5) 124 && (csq.charAt(i) == 'f' || csq.charAt(i) == 'F') 125 && (csq.charAt(i + 1) == 'a' || csq.charAt(i + 1) == 'A') 126 && (csq.charAt(i + 2) == 'l' || csq.charAt(i + 2) == 'L') 127 && (csq.charAt(i + 3) == 's' || csq.charAt(i + 3) == 'S') 128 && (csq.charAt(i + 4) == 'e' || csq.charAt(i + 4) == 'E')) { 129 cursor.increment(5); 130 return false; 131 } 132 throw new IllegalArgumentException ("Cannot parse boolean at " 133 + cursor.getIndex()); 134 } 135 136 146 public static byte parseByte(CharSequence csq) { 147 return parseByte(csq, 10); 148 } 149 150 160 public static byte parseByte(CharSequence csq, int radix) { 161 int i = parseInt(csq, radix); 162 if ((i < Byte.MIN_VALUE) || (i > Byte.MAX_VALUE)) 163 throw new NumberFormatException ("Overflow"); 164 return (byte) i; 165 } 166 167 178 public static byte parseByte(CharSequence csq, int radix, Cursor cursor) { 179 int i = parseInt(csq, radix, cursor); 180 if ((i < Byte.MIN_VALUE) || (i > Byte.MAX_VALUE)) 181 throw new NumberFormatException ("Overflow"); 182 return (byte) i; 183 } 184 185 195 public static short parseShort(CharSequence csq) { 196 return parseShort(csq, 10); 197 } 198 199 209 public static short parseShort(CharSequence csq, int radix) { 210 int i = parseInt(csq, radix); 211 if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) 212 throw new NumberFormatException ("Overflow"); 213 return (short) i; 214 } 215 216 227 public static short parseShort(CharSequence csq, int radix, Cursor cursor) { 228 int i = parseInt(csq, radix, cursor); 229 if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) 230 throw new NumberFormatException ("Overflow"); 231 return (short) i; 232 } 233 234 243 public static int parseInt(CharSequence csq) { 244 return parseInt(csq, 10); 245 } 246 247 250 public static int parseInt(String str) { 251 return parseIntString(str, 10, null); 252 } 253 254 264 public static int parseInt(CharSequence csq, int radix) { 265 return parseInt(csq, radix, null); 266 } 267 268 272 public static int parseInt(String str, int radix) { 273 return parseIntString(str, radix, null); 274 } 275 276 288 public static int parseInt(CharSequence csq, int radix, Cursor cursor) { 289 if (csq instanceof CharArray) 291 return parseIntCharArray((CharArray) csq, radix, cursor); 292 if (csq instanceof TextBuilder) 293 return parseIntTextBuilder((TextBuilder) csq, radix, cursor); 294 if (csq instanceof Text) 295 return parseIntText((Text) csq, radix, cursor); 296 if (((Object ) csq) instanceof String ) 297 return parseIntString((String ) ((Object ) csq), radix, cursor); 298 return parseIntCharSequence(csq, radix, cursor); 299 } 300 301 private static int parseIntCharArray(CharArray csq, int radix, Cursor cursor) { 302 final int start = (cursor != null) ? cursor.getIndex() : 0; 304 final int end = (cursor != null) ? cursor.getEndIndex() : csq.length(); 305 boolean isNegative = false; 306 int result = 0; int i = start; 308 for (; i < end; i++) { 309 char c = csq.charAt(i); 310 int digit = (c <= '9') ? c - '0' 311 : ((c <= 'Z') && (c >= 'A')) ? c - 'A' + 10 312 : ((c <= 'z') && (c >= 'a')) ? c - 'a' + 10 : -1; 313 if ((digit >= 0) && (digit < radix)) { 314 int newResult = result * radix - digit; 315 if (newResult > result) 316 throw new NumberFormatException ("Overflow"); 317 result = newResult; 318 } else if ((c == '-') && (i == start)) { 319 isNegative = true; 320 } else if ((c == '+') && (i == start)) { 321 } else { 323 if (cursor == null) 324 throw new NumberFormatException ("Incomplete parsing"); 325 break; } 327 } 328 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) != '0'))) 330 throw new NumberFormatException ("Cannot parse " + csq + " as int"); 331 if ((result == Integer.MIN_VALUE) && !isNegative) 332 throw new NumberFormatException ("Overflow"); 333 if (cursor != null) 334 cursor.setIndex(i); 335 return isNegative ? result : -result; 336 } 337 338 private static int parseIntTextBuilder(TextBuilder csq, int radix, 339 Cursor cursor) { 340 final int start = (cursor != null) ? cursor.getIndex() : 0; 342 final int end = (cursor != null) ? cursor.getEndIndex() : csq.length(); 343 boolean isNegative = false; 344 int result = 0; int i = start; 346 for (; i < end; i++) { 347 char c = csq.charAt(i); 348 int digit = (c <= '9') ? c - '0' 349 : ((c <= 'Z') && (c >= 'A')) ? c - 'A' + 10 350 : ((c <= 'z') && (c >= 'a')) ? c - 'a' + 10 : -1; 351 if ((digit >= 0) && (digit < radix)) { 352 int newResult = result * radix - digit; 353 if (newResult > result) 354 throw new NumberFormatException ("Overflow"); 355 result = newResult; 356 } else if ((c == '-') && (i == start)) { 357 isNegative = true; 358 } else if ((c == '+') && (i == start)) { 359 } else { 361 if (cursor == null) 362 throw new NumberFormatException ("Incomplete parsing"); 363 break; } 365 } 366 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) != '0'))) 368 throw new NumberFormatException ("Cannot parse " + csq + " as int"); 369 if ((result == Integer.MIN_VALUE) && !isNegative) 370 throw new NumberFormatException ("Overflow"); 371 if (cursor != null) 372 cursor.setIndex(i); 373 return isNegative ? result : -result; 374 } 375 376 private static int parseIntText(Text csq, int radix, Cursor cursor) { 377 final int start = (cursor != null) ? cursor.getIndex() : 0; 379 final int end = (cursor != null) ? cursor.getEndIndex() : csq.length(); 380 boolean isNegative = false; 381 int result = 0; int i = start; 383 for (; i < end; i++) { 384 char c = csq.charAt(i); 385 int digit = (c <= '9') ? c - '0' 386 : ((c <= 'Z') && (c >= 'A')) ? c - 'A' + 10 387 : ((c <= 'z') && (c >= 'a')) ? c - 'a' + 10 : -1; 388 if ((digit >= 0) && (digit < radix)) { 389 int newResult = result * radix - digit; 390 if (newResult > result) 391 throw new NumberFormatException ("Overflow"); 392 result = newResult; 393 } else if ((c == '-') && (i == start)) { 394 isNegative = true; 395 } else if ((c == '+') && (i == start)) { 396 } else { 398 if (cursor == null) 399 throw new NumberFormatException ("Incomplete parsing"); 400 break; } 402 } 403 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) != '0'))) 405 throw new NumberFormatException ("Cannot parse " + csq + " as int"); 406 if ((result == Integer.MIN_VALUE) && !isNegative) 407 throw new NumberFormatException ("Overflow"); 408 if (cursor != null) 409 cursor.setIndex(i); 410 return isNegative ? result : -result; 411 } 412 413 private static int parseIntString(String csq, int radix, Cursor cursor) { 414 final int start = (cursor != null) ? cursor.getIndex() : 0; 416 final int end = (cursor != null) ? cursor.getEndIndex() : csq.length(); 417 boolean isNegative = false; 418 int result = 0; int i = start; 420 for (; i < end; i++) { 421 char c = csq.charAt(i); 422 int digit = (c <= '9') ? c - '0' 423 : ((c <= 'Z') && (c >= 'A')) ? c - 'A' + 10 424 : ((c <= 'z') && (c >= 'a')) ? c - 'a' + 10 : -1; 425 if ((digit >= 0) && (digit < radix)) { 426 int newResult = result * radix - digit; 427 if (newResult > result) 428 throw new NumberFormatException ("Overflow"); 429 result = newResult; 430 } else if ((c == '-') && (i == start)) { 431 isNegative = true; 432 } else if ((c == '+') && (i == start)) { 433 } else { 435 if (cursor == null) 436 throw new NumberFormatException ("Incomplete parsing"); 437 break; } 439 } 440 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) != '0'))) 442 throw new NumberFormatException ("Cannot parse " + csq + " as int"); 443 if ((result == Integer.MIN_VALUE) && !isNegative) 444 throw new NumberFormatException ("Overflow"); 445 if (cursor != null) 446 cursor.setIndex(i); 447 return isNegative ? result : -result; 448 } 449 450 private static int parseIntCharSequence(CharSequence csq, int radix, 451 Cursor cursor) { 452 final int start = (cursor != null) ? cursor.getIndex() : 0; 454 final int end = (cursor != null) ? cursor.getEndIndex() : csq.length(); 455 boolean isNegative = false; 456 int result = 0; int i = start; 458 for (; i < end; i++) { 459 char c = csq.charAt(i); 460 int digit = (c <= '9') ? c - '0' 461 : ((c <= 'Z') && (c >= 'A')) ? c - 'A' + 10 462 : ((c <= 'z') && (c >= 'a')) ? c - 'a' + 10 : -1; 463 if ((digit >= 0) && (digit < radix)) { 464 int newResult = result * radix - digit; 465 if (newResult > result) 466 throw new NumberFormatException ("Overflow"); 467 result = newResult; 468 } else if ((c == '-') && (i == start)) { 469 isNegative = true; 470 } else if ((c == '+') && (i == start)) { 471 } else { 473 if (cursor == null) 474 throw new NumberFormatException ("Incomplete parsing"); 475 break; } 477 } 478 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) != '0'))) 480 throw new NumberFormatException ("Cannot parse " + csq + " as int"); 481 if ((result == Integer.MIN_VALUE) && !isNegative) 482 throw new NumberFormatException ("Overflow"); 483 if (cursor != null) 484 cursor.setIndex(i); 485 return isNegative ? result : -result; 486 } 487 488 497 public static long parseLong(CharSequence csq) { 498 return parseLong(csq, 10); 499 } 500 501 505 public static long parseLong(String str) { 506 return parseLongString(str, 10, null); 507 } 508 509 519 public static long parseLong(CharSequence csq, int radix) { 520 return parseLong(csq, radix, null); 521 } 522 523 527 public static long parseLong(String str, int radix) { 528 return parseLongString(str, radix, null); 529 } 530 531 543 public static long parseLong(CharSequence csq, int radix, Cursor cursor) { 544 if (csq instanceof CharArray) 546 return parseLongCharArray((CharArray) csq, radix, cursor); 547 if (csq instanceof TextBuilder) 548 return parseLongTextBuilder((TextBuilder) csq, radix, cursor); 549 if (csq instanceof Text) 550 return parseLongText((Text) csq, radix, cursor); 551 if (((Object ) csq) instanceof String ) 552 return parseLongString((String ) ((Object ) csq), radix, cursor); 553 return parseLongCharSequence(csq, radix, cursor); 554 } 555 556 private static long parseLongCharArray(CharArray csq, int radix, 557 Cursor cursor) { 558 final int start = (cursor != null) ? cursor.getIndex() : 0; 560 final int end = (cursor != null) ? cursor.getEndIndex() : csq.length(); 561 boolean isNegative = false; 562 long result = 0; int i = start; 564 for (; i < end; i++) { 565 char c = csq.charAt(i); 566 int digit = (c <= '9') ? c - '0' 567 : ((c <= 'Z') && (c >= 'A')) ? c - 'A' + 10 568 : ((c <= 'z') && (c >= 'a')) ? c - 'a' + 10 : -1; 569 if ((digit >= 0) && (digit < radix)) { 570 long newResult = result * radix - digit; 571 if (newResult > result) 572 throw new NumberFormatException ("Overflow"); 573 result = newResult; 574 } else if ((c == '-') && (i == start)) { 575 isNegative = true; 576 } else if ((c == '+') && (i == start)) { 577 } else { 579 if (cursor == null) 580 throw new NumberFormatException ("Incomplete parsing"); 581 break; } 583 } 584 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) != '0'))) 586 throw new NumberFormatException ("Cannot parse " + csq + " as int"); 587 if ((result == Long.MIN_VALUE) && !isNegative) 588 throw new NumberFormatException ("Overflow"); 589 if (cursor != null) 590 cursor.setIndex(i); 591 return isNegative ? result : -result; 592 } 593 594 private static long parseLongTextBuilder(TextBuilder csq, int radix, 595 Cursor cursor) { 596 final int start = (cursor != null) ? cursor.getIndex() : 0; 598 final int end = (cursor != null) ? cursor.getEndIndex() : csq.length(); 599 boolean isNegative = false; 600 long result = 0; int i = start; 602 for (; i < end; i++) { 603 char c = csq.charAt(i); 604 int digit = (c <= '9') ? c - '0' 605 : ((c <= 'Z') && (c >= 'A')) ? c - 'A' + 10 606 : ((c <= 'z') && (c >= 'a')) ? c - 'a' + 10 : -1; 607 if ((digit >= 0) && (digit < radix)) { 608 long newResult = result * radix - digit; 609 if (newResult > result) 610 throw new NumberFormatException ("Overflow"); 611 result = newResult; 612 } else if ((c == '-') && (i == start)) { 613 isNegative = true; 614 } else if ((c == '+') && (i == start)) { 615 } else { 617 if (cursor == null) 618 throw new NumberFormatException ("Incomplete parsing"); 619 break; } 621 } 622 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) != '0'))) 624 throw new NumberFormatException ("Cannot parse " + csq + " as int"); 625 if ((result == Long.MIN_VALUE) && !isNegative) 626 throw new NumberFormatException ("Overflow"); 627 if (cursor != null) 628 cursor.setIndex(i); 629 return isNegative ? result : -result; 630 } 631 632 private static long parseLongText(Text csq, int radix, Cursor cursor) { 633 final int start = (cursor != null) ? cursor.getIndex() : 0; 635 final int end = (cursor != null) ? cursor.getEndIndex() : csq.length(); 636 boolean isNegative = false; 637 long result = 0; int i = start; 639 for (; i < end; i++) { 640 char c = csq.charAt(i); 641 int digit = (c <= '9') ? c - '0' 642 : ((c <= 'Z') && (c >= 'A')) ? c - 'A' + 10 643 : ((c <= 'z') && (c >= 'a')) ? c - 'a' + 10 : -1; 644 if ((digit >= 0) && (digit < radix)) { 645 long newResult = result * radix - digit; 646 if (newResult > result) 647 throw new NumberFormatException ("Overflow"); 648 result = newResult; 649 } else if ((c == '-') && (i == start)) { 650 isNegative = true; 651 } else if ((c == '+') && (i == start)) { 652 } else { 654 if (cursor == null) 655 throw new NumberFormatException ("Incomplete parsing"); 656 break; } 658 } 659 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) != '0'))) 661 throw new NumberFormatException ("Cannot parse " + csq + " as int"); 662 if ((result == Long.MIN_VALUE) && !isNegative) 663 throw new NumberFormatException ("Overflow"); 664 if (cursor != null) 665 cursor.setIndex(i); 666 return isNegative ? result : -result; 667 } 668 669 private static long parseLongString(String csq, int radix, Cursor cursor) { 670 final int start = (cursor != null) ? cursor.getIndex() : 0; 672 final int end = (cursor != null) ? cursor.getEndIndex() : csq.length(); 673 boolean isNegative = false; 674 long result = 0; int i = start; 676 for (; i < end; i++) { 677 char c = csq.charAt(i); 678 int digit = (c <= '9') ? c - '0' 679 : ((c <= 'Z') && (c >= 'A')) ? c - 'A' + 10 680 : ((c <= 'z') && (c >= 'a')) ? c - 'a' + 10 : -1; 681 if ((digit >= 0) && (digit < radix)) { 682 long newResult = result * radix - digit; 683 if (newResult > result) 684 throw new NumberFormatException ("Overflow"); 685 result = newResult; 686 } else if ((c == '-') && (i == start)) { 687 isNegative = true; 688 } else if ((c == '+') && (i == start)) { 689 } else { 691 if (cursor == null) 692 throw new NumberFormatException ("Incomplete parsing"); 693 break; } 695 } 696 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) != '0'))) 698 throw new NumberFormatException ("Cannot parse " + csq + " as int"); 699 if ((result == Long.MIN_VALUE) && !isNegative) 700 throw new NumberFormatException ("Overflow"); 701 if (cursor != null) 702 cursor.setIndex(i); 703 return isNegative ? result : -result; 704 } 705 706 private static long parseLongCharSequence(CharSequence csq, int radix, 707 Cursor cursor) { 708 final int start = (cursor != null) ? cursor.getIndex() : 0; 710 final int end = (cursor != null) ? cursor.getEndIndex() : csq.length(); 711 boolean isNegative = false; 712 long result = 0; int i = start; 714 for (; i < end; i++) { 715 char c = csq.charAt(i); 716 int digit = (c <= '9') ? c - '0' 717 : ((c <= 'Z') && (c >= 'A')) ? c - 'A' + 10 718 : ((c <= 'z') && (c >= 'a')) ? c - 'a' + 10 : -1; 719 if ((digit >= 0) && (digit < radix)) { 720 long newResult = result * radix - digit; 721 if (newResult > result) 722 throw new NumberFormatException ("Overflow"); 723 result = newResult; 724 } else if ((c == '-') && (i == start)) { 725 isNegative = true; 726 } else if ((c == '+') && (i == start)) { 727 } else { 729 if (cursor == null) 730 throw new NumberFormatException ("Incomplete parsing"); 731 break; } 733 } 734 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) != '0'))) 736 throw new NumberFormatException ("Cannot parse " + csq + " as int"); 737 if ((result == Long.MIN_VALUE) && !isNegative) 738 throw new NumberFormatException ("Overflow"); 739 if (cursor != null) 740 cursor.setIndex(i); 741 return isNegative ? result : -result; 742 } 743 744 754 755 763 764 776 777 792 793 801 802 1248 1249 1259 public static Appendable format(boolean b, Appendable a) throws IOException { 1260 return b ? a.append('t').append('r').append('u').append('e') : a 1261 .append('f').append('a').append('l').append('s').append('e'); 1262 } 1263 1264 1278 public static Appendable format(int i, Appendable a) throws IOException { 1279 if (a instanceof TextBuilder) 1280 return ((TextBuilder) a).append(i); 1281 TextBuilder tmp = TextBuilder.newInstance(); 1282 tmp.append(i); 1283 appendTo(a, tmp); 1284 TextBuilder.recycle(tmp); 1285 return a; 1286 } 1287 1288 1300 public static Appendable format(int i, int radix, Appendable a) 1301 throws IOException { 1302 if (a instanceof TextBuilder) 1303 return ((TextBuilder) a).append(i, radix); 1304 TextBuilder tmp = TextBuilder.newInstance(); 1305 tmp.append(i, radix); 1306 appendTo(a, tmp); 1307 TextBuilder.recycle(tmp); 1308 return a; 1309 } 1310 1311 1325 public static Appendable format(long l, Appendable a) throws IOException { 1326 if (a instanceof TextBuilder) 1327 return ((TextBuilder) a).append(l); 1328 TextBuilder tmp = TextBuilder.newInstance(); 1329 tmp.append(l); 1330 appendTo(a, tmp); 1331 TextBuilder.recycle(tmp); 1332 return a; 1333 } 1334 1335 1347 public static Appendable format(long l, int radix, Appendable a) 1348 throws IOException { 1349 if (a instanceof TextBuilder) 1350 return ((TextBuilder) a).append(l); 1351 TextBuilder tmp = TextBuilder.newInstance(); 1352 tmp.append(l, radix); 1353 appendTo(a, tmp); 1354 TextBuilder.recycle(tmp); 1355 return a; 1356 } 1357 1358 1378 1379 1399 1400 1429 1430 1438 private static void appendTo(Object to, TextBuilder txt) throws IOException { 1439 if (to instanceof StringBuffer ) { 1440 txt.appendTo((StringBuffer ) to); 1441 1445 } else { 1446 ((Appendable ) to).append(txt); 1447 } 1448 } 1449 1450} | Popular Tags |