1 16 package org.apache.commons.lang.builder; 17 18 import org.apache.commons.lang.BooleanUtils; 19 import org.apache.commons.lang.ObjectUtils; 20 21 92 public class ToStringBuilder { 93 94 97 private static ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE; 98 99 101 113 public static ToStringStyle getDefaultStyle() { 114 return defaultStyle; 115 } 116 117 122 public static String reflectionToString(Object object) { 123 return ReflectionToStringBuilder.toString(object); 124 } 125 126 131 public static String reflectionToString(Object object, ToStringStyle style) { 132 return ReflectionToStringBuilder.toString(object, style); 133 } 134 135 140 public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients) { 141 return ReflectionToStringBuilder.toString(object, style, outputTransients, false, null); 142 } 143 144 150 public static String reflectionToString( 151 Object object, 152 ToStringStyle style, 153 boolean outputTransients, 154 Class reflectUpToClass) { 155 return ReflectionToStringBuilder.toString(object, style, outputTransients, false, reflectUpToClass); 156 } 157 158 164 public static void setDefaultStyle(ToStringStyle style) { 165 if (style == null) { 166 throw new IllegalArgumentException ("The style must not be null"); 167 } 168 defaultStyle = style; 169 } 170 171 174 private final StringBuffer buffer; 175 176 179 private final Object object; 180 181 184 private final ToStringStyle style; 185 186 196 public ToStringBuilder(Object object) { 197 this(object, getDefaultStyle(), null); 198 } 199 200 212 public ToStringBuilder(Object object, ToStringStyle style) { 213 this(object, style, null); 214 } 215 216 229 public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) { 230 if (style == null) { 231 style = getDefaultStyle(); 232 } 233 if (buffer == null) { 234 buffer = new StringBuffer (512); 235 } 236 this.buffer = buffer; 237 this.style = style; 238 this.object = object; 239 240 style.appendStart(buffer, object); 241 } 242 243 245 252 public ToStringBuilder append(boolean value) { 253 style.append(buffer, null, value); 254 return this; 255 } 256 257 259 266 public ToStringBuilder append(boolean[] array) { 267 style.append(buffer, null, array, null); 268 return this; 269 } 270 271 273 280 public ToStringBuilder append(byte value) { 281 style.append(buffer, null, value); 282 return this; 283 } 284 285 287 294 public ToStringBuilder append(byte[] array) { 295 style.append(buffer, null, array, null); 296 return this; 297 } 298 299 301 308 public ToStringBuilder append(char value) { 309 style.append(buffer, null, value); 310 return this; 311 } 312 313 315 322 public ToStringBuilder append(char[] array) { 323 style.append(buffer, null, array, null); 324 return this; 325 } 326 327 329 336 public ToStringBuilder append(double value) { 337 style.append(buffer, null, value); 338 return this; 339 } 340 341 343 350 public ToStringBuilder append(double[] array) { 351 style.append(buffer, null, array, null); 352 return this; 353 } 354 355 357 364 public ToStringBuilder append(float value) { 365 style.append(buffer, null, value); 366 return this; 367 } 368 369 371 378 public ToStringBuilder append(float[] array) { 379 style.append(buffer, null, array, null); 380 return this; 381 } 382 383 385 392 public ToStringBuilder append(int value) { 393 style.append(buffer, null, value); 394 return this; 395 } 396 397 399 406 public ToStringBuilder append(int[] array) { 407 style.append(buffer, null, array, null); 408 return this; 409 } 410 411 413 420 public ToStringBuilder append(long value) { 421 style.append(buffer, null, value); 422 return this; 423 } 424 425 427 434 public ToStringBuilder append(long[] array) { 435 style.append(buffer, null, array, null); 436 return this; 437 } 438 439 441 448 public ToStringBuilder append(Object object) { 449 style.append(buffer, null, object, null); 450 return this; 451 } 452 453 455 462 public ToStringBuilder append(Object [] array) { 463 style.append(buffer, null, array, null); 464 return this; 465 } 466 467 469 476 public ToStringBuilder append(short value) { 477 style.append(buffer, null, value); 478 return this; 479 } 480 481 483 490 public ToStringBuilder append(short[] array) { 491 style.append(buffer, null, array, null); 492 return this; 493 } 494 495 503 public ToStringBuilder append(String fieldName, boolean value) { 504 style.append(buffer, fieldName, value); 505 return this; 506 } 507 508 516 public ToStringBuilder append(String fieldName, boolean[] array) { 517 style.append(buffer, fieldName, array, null); 518 return this; 519 } 520 521 536 public ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail) { 537 style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); 538 return this; 539 } 540 541 549 public ToStringBuilder append(String fieldName, byte value) { 550 style.append(buffer, fieldName, value); 551 return this; 552 } 553 554 561 public ToStringBuilder append(String fieldName, byte[] array) { 562 style.append(buffer, fieldName, array, null); 563 return this; 564 } 565 566 581 public ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail) { 582 style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); 583 return this; 584 } 585 586 594 public ToStringBuilder append(String fieldName, char value) { 595 style.append(buffer, fieldName, value); 596 return this; 597 } 598 599 607 public ToStringBuilder append(String fieldName, char[] array) { 608 style.append(buffer, fieldName, array, null); 609 return this; 610 } 611 612 627 public ToStringBuilder append(String fieldName, char[] array, boolean fullDetail) { 628 style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); 629 return this; 630 } 631 632 640 public ToStringBuilder append(String fieldName, double value) { 641 style.append(buffer, fieldName, value); 642 return this; 643 } 644 645 653 public ToStringBuilder append(String fieldName, double[] array) { 654 style.append(buffer, fieldName, array, null); 655 return this; 656 } 657 658 673 public ToStringBuilder append(String fieldName, double[] array, boolean fullDetail) { 674 style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); 675 return this; 676 } 677 678 686 public ToStringBuilder append(String fieldName, float value) { 687 style.append(buffer, fieldName, value); 688 return this; 689 } 690 691 699 public ToStringBuilder append(String fieldName, float[] array) { 700 style.append(buffer, fieldName, array, null); 701 return this; 702 } 703 704 719 public ToStringBuilder append(String fieldName, float[] array, boolean fullDetail) { 720 style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); 721 return this; 722 } 723 724 732 public ToStringBuilder append(String fieldName, int value) { 733 style.append(buffer, fieldName, value); 734 return this; 735 } 736 737 745 public ToStringBuilder append(String fieldName, int[] array) { 746 style.append(buffer, fieldName, array, null); 747 return this; 748 } 749 750 765 public ToStringBuilder append(String fieldName, int[] array, boolean fullDetail) { 766 style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); 767 return this; 768 } 769 770 778 public ToStringBuilder append(String fieldName, long value) { 779 style.append(buffer, fieldName, value); 780 return this; 781 } 782 783 791 public ToStringBuilder append(String fieldName, long[] array) { 792 style.append(buffer, fieldName, array, null); 793 return this; 794 } 795 796 811 public ToStringBuilder append(String fieldName, long[] array, boolean fullDetail) { 812 style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); 813 return this; 814 } 815 816 824 public ToStringBuilder append(String fieldName, Object object) { 825 style.append(buffer, fieldName, object, null); 826 return this; 827 } 828 829 839 public ToStringBuilder append(String fieldName, Object object, boolean fullDetail) { 840 style.append(buffer, fieldName, object, BooleanUtils.toBooleanObject(fullDetail)); 841 return this; 842 } 843 844 852 public ToStringBuilder append(String fieldName, Object [] array) { 853 style.append(buffer, fieldName, array, null); 854 return this; 855 } 856 857 872 public ToStringBuilder append(String fieldName, Object [] array, boolean fullDetail) { 873 style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); 874 return this; 875 } 876 877 885 public ToStringBuilder append(String fieldName, short value) { 886 style.append(buffer, fieldName, value); 887 return this; 888 } 889 890 898 public ToStringBuilder append(String fieldName, short[] array) { 899 style.append(buffer, fieldName, array, null); 900 return this; 901 } 902 903 918 public ToStringBuilder append(String fieldName, short[] array, boolean fullDetail) { 919 style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); 920 return this; 921 } 922 923 932 public ToStringBuilder appendAsObjectToString(Object object) { 933 ObjectUtils.appendIdentityToString(this.getStringBuffer(), object); 934 return this; 935 } 936 937 939 951 public ToStringBuilder appendSuper(String superToString) { 952 if (superToString != null) { 953 style.appendSuper(buffer, superToString); 954 } 955 return this; 956 } 957 958 985 public ToStringBuilder appendToString(String toString) { 986 if (toString != null) { 987 style.appendToString(buffer, toString); 988 } 989 return this; 990 } 991 992 998 public Object getObject() { 999 return object; 1000 } 1001 1002 1007 public StringBuffer getStringBuffer() { 1008 return buffer; 1009 } 1010 1011 1013 1019 public ToStringStyle getStyle() { 1020 return style; 1021 } 1022 1023 1033 public String toString() { 1034 if (this.getObject() == null) { 1035 this.getStringBuffer().append(this.getStyle().getNullText()); 1036 } else { 1037 style.appendEnd(this.getStringBuffer(), this.getObject()); 1038 } 1039 return this.getStringBuffer().toString(); 1040 } 1041 1042} 1043 | Popular Tags |