1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.builder;
18
19 import java.io.Serializable;
20 import java.lang.reflect.Array;
21 import java.util.Collection;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Objects;
25 import java.util.WeakHashMap;
26
27 import org.apache.commons.lang3.ClassUtils;
28 import org.apache.commons.lang3.ObjectUtils;
29 import org.apache.commons.lang3.StringEscapeUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.commons.lang3.Strings;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 @SuppressWarnings("deprecation")
70 public abstract class ToStringStyle implements Serializable {
71
72
73
74
75
76
77
78 private static final class DefaultToStringStyle extends ToStringStyle {
79
80
81
82
83
84
85 private static final long serialVersionUID = 1L;
86
87
88
89
90
91
92 DefaultToStringStyle() {
93 }
94
95
96
97
98
99
100 private Object readResolve() {
101 return DEFAULT_STYLE;
102 }
103
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117 private static final class JsonToStringStyle extends ToStringStyle {
118
119 private static final long serialVersionUID = 1L;
120
121 private static final String FIELD_NAME_QUOTE = "\"";
122
123
124
125
126
127
128
129
130 JsonToStringStyle() {
131 setUseClassName(false);
132 setUseIdentityHashCode(false);
133
134 setContentStart("{");
135 setContentEnd("}");
136
137 setArrayStart("[");
138 setArrayEnd("]");
139
140 setFieldSeparator(",");
141 setFieldNameValueSeparator(":");
142
143 setNullText("null");
144
145 setSummaryObjectStartText("\"<");
146 setSummaryObjectEndText(">\"");
147
148 setSizeStartText("\"<size=");
149 setSizeEndText(">\"");
150 }
151
152 @Override
153 public void append(final StringBuffer buffer, final String fieldName, final boolean[] array, final Boolean fullDetail) {
154 checkAppendInput(fieldName, fullDetail);
155 super.append(buffer, fieldName, array, fullDetail);
156 }
157
158 @Override
159 public void append(final StringBuffer buffer, final String fieldName, final byte[] array, final Boolean fullDetail) {
160 checkAppendInput(fieldName, fullDetail);
161 super.append(buffer, fieldName, array, fullDetail);
162 }
163
164 @Override
165 public void append(final StringBuffer buffer, final String fieldName, final char[] array, final Boolean fullDetail) {
166 checkAppendInput(fieldName, fullDetail);
167 super.append(buffer, fieldName, array, fullDetail);
168 }
169
170 @Override
171 public void append(final StringBuffer buffer, final String fieldName, final double[] array, final Boolean fullDetail) {
172 checkAppendInput(fieldName, fullDetail);
173 super.append(buffer, fieldName, array, fullDetail);
174 }
175
176 @Override
177 public void append(final StringBuffer buffer, final String fieldName, final float[] array, final Boolean fullDetail) {
178 checkAppendInput(fieldName, fullDetail);
179 super.append(buffer, fieldName, array, fullDetail);
180 }
181
182 @Override
183 public void append(final StringBuffer buffer, final String fieldName, final int[] array, final Boolean fullDetail) {
184 checkAppendInput(fieldName, fullDetail);
185 super.append(buffer, fieldName, array, fullDetail);
186 }
187
188 @Override
189 public void append(final StringBuffer buffer, final String fieldName, final long[] array, final Boolean fullDetail) {
190 checkAppendInput(fieldName, fullDetail);
191 super.append(buffer, fieldName, array, fullDetail);
192 }
193
194 @Override
195 public void append(final StringBuffer buffer, final String fieldName, final Object value, final Boolean fullDetail) {
196 checkAppendInput(fieldName, fullDetail);
197 super.append(buffer, fieldName, value, fullDetail);
198 }
199
200 @Override
201 public void append(final StringBuffer buffer, final String fieldName, final Object[] array, final Boolean fullDetail) {
202 checkAppendInput(fieldName, fullDetail);
203 super.append(buffer, fieldName, array, fullDetail);
204 }
205
206 @Override
207 public void append(final StringBuffer buffer, final String fieldName, final short[] array, final Boolean fullDetail) {
208 checkAppendInput(fieldName, fullDetail);
209 super.append(buffer, fieldName, array, fullDetail);
210 }
211
212 @Override
213 protected void appendDetail(final StringBuffer buffer, final String fieldName, final char value) {
214 appendValueAsString(buffer, String.valueOf(value));
215 }
216
217 @Override
218 protected void appendDetail(final StringBuffer buffer, final String fieldName, final Collection<?> coll) {
219 if (coll != null && !coll.isEmpty()) {
220 buffer.append(getArrayStart());
221 int i = 0;
222 for (final Object item : coll) {
223 appendDetail(buffer, fieldName, i++, item);
224 }
225 buffer.append(getArrayEnd());
226 return;
227 }
228 buffer.append(coll);
229 }
230
231 @Override
232 protected void appendDetail(final StringBuffer buffer, final String fieldName, final Map<?, ?> map) {
233 if (map != null && !map.isEmpty()) {
234 buffer.append(getContentStart());
235
236 boolean firstItem = true;
237 for (final Entry<?, ?> entry : map.entrySet()) {
238 final String keyStr = Objects.toString(entry.getKey(), null);
239 if (keyStr != null) {
240 if (firstItem) {
241 firstItem = false;
242 } else {
243 appendFieldEnd(buffer, keyStr);
244 }
245 appendFieldStart(buffer, keyStr);
246 final Object value = entry.getValue();
247 if (value == null) {
248 appendNullText(buffer, keyStr);
249 } else {
250 appendInternal(buffer, keyStr, value, true);
251 }
252 }
253 }
254
255 buffer.append(getContentEnd());
256 return;
257 }
258
259 buffer.append(map);
260 }
261
262 @Override
263 protected void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) {
264
265 if (value == null) {
266 appendNullText(buffer, fieldName);
267 return;
268 }
269
270 if (value instanceof String || value instanceof Character) {
271 appendValueAsString(buffer, value.toString());
272 return;
273 }
274
275 if (value instanceof Number || value instanceof Boolean) {
276 buffer.append(value);
277 return;
278 }
279
280 final String valueAsString = value.toString();
281 if (isJsonObject(valueAsString) || isJsonArray(valueAsString)) {
282 buffer.append(value);
283 return;
284 }
285
286 appendDetail(buffer, fieldName, valueAsString);
287 }
288
289 @Override
290 protected void appendFieldStart(final StringBuffer buffer, final String fieldName) {
291
292 checkFieldName(fieldName);
293
294 super.appendFieldStart(buffer, FIELD_NAME_QUOTE + StringEscapeUtils.escapeJson(fieldName)
295 + FIELD_NAME_QUOTE);
296 }
297
298
299
300
301
302
303
304 private void appendValueAsString(final StringBuffer buffer, final String value) {
305 buffer.append('"').append(StringEscapeUtils.escapeJson(value)).append('"');
306 }
307
308 private void checkAppendInput(final String fieldName, final Boolean fullDetail) {
309 checkFieldName(fieldName);
310 checkIsFullDetail(fullDetail);
311 }
312
313 private void checkFieldName(final String fieldName) {
314 if (fieldName == null) {
315 throw new UnsupportedOperationException("Field names are mandatory when using JsonToStringStyle");
316 }
317 }
318
319 private void checkIsFullDetail(final Boolean fullDetail) {
320 if (!isFullDetail(fullDetail)) {
321 throw new UnsupportedOperationException("FullDetail must be true when using JsonToStringStyle");
322 }
323 }
324
325 private boolean isJsonArray(final String valueAsString) {
326 return valueAsString.startsWith(getArrayStart())
327 && valueAsString.endsWith(getArrayEnd());
328 }
329
330 private boolean isJsonObject(final String valueAsString) {
331 return valueAsString.startsWith(getContentStart())
332 && valueAsString.endsWith(getContentEnd());
333 }
334
335
336
337
338
339
340 private Object readResolve() {
341 return JSON_STYLE;
342 }
343
344 }
345
346
347
348
349
350
351
352 private static final class MultiLineToStringStyle extends ToStringStyle {
353
354 private static final long serialVersionUID = 1L;
355
356
357
358
359
360
361 MultiLineToStringStyle() {
362 setContentStart("[");
363 setFieldSeparator(System.lineSeparator() + " ");
364 setFieldSeparatorAtStart(true);
365 setContentEnd(System.lineSeparator() + "]");
366 }
367
368
369
370
371
372
373 private Object readResolve() {
374 return MULTI_LINE_STYLE;
375 }
376
377 }
378
379
380
381
382
383
384
385
386 private static final class NoClassNameToStringStyle extends ToStringStyle {
387
388 private static final long serialVersionUID = 1L;
389
390
391
392
393
394
395 NoClassNameToStringStyle() {
396 setUseClassName(false);
397 setUseIdentityHashCode(false);
398 }
399
400
401
402
403
404
405 private Object readResolve() {
406 return NO_CLASS_NAME_STYLE;
407 }
408
409 }
410
411
412
413
414
415
416
417
418 private static final class NoFieldNameToStringStyle extends ToStringStyle {
419
420 private static final long serialVersionUID = 1L;
421
422
423
424
425
426
427 NoFieldNameToStringStyle() {
428 setUseFieldNames(false);
429 }
430
431
432
433
434
435
436 private Object readResolve() {
437 return NO_FIELD_NAMES_STYLE;
438 }
439
440 }
441
442
443
444
445
446
447
448
449 private static final class ShortPrefixToStringStyle extends ToStringStyle {
450
451 private static final long serialVersionUID = 1L;
452
453
454
455
456
457
458 ShortPrefixToStringStyle() {
459 setUseShortClassName(true);
460 setUseIdentityHashCode(false);
461 }
462
463
464
465
466
467 private Object readResolve() {
468 return SHORT_PREFIX_STYLE;
469 }
470
471 }
472
473
474
475
476
477
478
479
480 private static final class SimpleToStringStyle extends ToStringStyle {
481
482 private static final long serialVersionUID = 1L;
483
484
485
486
487
488
489 SimpleToStringStyle() {
490 setUseClassName(false);
491 setUseIdentityHashCode(false);
492 setUseFieldNames(false);
493 setContentStart(StringUtils.EMPTY);
494 setContentEnd(StringUtils.EMPTY);
495 }
496
497
498
499
500
501 private Object readResolve() {
502 return SIMPLE_STYLE;
503 }
504
505 }
506
507
508
509
510 private static final long serialVersionUID = -2587890625525655916L;
511
512
513
514
515
516
517
518
519
520 public static final ToStringStyle DEFAULT_STYLE = new DefaultToStringStyle();
521
522
523
524
525
526
527
528
529
530
531
532
533
534 public static final ToStringStyle MULTI_LINE_STYLE = new MultiLineToStringStyle();
535
536
537
538
539
540
541
542
543
544
545 public static final ToStringStyle NO_FIELD_NAMES_STYLE = new NoFieldNameToStringStyle();
546
547
548
549
550
551
552
553
554
555
556
557 public static final ToStringStyle SHORT_PREFIX_STYLE = new ShortPrefixToStringStyle();
558
559
560
561
562
563
564
565
566
567 public static final ToStringStyle SIMPLE_STYLE = new SimpleToStringStyle();
568
569
570
571
572
573
574
575
576
577
578
579 public static final ToStringStyle NO_CLASS_NAME_STYLE = new NoClassNameToStringStyle();
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598 public static final ToStringStyle JSON_STYLE = new JsonToStringStyle();
599
600
601
602
603
604 private static final ThreadLocal<WeakHashMap<Object, Object>> REGISTRY = ThreadLocal.withInitial(WeakHashMap::new);
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621 public static Map<Object, Object> getRegistry() {
622 return REGISTRY.get();
623 }
624
625
626
627
628
629
630
631
632
633
634 static boolean isRegistered(final Object value) {
635 return getRegistry().containsKey(value);
636 }
637
638
639
640
641
642
643
644
645 static void register(final Object value) {
646 if (value != null) {
647 getRegistry().put(value, null);
648 }
649 }
650
651
652
653
654
655
656
657
658
659
660
661 static void unregister(final Object value) {
662 if (value != null) {
663 final Map<Object, Object> m = getRegistry();
664 m.remove(value);
665 if (m.isEmpty()) {
666 REGISTRY.remove();
667 }
668 }
669 }
670
671
672
673
674 private boolean useFieldNames = true;
675
676
677
678
679 private boolean useClassName = true;
680
681
682
683
684 private boolean useShortClassName;
685
686
687
688
689 private boolean useIdentityHashCode = true;
690
691
692
693
694 private String contentStart = "[";
695
696
697
698
699 private String contentEnd = "]";
700
701
702
703
704 private String fieldNameValueSeparator = "=";
705
706
707
708
709 private boolean fieldSeparatorAtStart;
710
711
712
713
714 private boolean fieldSeparatorAtEnd;
715
716
717
718
719 private String fieldSeparator = ",";
720
721
722
723
724 private String arrayStart = "{";
725
726
727
728
729 private String arraySeparator = ",";
730
731
732
733
734 private boolean arrayContentDetail = true;
735
736
737
738
739 private String arrayEnd = "}";
740
741
742
743
744
745 private boolean defaultFullDetail = true;
746
747
748
749
750 private String nullText = "<null>";
751
752
753
754
755 private String sizeStartText = "<size=";
756
757
758
759
760 private String sizeEndText = ">";
761
762
763
764
765 private String summaryObjectStartText = "<";
766
767
768
769
770 private String summaryObjectEndText = ">";
771
772
773
774
775 protected ToStringStyle() {
776 }
777
778
779
780
781
782
783
784
785
786 public void append(final StringBuffer buffer, final String fieldName, final boolean value) {
787 appendFieldStart(buffer, fieldName);
788 appendDetail(buffer, fieldName, value);
789 appendFieldEnd(buffer, fieldName);
790 }
791
792
793
794
795
796
797
798
799
800
801
802 public void append(final StringBuffer buffer, final String fieldName, final boolean[] array, final Boolean fullDetail) {
803 appendFieldStart(buffer, fieldName);
804
805 if (array == null) {
806 appendNullText(buffer, fieldName);
807
808 } else if (isFullDetail(fullDetail)) {
809 appendDetail(buffer, fieldName, array);
810
811 } else {
812 appendSummary(buffer, fieldName, array);
813 }
814
815 appendFieldEnd(buffer, fieldName);
816 }
817
818
819
820
821
822
823
824
825
826 public void append(final StringBuffer buffer, final String fieldName, final byte value) {
827 appendFieldStart(buffer, fieldName);
828 appendDetail(buffer, fieldName, value);
829 appendFieldEnd(buffer, fieldName);
830 }
831
832
833
834
835
836
837
838
839
840
841
842 public void append(final StringBuffer buffer, final String fieldName, final byte[] array, final Boolean fullDetail) {
843 appendFieldStart(buffer, fieldName);
844
845 if (array == null) {
846 appendNullText(buffer, fieldName);
847
848 } else if (isFullDetail(fullDetail)) {
849 appendDetail(buffer, fieldName, array);
850
851 } else {
852 appendSummary(buffer, fieldName, array);
853 }
854
855 appendFieldEnd(buffer, fieldName);
856 }
857
858
859
860
861
862
863
864
865
866 public void append(final StringBuffer buffer, final String fieldName, final char value) {
867 appendFieldStart(buffer, fieldName);
868 appendDetail(buffer, fieldName, value);
869 appendFieldEnd(buffer, fieldName);
870 }
871
872
873
874
875
876
877
878
879
880
881
882 public void append(final StringBuffer buffer, final String fieldName, final char[] array, final Boolean fullDetail) {
883 appendFieldStart(buffer, fieldName);
884
885 if (array == null) {
886 appendNullText(buffer, fieldName);
887
888 } else if (isFullDetail(fullDetail)) {
889 appendDetail(buffer, fieldName, array);
890
891 } else {
892 appendSummary(buffer, fieldName, array);
893 }
894
895 appendFieldEnd(buffer, fieldName);
896 }
897
898
899
900
901
902
903
904
905
906 public void append(final StringBuffer buffer, final String fieldName, final double value) {
907 appendFieldStart(buffer, fieldName);
908 appendDetail(buffer, fieldName, value);
909 appendFieldEnd(buffer, fieldName);
910 }
911
912
913
914
915
916
917
918
919
920
921
922 public void append(final StringBuffer buffer, final String fieldName, final double[] array, final Boolean fullDetail) {
923 appendFieldStart(buffer, fieldName);
924
925 if (array == null) {
926 appendNullText(buffer, fieldName);
927
928 } else if (isFullDetail(fullDetail)) {
929 appendDetail(buffer, fieldName, array);
930
931 } else {
932 appendSummary(buffer, fieldName, array);
933 }
934
935 appendFieldEnd(buffer, fieldName);
936 }
937
938
939
940
941
942
943
944
945
946 public void append(final StringBuffer buffer, final String fieldName, final float value) {
947 appendFieldStart(buffer, fieldName);
948 appendDetail(buffer, fieldName, value);
949 appendFieldEnd(buffer, fieldName);
950 }
951
952
953
954
955
956
957
958
959
960
961
962 public void append(final StringBuffer buffer, final String fieldName, final float[] array, final Boolean fullDetail) {
963 appendFieldStart(buffer, fieldName);
964
965 if (array == null) {
966 appendNullText(buffer, fieldName);
967
968 } else if (isFullDetail(fullDetail)) {
969 appendDetail(buffer, fieldName, array);
970
971 } else {
972 appendSummary(buffer, fieldName, array);
973 }
974
975 appendFieldEnd(buffer, fieldName);
976 }
977
978
979
980
981
982
983
984
985
986 public void append(final StringBuffer buffer, final String fieldName, final int value) {
987 appendFieldStart(buffer, fieldName);
988 appendDetail(buffer, fieldName, value);
989 appendFieldEnd(buffer, fieldName);
990 }
991
992
993
994
995
996
997
998
999
1000
1001
1002 public void append(final StringBuffer buffer, final String fieldName, final int[] array, final Boolean fullDetail) {
1003 appendFieldStart(buffer, fieldName);
1004
1005 if (array == null) {
1006 appendNullText(buffer, fieldName);
1007
1008 } else if (isFullDetail(fullDetail)) {
1009 appendDetail(buffer, fieldName, array);
1010
1011 } else {
1012 appendSummary(buffer, fieldName, array);
1013 }
1014
1015 appendFieldEnd(buffer, fieldName);
1016 }
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026 public void append(final StringBuffer buffer, final String fieldName, final long value) {
1027 appendFieldStart(buffer, fieldName);
1028 appendDetail(buffer, fieldName, value);
1029 appendFieldEnd(buffer, fieldName);
1030 }
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042 public void append(final StringBuffer buffer, final String fieldName, final long[] array, final Boolean fullDetail) {
1043 appendFieldStart(buffer, fieldName);
1044
1045 if (array == null) {
1046 appendNullText(buffer, fieldName);
1047
1048 } else if (isFullDetail(fullDetail)) {
1049 appendDetail(buffer, fieldName, array);
1050
1051 } else {
1052 appendSummary(buffer, fieldName, array);
1053 }
1054
1055 appendFieldEnd(buffer, fieldName);
1056 }
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069 public void append(final StringBuffer buffer, final String fieldName, final Object value, final Boolean fullDetail) {
1070 appendFieldStart(buffer, fieldName);
1071
1072 if (value == null) {
1073 appendNullText(buffer, fieldName);
1074
1075 } else {
1076 appendInternal(buffer, fieldName, value, isFullDetail(fullDetail));
1077 }
1078
1079 appendFieldEnd(buffer, fieldName);
1080 }
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092 public void append(final StringBuffer buffer, final String fieldName, final Object[] array, final Boolean fullDetail) {
1093 appendFieldStart(buffer, fieldName);
1094
1095 if (array == null) {
1096 appendNullText(buffer, fieldName);
1097
1098 } else if (isFullDetail(fullDetail)) {
1099 appendDetail(buffer, fieldName, array);
1100
1101 } else {
1102 appendSummary(buffer, fieldName, array);
1103 }
1104
1105 appendFieldEnd(buffer, fieldName);
1106 }
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116 public void append(final StringBuffer buffer, final String fieldName, final short value) {
1117 appendFieldStart(buffer, fieldName);
1118 appendDetail(buffer, fieldName, value);
1119 appendFieldEnd(buffer, fieldName);
1120 }
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132 public void append(final StringBuffer buffer, final String fieldName, final short[] array, final Boolean fullDetail) {
1133 appendFieldStart(buffer, fieldName);
1134
1135 if (array == null) {
1136 appendNullText(buffer, fieldName);
1137
1138 } else if (isFullDetail(fullDetail)) {
1139 appendDetail(buffer, fieldName, array);
1140
1141 } else {
1142 appendSummary(buffer, fieldName, array);
1143 }
1144
1145 appendFieldEnd(buffer, fieldName);
1146 }
1147
1148
1149
1150
1151
1152
1153
1154 protected void appendClassName(final StringBuffer buffer, final Object object) {
1155 if (useClassName && object != null) {
1156 register(object);
1157 if (useShortClassName) {
1158 buffer.append(getShortClassName(object.getClass()));
1159 } else {
1160 buffer.append(object.getClass().getName());
1161 }
1162 }
1163 }
1164
1165
1166
1167
1168
1169
1170 protected void appendContentEnd(final StringBuffer buffer) {
1171 buffer.append(contentEnd);
1172 }
1173
1174
1175
1176
1177
1178
1179 protected void appendContentStart(final StringBuffer buffer) {
1180 buffer.append(contentStart);
1181 }
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195 protected void appendCyclicObject(final StringBuffer buffer, final String fieldName, final Object value) {
1196 ObjectUtils.identityToString(buffer, value);
1197 }
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207 protected void appendDetail(final StringBuffer buffer, final String fieldName, final boolean value) {
1208 buffer.append(value);
1209 }
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220 protected void appendDetail(final StringBuffer buffer, final String fieldName, final boolean[] array) {
1221 buffer.append(arrayStart);
1222 for (int i = 0; i < array.length; i++) {
1223 if (i > 0) {
1224 buffer.append(arraySeparator);
1225 }
1226 appendDetail(buffer, fieldName, array[i]);
1227 }
1228 buffer.append(arrayEnd);
1229 }
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239 protected void appendDetail(final StringBuffer buffer, final String fieldName, final byte value) {
1240 buffer.append(value);
1241 }
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252 protected void appendDetail(final StringBuffer buffer, final String fieldName, final byte[] array) {
1253 buffer.append(arrayStart);
1254 for (int i = 0; i < array.length; i++) {
1255 if (i > 0) {
1256 buffer.append(arraySeparator);
1257 }
1258 appendDetail(buffer, fieldName, array[i]);
1259 }
1260 buffer.append(arrayEnd);
1261 }
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271 protected void appendDetail(final StringBuffer buffer, final String fieldName, final char value) {
1272 buffer.append(value);
1273 }
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284 protected void appendDetail(final StringBuffer buffer, final String fieldName, final char[] array) {
1285 buffer.append(arrayStart);
1286 for (int i = 0; i < array.length; i++) {
1287 if (i > 0) {
1288 buffer.append(arraySeparator);
1289 }
1290 appendDetail(buffer, fieldName, array[i]);
1291 }
1292 buffer.append(arrayEnd);
1293 }
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303 protected void appendDetail(final StringBuffer buffer, final String fieldName, final Collection<?> coll) {
1304 buffer.append(coll);
1305 }
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315 protected void appendDetail(final StringBuffer buffer, final String fieldName, final double value) {
1316 buffer.append(value);
1317 }
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328 protected void appendDetail(final StringBuffer buffer, final String fieldName, final double[] array) {
1329 buffer.append(arrayStart);
1330 for (int i = 0; i < array.length; i++) {
1331 if (i > 0) {
1332 buffer.append(arraySeparator);
1333 }
1334 appendDetail(buffer, fieldName, array[i]);
1335 }
1336 buffer.append(arrayEnd);
1337 }
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347 protected void appendDetail(final StringBuffer buffer, final String fieldName, final float value) {
1348 buffer.append(value);
1349 }
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360 protected void appendDetail(final StringBuffer buffer, final String fieldName, final float[] array) {
1361 buffer.append(arrayStart);
1362 for (int i = 0; i < array.length; i++) {
1363 if (i > 0) {
1364 buffer.append(arraySeparator);
1365 }
1366 appendDetail(buffer, fieldName, array[i]);
1367 }
1368 buffer.append(arrayEnd);
1369 }
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379 protected void appendDetail(final StringBuffer buffer, final String fieldName, final int value) {
1380 buffer.append(value);
1381 }
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393 protected void appendDetail(final StringBuffer buffer, final String fieldName, final int i, final Object item) {
1394 if (i > 0) {
1395 buffer.append(arraySeparator);
1396 }
1397 if (item == null) {
1398 appendNullText(buffer, fieldName);
1399 } else {
1400 appendInternal(buffer, fieldName, item, arrayContentDetail);
1401 }
1402 }
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413 protected void appendDetail(final StringBuffer buffer, final String fieldName, final int[] array) {
1414 buffer.append(arrayStart);
1415 for (int i = 0; i < array.length; i++) {
1416 if (i > 0) {
1417 buffer.append(arraySeparator);
1418 }
1419 appendDetail(buffer, fieldName, array[i]);
1420 }
1421 buffer.append(arrayEnd);
1422 }
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432 protected void appendDetail(final StringBuffer buffer, final String fieldName, final long value) {
1433 buffer.append(value);
1434 }
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445 protected void appendDetail(final StringBuffer buffer, final String fieldName, final long[] array) {
1446 buffer.append(arrayStart);
1447 for (int i = 0; i < array.length; i++) {
1448 if (i > 0) {
1449 buffer.append(arraySeparator);
1450 }
1451 appendDetail(buffer, fieldName, array[i]);
1452 }
1453 buffer.append(arrayEnd);
1454 }
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464 protected void appendDetail(final StringBuffer buffer, final String fieldName, final Map<?, ?> map) {
1465 buffer.append(map);
1466 }
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477 protected void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) {
1478 buffer.append(value);
1479 }
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490 protected void appendDetail(final StringBuffer buffer, final String fieldName, final Object[] array) {
1491 buffer.append(arrayStart);
1492 for (int i = 0; i < array.length; i++) {
1493 appendDetail(buffer, fieldName, i, array[i]);
1494 }
1495 buffer.append(arrayEnd);
1496 }
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506 protected void appendDetail(final StringBuffer buffer, final String fieldName, final short value) {
1507 buffer.append(value);
1508 }
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519 protected void appendDetail(final StringBuffer buffer, final String fieldName, final short[] array) {
1520 buffer.append(arrayStart);
1521 for (int i = 0; i < array.length; i++) {
1522 if (i > 0) {
1523 buffer.append(arraySeparator);
1524 }
1525 appendDetail(buffer, fieldName, array[i]);
1526 }
1527 buffer.append(arrayEnd);
1528 }
1529
1530
1531
1532
1533
1534
1535
1536
1537 public void appendEnd(final StringBuffer buffer, final Object object) {
1538 if (!this.fieldSeparatorAtEnd) {
1539 removeLastFieldSeparator(buffer);
1540 }
1541 appendContentEnd(buffer);
1542 unregister(object);
1543 }
1544
1545
1546
1547
1548
1549
1550
1551 protected void appendFieldEnd(final StringBuffer buffer, final String fieldName) {
1552 appendFieldSeparator(buffer);
1553 }
1554
1555
1556
1557
1558
1559
1560 protected void appendFieldSeparator(final StringBuffer buffer) {
1561 buffer.append(fieldSeparator);
1562 }
1563
1564
1565
1566
1567
1568
1569
1570 protected void appendFieldStart(final StringBuffer buffer, final String fieldName) {
1571 if (useFieldNames && fieldName != null) {
1572 buffer.append(fieldName);
1573 buffer.append(fieldNameValueSeparator);
1574 }
1575 }
1576
1577
1578
1579
1580
1581
1582
1583 protected void appendIdentityHashCode(final StringBuffer buffer, final Object object) {
1584 if (isUseIdentityHashCode() && object != null) {
1585 register(object);
1586 buffer.append('@');
1587 buffer.append(ObjectUtils.identityHashCodeHex(object));
1588 }
1589 }
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610 protected void appendInternal(final StringBuffer buffer, final String fieldName, final Object value, final boolean detail) {
1611 if (isRegistered(value)
1612 && !(value instanceof Number || value instanceof Boolean || value instanceof Character)) {
1613 appendCyclicObject(buffer, fieldName, value);
1614 return;
1615 }
1616
1617 register(value);
1618
1619 try {
1620 if (value instanceof Collection<?>) {
1621 if (detail) {
1622 appendDetail(buffer, fieldName, (Collection<?>) value);
1623 } else {
1624 appendSummarySize(buffer, fieldName, ((Collection<?>) value).size());
1625 }
1626
1627 } else if (value instanceof Map<?, ?>) {
1628 if (detail) {
1629 appendDetail(buffer, fieldName, (Map<?, ?>) value);
1630 } else {
1631 appendSummarySize(buffer, fieldName, ((Map<?, ?>) value).size());
1632 }
1633
1634 } else if (value instanceof long[]) {
1635 if (detail) {
1636 appendDetail(buffer, fieldName, (long[]) value);
1637 } else {
1638 appendSummary(buffer, fieldName, (long[]) value);
1639 }
1640
1641 } else if (value instanceof int[]) {
1642 if (detail) {
1643 appendDetail(buffer, fieldName, (int[]) value);
1644 } else {
1645 appendSummary(buffer, fieldName, (int[]) value);
1646 }
1647
1648 } else if (value instanceof short[]) {
1649 if (detail) {
1650 appendDetail(buffer, fieldName, (short[]) value);
1651 } else {
1652 appendSummary(buffer, fieldName, (short[]) value);
1653 }
1654
1655 } else if (value instanceof byte[]) {
1656 if (detail) {
1657 appendDetail(buffer, fieldName, (byte[]) value);
1658 } else {
1659 appendSummary(buffer, fieldName, (byte[]) value);
1660 }
1661
1662 } else if (value instanceof char[]) {
1663 if (detail) {
1664 appendDetail(buffer, fieldName, (char[]) value);
1665 } else {
1666 appendSummary(buffer, fieldName, (char[]) value);
1667 }
1668
1669 } else if (value instanceof double[]) {
1670 if (detail) {
1671 appendDetail(buffer, fieldName, (double[]) value);
1672 } else {
1673 appendSummary(buffer, fieldName, (double[]) value);
1674 }
1675
1676 } else if (value instanceof float[]) {
1677 if (detail) {
1678 appendDetail(buffer, fieldName, (float[]) value);
1679 } else {
1680 appendSummary(buffer, fieldName, (float[]) value);
1681 }
1682
1683 } else if (value instanceof boolean[]) {
1684 if (detail) {
1685 appendDetail(buffer, fieldName, (boolean[]) value);
1686 } else {
1687 appendSummary(buffer, fieldName, (boolean[]) value);
1688 }
1689
1690 } else if (ObjectUtils.isArray(value)) {
1691 if (detail) {
1692 appendDetail(buffer, fieldName, (Object[]) value);
1693 } else {
1694 appendSummary(buffer, fieldName, (Object[]) value);
1695 }
1696
1697 } else if (detail) {
1698 appendDetail(buffer, fieldName, value);
1699 } else {
1700 appendSummary(buffer, fieldName, value);
1701 }
1702 } finally {
1703 unregister(value);
1704 }
1705 }
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715 protected void appendNullText(final StringBuffer buffer, final String fieldName) {
1716 buffer.append(nullText);
1717 }
1718
1719
1720
1721
1722
1723
1724
1725 public void appendStart(final StringBuffer buffer, final Object object) {
1726 if (object != null) {
1727 appendClassName(buffer, object);
1728 appendIdentityHashCode(buffer, object);
1729 appendContentStart(buffer);
1730 if (fieldSeparatorAtStart) {
1731 appendFieldSeparator(buffer);
1732 }
1733 }
1734 }
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745 protected void appendSummary(final StringBuffer buffer, final String fieldName, final boolean[] array) {
1746 appendSummarySize(buffer, fieldName, array.length);
1747 }
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758 protected void appendSummary(final StringBuffer buffer, final String fieldName, final byte[] array) {
1759 appendSummarySize(buffer, fieldName, array.length);
1760 }
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771 protected void appendSummary(final StringBuffer buffer, final String fieldName, final char[] array) {
1772 appendSummarySize(buffer, fieldName, array.length);
1773 }
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784 protected void appendSummary(final StringBuffer buffer, final String fieldName, final double[] array) {
1785 appendSummarySize(buffer, fieldName, array.length);
1786 }
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797 protected void appendSummary(final StringBuffer buffer, final String fieldName, final float[] array) {
1798 appendSummarySize(buffer, fieldName, array.length);
1799 }
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810 protected void appendSummary(final StringBuffer buffer, final String fieldName, final int[] array) {
1811 appendSummarySize(buffer, fieldName, array.length);
1812 }
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823 protected void appendSummary(final StringBuffer buffer, final String fieldName, final long[] array) {
1824 appendSummarySize(buffer, fieldName, array.length);
1825 }
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836 protected void appendSummary(final StringBuffer buffer, final String fieldName, final Object value) {
1837 buffer.append(summaryObjectStartText);
1838 buffer.append(getShortClassName(value.getClass()));
1839 buffer.append(summaryObjectEndText);
1840 }
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851 protected void appendSummary(final StringBuffer buffer, final String fieldName, final Object[] array) {
1852 appendSummarySize(buffer, fieldName, array.length);
1853 }
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864 protected void appendSummary(final StringBuffer buffer, final String fieldName, final short[] array) {
1865 appendSummarySize(buffer, fieldName, array.length);
1866 }
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883 protected void appendSummarySize(final StringBuffer buffer, final String fieldName, final int size) {
1884 buffer.append(sizeStartText);
1885 buffer.append(size);
1886 buffer.append(sizeEndText);
1887 }
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899 public void appendSuper(final StringBuffer buffer, final String superToString) {
1900 appendToString(buffer, superToString);
1901 }
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913 public void appendToString(final StringBuffer buffer, final String toString) {
1914 if (toString != null) {
1915 final int pos1 = toString.indexOf(contentStart) + contentStart.length();
1916 final int pos2 = toString.lastIndexOf(contentEnd);
1917 if (pos1 != pos2 && pos1 >= 0 && pos2 >= 0) {
1918 if (fieldSeparatorAtStart) {
1919 removeLastFieldSeparator(buffer);
1920 }
1921 buffer.append(toString, pos1, pos2);
1922 appendFieldSeparator(buffer);
1923 }
1924 }
1925 }
1926
1927
1928
1929
1930
1931
1932 protected String getArrayEnd() {
1933 return arrayEnd;
1934 }
1935
1936
1937
1938
1939
1940
1941 protected String getArraySeparator() {
1942 return arraySeparator;
1943 }
1944
1945
1946
1947
1948
1949
1950 protected String getArrayStart() {
1951 return arrayStart;
1952 }
1953
1954
1955
1956
1957
1958
1959 protected String getContentEnd() {
1960 return contentEnd;
1961 }
1962
1963
1964
1965
1966
1967
1968 protected String getContentStart() {
1969 return contentStart;
1970 }
1971
1972
1973
1974
1975
1976
1977 protected String getFieldNameValueSeparator() {
1978 return fieldNameValueSeparator;
1979 }
1980
1981
1982
1983
1984
1985
1986 protected String getFieldSeparator() {
1987 return fieldSeparator;
1988 }
1989
1990
1991
1992
1993
1994
1995 protected String getNullText() {
1996 return nullText;
1997 }
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008 protected String getShortClassName(final Class<?> cls) {
2009 return ClassUtils.getShortClassName(cls);
2010 }
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020 protected String getSizeEndText() {
2021 return sizeEndText;
2022 }
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032 protected String getSizeStartText() {
2033 return sizeStartText;
2034 }
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044 protected String getSummaryObjectEndText() {
2045 return summaryObjectEndText;
2046 }
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056 protected String getSummaryObjectStartText() {
2057 return summaryObjectStartText;
2058 }
2059
2060
2061
2062
2063
2064
2065 protected boolean isArrayContentDetail() {
2066 return arrayContentDetail;
2067 }
2068
2069
2070
2071
2072
2073
2074
2075 protected boolean isDefaultFullDetail() {
2076 return defaultFullDetail;
2077 }
2078
2079
2080
2081
2082
2083
2084
2085
2086 protected boolean isFieldSeparatorAtEnd() {
2087 return fieldSeparatorAtEnd;
2088 }
2089
2090
2091
2092
2093
2094
2095
2096
2097 protected boolean isFieldSeparatorAtStart() {
2098 return fieldSeparatorAtStart;
2099 }
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115 protected boolean isFullDetail(final Boolean fullDetailRequest) {
2116 if (fullDetailRequest == null) {
2117 return defaultFullDetail;
2118 }
2119 return fullDetailRequest.booleanValue();
2120 }
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130 protected boolean isUseClassName() {
2131 return useClassName;
2132 }
2133
2134
2135
2136
2137
2138
2139 protected boolean isUseFieldNames() {
2140 return useFieldNames;
2141 }
2142
2143
2144
2145
2146
2147
2148 protected boolean isUseIdentityHashCode() {
2149 return useIdentityHashCode;
2150 }
2151
2152
2153
2154
2155
2156
2157
2158 protected boolean isUseShortClassName() {
2159 return useShortClassName;
2160 }
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171 protected void reflectionAppendArrayDetail(final StringBuffer buffer, final String fieldName, final Object array) {
2172 buffer.append(arrayStart);
2173 final int length = Array.getLength(array);
2174 for (int i = 0; i < length; i++) {
2175 appendDetail(buffer, fieldName, i, Array.get(array, i));
2176 }
2177 buffer.append(arrayEnd);
2178 }
2179
2180
2181
2182
2183
2184
2185
2186 protected void removeLastFieldSeparator(final StringBuffer buffer) {
2187 if (Strings.CS.endsWith(buffer, fieldSeparator)) {
2188 buffer.setLength(buffer.length() - fieldSeparator.length());
2189 }
2190 }
2191
2192
2193
2194
2195
2196
2197 protected void setArrayContentDetail(final boolean arrayContentDetail) {
2198 this.arrayContentDetail = arrayContentDetail;
2199 }
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209 protected void setArrayEnd(String arrayEnd) {
2210 if (arrayEnd == null) {
2211 arrayEnd = StringUtils.EMPTY;
2212 }
2213 this.arrayEnd = arrayEnd;
2214 }
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224 protected void setArraySeparator(String arraySeparator) {
2225 if (arraySeparator == null) {
2226 arraySeparator = StringUtils.EMPTY;
2227 }
2228 this.arraySeparator = arraySeparator;
2229 }
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239 protected void setArrayStart(String arrayStart) {
2240 if (arrayStart == null) {
2241 arrayStart = StringUtils.EMPTY;
2242 }
2243 this.arrayStart = arrayStart;
2244 }
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254 protected void setContentEnd(String contentEnd) {
2255 if (contentEnd == null) {
2256 contentEnd = StringUtils.EMPTY;
2257 }
2258 this.contentEnd = contentEnd;
2259 }
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269 protected void setContentStart(String contentStart) {
2270 if (contentStart == null) {
2271 contentStart = StringUtils.EMPTY;
2272 }
2273 this.contentStart = contentStart;
2274 }
2275
2276
2277
2278
2279
2280
2281
2282 protected void setDefaultFullDetail(final boolean defaultFullDetail) {
2283 this.defaultFullDetail = defaultFullDetail;
2284 }
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294 protected void setFieldNameValueSeparator(String fieldNameValueSeparator) {
2295 if (fieldNameValueSeparator == null) {
2296 fieldNameValueSeparator = StringUtils.EMPTY;
2297 }
2298 this.fieldNameValueSeparator = fieldNameValueSeparator;
2299 }
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309 protected void setFieldSeparator(String fieldSeparator) {
2310 if (fieldSeparator == null) {
2311 fieldSeparator = StringUtils.EMPTY;
2312 }
2313 this.fieldSeparator = fieldSeparator;
2314 }
2315
2316
2317
2318
2319
2320
2321
2322
2323 protected void setFieldSeparatorAtEnd(final boolean fieldSeparatorAtEnd) {
2324 this.fieldSeparatorAtEnd = fieldSeparatorAtEnd;
2325 }
2326
2327
2328
2329
2330
2331
2332
2333
2334 protected void setFieldSeparatorAtStart(final boolean fieldSeparatorAtStart) {
2335 this.fieldSeparatorAtStart = fieldSeparatorAtStart;
2336 }
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346 protected void setNullText(String nullText) {
2347 if (nullText == null) {
2348 nullText = StringUtils.EMPTY;
2349 }
2350 this.nullText = nullText;
2351 }
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364 protected void setSizeEndText(String sizeEndText) {
2365 if (sizeEndText == null) {
2366 sizeEndText = StringUtils.EMPTY;
2367 }
2368 this.sizeEndText = sizeEndText;
2369 }
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382 protected void setSizeStartText(String sizeStartText) {
2383 if (sizeStartText == null) {
2384 sizeStartText = StringUtils.EMPTY;
2385 }
2386 this.sizeStartText = sizeStartText;
2387 }
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400 protected void setSummaryObjectEndText(String summaryObjectEndText) {
2401 if (summaryObjectEndText == null) {
2402 summaryObjectEndText = StringUtils.EMPTY;
2403 }
2404 this.summaryObjectEndText = summaryObjectEndText;
2405 }
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418 protected void setSummaryObjectStartText(String summaryObjectStartText) {
2419 if (summaryObjectStartText == null) {
2420 summaryObjectStartText = StringUtils.EMPTY;
2421 }
2422 this.summaryObjectStartText = summaryObjectStartText;
2423 }
2424
2425
2426
2427
2428
2429
2430 protected void setUseClassName(final boolean useClassName) {
2431 this.useClassName = useClassName;
2432 }
2433
2434
2435
2436
2437
2438
2439 protected void setUseFieldNames(final boolean useFieldNames) {
2440 this.useFieldNames = useFieldNames;
2441 }
2442
2443
2444
2445
2446
2447
2448 protected void setUseIdentityHashCode(final boolean useIdentityHashCode) {
2449 this.useIdentityHashCode = useIdentityHashCode;
2450 }
2451
2452
2453
2454
2455
2456
2457
2458 protected void setUseShortClassName(final boolean useShortClassName) {
2459 this.useShortClassName = useShortClassName;
2460 }
2461 }