01: /**
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */package com.tc.util.stringification;
04:
05: import org.apache.commons.lang.SystemUtils;
06: import org.apache.commons.lang.builder.StandardToStringStyle;
07: import org.apache.commons.lang.builder.ToStringBuilder;
08: import org.apache.commons.lang.builder.ToStringStyle;
09:
10: /**
11: * A subclass of {@link ToStringBuilder} that lets us override things as we'd want.
12: */
13: public class OurStringBuilder extends ToStringBuilder {
14:
15: public static StandardToStringStyle STANDARD_STYLE;
16: public static StandardToStringStyle MULTI_LINE_STYLE;
17: public static StandardToStringStyle COMPACT_STYLE;
18:
19: static {
20: STANDARD_STYLE = new StandardToStringStyle();
21: STANDARD_STYLE.setUseShortClassName(true);
22: STANDARD_STYLE.setArrayContentDetail(true);
23: STANDARD_STYLE.setFieldSeparator(", ");
24: STANDARD_STYLE.setArraySeparator(", ");
25:
26: MULTI_LINE_STYLE = new StandardToStringStyle();
27: MULTI_LINE_STYLE.setUseShortClassName(true);
28: MULTI_LINE_STYLE.setArrayContentDetail(true);
29: MULTI_LINE_STYLE.setContentStart("[");
30: MULTI_LINE_STYLE.setFieldSeparator(SystemUtils.LINE_SEPARATOR
31: + " ");
32: MULTI_LINE_STYLE.setFieldSeparatorAtStart(true);
33: MULTI_LINE_STYLE
34: .setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
35: MULTI_LINE_STYLE.setArraySeparator(", ");
36:
37: COMPACT_STYLE = new StandardToStringStyle() {
38: public void appendStart(StringBuffer buffer, Object object) {
39: buffer.append("<");
40: appendClassName(buffer, object);
41: appendIdentityHashCode(buffer, object);
42: appendContentStart(buffer);
43: }
44: };
45: COMPACT_STYLE.setUseShortClassName(true);
46: COMPACT_STYLE.setArrayContentDetail(true);
47: COMPACT_STYLE.setContentStart(": ");
48: COMPACT_STYLE.setContentEnd(">");
49: COMPACT_STYLE.setFieldNameValueSeparator(" ");
50: COMPACT_STYLE.setFieldSeparator(", ");
51: COMPACT_STYLE.setArraySeparator(", ");
52: }
53:
54: public OurStringBuilder(Object arg0, ToStringStyle arg1,
55: StringBuffer arg2) {
56: super (arg0, arg1, arg2);
57: }
58:
59: public OurStringBuilder(Object arg0, ToStringStyle arg1) {
60: super (arg0, arg1);
61: }
62:
63: public OurStringBuilder(Object arg0) {
64: this (arg0, STANDARD_STYLE);
65: }
66:
67: public ToStringBuilder append(String tag, Object[] arr, boolean b) {
68: if (arr == null)
69: return super .append(tag, arr, b);
70: if (arr.length == 0)
71: return super .append(tag, arr, b);
72: this .append("{elementCount=" + arr.length + ":");
73: for (int i = 0; i < arr.length; i++) {
74: Object e = arr[i];
75: if (e == null)
76: this .append("<null>");
77: else
78: this .append("[#" + i + "<" + e.getClass().getName()
79: + "=" + e + ">]");
80: }
81: this .append("]");
82: return this;
83: }
84:
85: }
|