1 24 package fr.dyade.aaa.util; 25 26 import java.lang.reflect.Method ; 27 28 import java.util.*; 29 import java.io.*; 30 31 32 36 public class Strings { 37 45 public static final void toString(StringBuffer output, Object obj) { 46 if (obj == null) { 47 output.append("null"); 48 return; 49 } 50 51 if (obj instanceof String ) { 52 toString(output, (String ) obj); 53 return; 54 } 55 if (obj instanceof List) { 56 toString(output, (List) obj); 57 return; 58 } 59 if (obj instanceof Collection) { 60 toString(output, (Collection) obj); 61 return; 62 } 63 if (obj instanceof Map) { 64 toString(output, (Map) obj); 65 return; 66 } 67 if (obj instanceof Map.Entry) { 68 toString(output, (Map.Entry) obj); 69 return; 70 } 71 72 Class type = obj.getClass(); 73 if (type.isArray()) { 74 toString(output, obj, type.getComponentType()); 75 return; 76 } 77 78 try { 79 Class [] argstype = new Class [1]; 80 argstype[0] = Class.forName("java.io.StringBuffer"); 81 Method method = type.getMethod("toString", argstype); 82 Object [] args = new Object [1]; 83 args[0] = output; 84 method.invoke(obj, args); 85 return; 86 } catch (Exception exc) {} 87 88 output.append(obj.toString()); 89 } 90 91 98 public static final String toString(Object obj) { 99 if (obj == null) 100 return "null"; 101 102 if (obj instanceof String ) { 103 return toString((String ) obj); 105 } 106 107 StringBuffer output = new StringBuffer (); 108 toString(output, obj); 109 return output.toString(); 110 } 111 112 123 public static final void toByteArray(ByteArrayOutputStream output, 124 String str) { 125 if (str == null) { 126 return; 127 } 128 129 output.write(34); 131 int max = str.length(); 132 for (int i = 0; i < max; i ++) { 133 int b = (int) str.charAt(i); 135 if ((b >= 32) && (b <= 126)) { 136 switch (b) { 138 case 34: case 92: output.write(92); break; 142 } 143 output.write(b); 144 } else { 145 output.write(92); switch (b) { 147 case 8: output.write(98); break; 150 case 9: output.write(116); break; 153 case 10: output.write(110); break; 156 case 12: output.write(102); break; 159 case 13: output.write(114); break; 162 default: 163 output.write(117); int b3 = b >> 4; 166 int b4 = b & 0xf; 167 if (b4 < 10) 168 b4 += 48; else 170 b4 += 87; int b2 = b3 >> 4; 172 b3 &= 0xf; 173 if (b3 < 10) 174 b3 += 48; else 176 b3 += 87; int b1 = b2 >> 4; 178 b2 &= 0xf; 179 if (b2 < 10) 180 b2 += 48; else 182 b2 += 87; if (b1 < 10) 184 b1 += 48; else 186 b1 += 87; output.write(b1); 188 output.write(b2); 189 output.write(b3); 190 output.write(b4); 191 break; 192 } 193 } 194 } 195 196 output.write(34); } 198 199 210 public static final void toString(StringBuffer output, String str) { 211 if (str == null) { 212 output.append("null"); 213 return; 214 } 215 216 output.append(toString(str)); 217 } 218 219 230 public static final String toString(String str) { 231 if (str == null) 232 return "null"; 233 234 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 235 toByteArray(buffer, str); 236 return buffer.toString(); 237 } 238 239 248 public static int listMax = 10; 249 250 259 public static int listBorder = 3; 260 261 269 public static final void toString(StringBuffer output, 270 Object obj, Class type) { 271 if (obj == null) { 272 output.append("null"); 273 return; 274 } 275 276 if (type.isPrimitive()) { 277 if (type == Boolean.TYPE) 278 toString(output, (boolean[]) obj); 279 else if (type == Character.TYPE) 280 toString(output, (char[]) obj); 281 else if (type == Byte.TYPE) 282 toString(output, (byte[]) obj); 283 else if (type == Short.TYPE) 284 toString(output, (short[]) obj); 285 else if (type == Integer.TYPE) 286 toString(output, (int[]) obj); 287 else if (type == Long.TYPE) 288 toString(output, (long[]) obj); 289 else if (type == Float.TYPE) 290 toString(output, (float[]) obj); 291 else if (type == Double.TYPE) 292 toString(output, (double[]) obj); 293 return; 294 } 295 296 Object [] tab = (Object []) obj; 297 298 output.append("("); 299 int size = tab.length; 300 output.append(size); 301 if (listMax == -1 || size <= listMax) { 302 for (int i = 0; i < size; i ++) { 303 output.append(","); 304 toString(output, tab[i]); 305 } 306 } else { 307 int border = size / 2; 308 if (listBorder < border) 309 border = listBorder; 310 for (int i = 0; i < border; i ++) { 311 output.append(","); 312 toString(output, tab[i]); 313 } 314 output.append(",..."); 315 for (int i = border; i > 0; i --) { 316 output.append(","); 317 toString(output, tab[size - i]); 318 } 319 } 320 output.append(")"); 321 } 322 323 331 public static final void toStringArray(StringBuffer output, Object tab) { 332 if (tab == null) { 333 output.append("null"); 334 return; 335 } 336 337 Class type = tab.getClass(); 338 if (! type.isArray()) { 339 toString(output, tab); 340 return; 341 } 342 343 toString(output, tab, type.getComponentType()); 344 } 345 346 353 public static final String toStringArray(Object tab) { 354 if (tab == null) 355 return "null"; 356 357 Class type = tab.getClass(); 358 if (! type.isArray()) 359 return toString(tab); 360 361 StringBuffer output = new StringBuffer (); 362 toString(output, tab, type.getComponentType()); 363 return output.toString(); 364 } 365 366 373 public static final void toString(StringBuffer output, boolean[] tab) { 374 if (tab == null) { 375 output.append("null"); 376 return; 377 } 378 379 output.append("("); 380 int size = tab.length; 381 output.append(size); 382 if (listMax == -1 || size <= listMax) { 383 for (int i = 0; i < size; i ++) { 384 output.append(","); 385 output.append(tab[i]); 386 } 387 } else { 388 int border = size / 2; 389 if (listBorder < border) 390 border = listBorder; 391 for (int i = 0; i < border; i ++) { 392 output.append(","); 393 output.append(tab[i]); 394 } 395 output.append(",..."); 396 for (int i = border; i > 0; i --) { 397 output.append(","); 398 output.append(tab[size - i]); 399 } 400 } 401 output.append(")"); 402 } 403 404 411 public static final void toString(StringBuffer output, byte[] tab) { 412 if (tab == null) { 413 output.append("null"); 414 return; 415 } 416 417 output.append("("); 418 int size = tab.length; 419 output.append(size); 420 if (listMax == -1 || size <= listMax) { 421 for (int i = 0; i < size; i ++) { 422 output.append(","); 423 output.append(tab[i]); 424 } 425 } else { 426 int border = size / 2; 427 if (listBorder < border) 428 border = listBorder; 429 for (int i = 0; i < border; i ++) { 430 output.append(","); 431 output.append(tab[i]); 432 } 433 output.append(",..."); 434 for (int i = border; i > 0; i --) { 435 output.append(","); 436 output.append(tab[size - i]); 437 } 438 } 439 output.append(")"); 440 } 441 442 449 public static final void toString(StringBuffer output, char[] tab) { 450 if (tab == null) { 451 output.append("null"); 452 return; 453 } 454 455 output.append("("); 456 int size = tab.length; 457 output.append(size); 458 if (listMax == -1 || size <= listMax) { 459 for (int i = 0; i < size; i ++) { 460 output.append(","); 461 output.append(tab[i]); 462 } 463 } else { 464 int border = size / 2; 465 if (listBorder < border) 466 border = listBorder; 467 for (int i = 0; i < border; i ++) { 468 output.append(","); 469 output.append(tab[i]); 470 } 471 output.append(",..."); 472 for (int i = border; i > 0; i --) { 473 output.append(","); 474 output.append(tab[size - i]); 475 } 476 } 477 output.append(")"); 478 } 479 480 487 public static final void toString(StringBuffer output, short[] tab) { 488 if (tab == null) { 489 output.append("null"); 490 return; 491 } 492 493 output.append("("); 494 int size = tab.length; 495 output.append(size); 496 if (listMax == -1 || size <= listMax) { 497 for (int i = 0; i < size; i ++) { 498 output.append(","); 499 output.append(tab[i]); 500 } 501 } else { 502 int border = size / 2; 503 if (listBorder < border) 504 border = listBorder; 505 for (int i = 0; i < border; i ++) { 506 output.append(","); 507 output.append(tab[i]); 508 } 509 output.append(",..."); 510 for (int i = border; i > 0; i --) { 511 output.append(","); 512 output.append(tab[size - i]); 513 } 514 } 515 output.append(")"); 516 } 517 518 525 public static final void toString(StringBuffer output, int[] tab) { 526 if (tab == null) { 527 output.append("null"); 528 return; 529 } 530 531 output.append("("); 532 int size = tab.length; 533 output.append(size); 534 if (listMax == -1 || size <= listMax) { 535 for (int i = 0; i < size; i ++) { 536 output.append(","); 537 output.append(tab[i]); 538 } 539 } else { 540 int border = size / 2; 541 if (listBorder < border) 542 border = listBorder; 543 for (int i = 0; i < border; i ++) { 544 output.append(","); 545 output.append(tab[i]); 546 } 547 output.append(",..."); 548 for (int i = border; i > 0; i --) { 549 output.append(","); 550 output.append(tab[size - i]); 551 } 552 } 553 output.append(")"); 554 } 555 556 563 public static final void toString(StringBuffer output, long[] tab) { 564 if (tab == null) { 565 output.append("null"); 566 return; 567 } 568 569 output.append("("); 570 int size = tab.length; 571 output.append(size); 572 if (listMax == -1 || size <= listMax) { 573 for (int i = 0; i < size; i ++) { 574 output.append(","); 575 output.append(tab[i]); 576 } 577 } else { 578 int border = size / 2; 579 if (listBorder < border) 580 border = listBorder; 581 for (int i = 0; i < border; i ++) { 582 output.append(","); 583 output.append(tab[i]); 584 } 585 output.append(",..."); 586 for (int i = border; i > 0; i --) { 587 output.append(","); 588 output.append(tab[size - i]); 589 } 590 } 591 output.append(")"); 592 } 593 594 601 public static final void toString(StringBuffer output, float[] tab) { 602 if (tab == null) { 603 output.append("null"); 604 return; 605 } 606 607 output.append("("); 608 int size = tab.length; 609 output.append(size); 610 if (listMax == -1 || size <= listMax) { 611 for (int i = 0; i < size; i ++) { 612 output.append(","); 613 output.append(tab[i]); 614 } 615 } else { 616 int border = size / 2; 617 if (listBorder < border) 618 border = listBorder; 619 for (int i = 0; i < border; i ++) { 620 output.append(","); 621 output.append(tab[i]); 622 } 623 output.append(",..."); 624 for (int i = border; i > 0; i --) { 625 output.append(","); 626 output.append(tab[size - i]); 627 } 628 } 629 output.append(")"); 630 } 631 632 639 public static final void toString(StringBuffer output, double[] tab) { 640 if (tab == null) { 641 output.append("null"); 642 return; 643 } 644 645 output.append("("); 646 int size = tab.length; 647 output.append(size); 648 if (listMax == -1 || size <= listMax) { 649 for (int i = 0; i < size; i ++) { 650 output.append(","); 651 output.append(tab[i]); 652 } 653 } else { 654 int border = size / 2; 655 if (listBorder < border) 656 border = listBorder; 657 for (int i = 0; i < border; i ++) { 658 output.append(","); 659 output.append(tab[i]); 660 } 661 output.append(",..."); 662 for (int i = border; i > 0; i --) { 663 output.append(","); 664 output.append(tab[size - i]); 665 } 666 } 667 output.append(")"); 668 } 669 670 678 public static final void toString(StringBuffer output, List list) { 679 if (list == null) { 680 output.append("null"); 681 return; 682 } 683 684 output.append("("); 685 int size = list.size(); 686 output.append(size); 687 if (listMax == -1 || size <= listMax || size <= (listBorder*2)) { 688 for (Iterator it = list.iterator(); it.hasNext();) { 689 output.append(","); 690 toString(output, it.next()); 691 } 692 } else { 693 int border = size / 2; 694 if (listBorder < border) 695 border = listBorder; 696 for (int i = 0; i < border; i ++) { 697 output.append(","); 698 toString(output, list.get(i)); 699 } 700 output.append(",..."); 701 for (int i = border; i > 0; i --) { 702 output.append(","); 703 toString(output, list.get(size - i)); 704 } 705 } 706 output.append(")"); 707 } 708 709 716 public static final String toString(List list) { 717 if (list == null) 718 return "null"; 719 720 StringBuffer output = new StringBuffer (); 721 toString(output, list); 722 return output.toString(); 723 } 724 725 733 public static final void toString(StringBuffer output, Collection set) { 734 if (set == null) { 735 output.append("null"); 736 return; 737 } 738 739 output.append("("); 740 int size = set.size(); 741 output.append(size); 742 if (listMax != -1 && size > listMax) 743 size = listBorder; 744 for (Iterator it = set.iterator(); size > 0; size --) { 745 output.append(","); 746 toString(output, it.next()); 747 } 748 output.append(")"); 749 } 750 751 758 public static final String toString(Collection set) { 759 if (set == null) 760 return "null"; 761 762 StringBuffer output = new StringBuffer (); 763 toString(output, set); 764 return output.toString(); 765 } 766 767 775 public static final void toString(StringBuffer output, Map map) { 776 if (map == null) { 777 output.append("null"); 778 return; 779 } 780 781 toString(output, map.values()); 782 } 783 784 791 public static final String toString(Map map) { 792 if (map == null) 793 return "null"; 794 795 StringBuffer output = new StringBuffer (); 796 toString(output, map); 797 return output.toString(); 798 } 799 800 806 public static final void toString(StringBuffer output, Map.Entry entry) { 807 if (entry == null) { 808 output.append("null"); 809 return; 810 } 811 output.append("("); 812 output.append(entry.getKey()); 813 output.append(","); 814 output.append(entry.getValue()); 815 output.append(")"); 816 } 817 818 } 819 | Popular Tags |