KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > stringification > OurStringBuilder


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util.stringification;
5
6 import org.apache.commons.lang.SystemUtils;
7 import org.apache.commons.lang.builder.StandardToStringStyle;
8 import org.apache.commons.lang.builder.ToStringBuilder;
9 import org.apache.commons.lang.builder.ToStringStyle;
10
11 /**
12  * A subclass of {@link ToStringBuilder} that lets us override things as we'd want.
13  */

14 public class OurStringBuilder extends ToStringBuilder {
15
16   public static StandardToStringStyle STANDARD_STYLE;
17   public static StandardToStringStyle MULTI_LINE_STYLE;
18   public static StandardToStringStyle COMPACT_STYLE;
19
20   static {
21     STANDARD_STYLE = new StandardToStringStyle();
22     STANDARD_STYLE.setUseShortClassName(true);
23     STANDARD_STYLE.setArrayContentDetail(true);
24     STANDARD_STYLE.setFieldSeparator(", ");
25     STANDARD_STYLE.setArraySeparator(", ");
26
27     MULTI_LINE_STYLE = new StandardToStringStyle();
28     MULTI_LINE_STYLE.setUseShortClassName(true);
29     MULTI_LINE_STYLE.setArrayContentDetail(true);
30     MULTI_LINE_STYLE.setContentStart("[");
31     MULTI_LINE_STYLE.setFieldSeparator(SystemUtils.LINE_SEPARATOR + " ");
32     MULTI_LINE_STYLE.setFieldSeparatorAtStart(true);
33     MULTI_LINE_STYLE.setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
34     MULTI_LINE_STYLE.setArraySeparator(", ");
35
36     COMPACT_STYLE = new StandardToStringStyle() {
37       public void appendStart(StringBuffer JavaDoc buffer, Object JavaDoc object) {
38         buffer.append("<");
39         appendClassName(buffer, object);
40         appendIdentityHashCode(buffer, object);
41         appendContentStart(buffer);
42       }
43     };
44     COMPACT_STYLE.setUseShortClassName(true);
45     COMPACT_STYLE.setArrayContentDetail(true);
46     COMPACT_STYLE.setContentStart(": ");
47     COMPACT_STYLE.setContentEnd(">");
48     COMPACT_STYLE.setFieldNameValueSeparator(" ");
49     COMPACT_STYLE.setFieldSeparator(", ");
50     COMPACT_STYLE.setArraySeparator(", ");
51   }
52
53   public OurStringBuilder(Object JavaDoc arg0, ToStringStyle arg1, StringBuffer JavaDoc arg2) {
54     super(arg0, arg1, arg2);
55   }
56
57   public OurStringBuilder(Object JavaDoc arg0, ToStringStyle arg1) {
58     super(arg0, arg1);
59   }
60
61   public OurStringBuilder(Object JavaDoc arg0) {
62     this(arg0, STANDARD_STYLE);
63   }
64   
65   public ToStringBuilder append(String JavaDoc tag, Object JavaDoc[]arr, boolean b) {
66     if (arr == null) return super.append(tag, arr, b);
67     if (arr.length == 0) return super.append(tag, arr, b);
68     this.append("{elementCount=" + arr.length + ":");
69     for (int i = 0; i < arr.length; i++) {
70       Object JavaDoc e = arr[i];
71       if (e == null) this.append("<null>");
72       else this.append("[#" + i + "<" + e.getClass().getName() + "=" + e + ">]");
73     }
74     this.append("]");
75     return this;
76   }
77
78 }
79
Popular Tags