001: /**
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */package com.tc.util;
004:
005: import org.apache.commons.lang.ClassUtils;
006:
007: /**
008: * A {@link Stringifier}that generally uses {@link Object#toString()}on objects, but uses special cases for certain
009: * objects (like arrays) for which {@link Object#toString()}doesn't do a very good job.
010: */
011: public class StandardStringifier implements Stringifier {
012:
013: public static final StandardStringifier INSTANCE = new StandardStringifier();
014:
015: private StandardStringifier() {
016: // Use INSTANCE instead.
017: }
018:
019: public String toString(Object o) {
020: if (o == null)
021: return "<null>";
022: else if (o instanceof Object[]) {
023: return toString((Object[]) o);
024: } else if (o instanceof byte[]) {
025: return toString((byte[]) o);
026: } else if (o instanceof short[]) {
027: return toString((short[]) o);
028: } else if (o instanceof int[]) {
029: return toString((int[]) o);
030: } else if (o instanceof long[]) {
031: return toString((long[]) o);
032: } else if (o instanceof boolean[]) {
033: return toString((boolean[]) o);
034: } else if (o instanceof char[]) {
035: return toString((char[]) o);
036: } else if (o instanceof float[]) {
037: return toString((float[]) o);
038: } else if (o instanceof double[]) {
039: return toString((double[]) o);
040: } else {
041: return o.toString();
042: }
043: }
044:
045: private static final int MAX_ITEMS_TO_SHOW = 20;
046:
047: private String toString(Object[] arr) {
048: StringBuffer out = new StringBuffer();
049:
050: out.append(ClassUtils.getShortClassName(arr.getClass()
051: .getComponentType())
052: + "[" + arr.length + "]");
053: if (arr.length > 0) {
054: out.append(": ");
055: int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW);
056: for (int i = 0; i < count; ++i) {
057: if (i > 0)
058: out.append(", ");
059: out.append(toString(arr[i]));
060: }
061:
062: if (arr.length > count) {
063: out.append(", ...");
064: }
065: }
066:
067: return out.toString();
068: }
069:
070: private String toString(byte[] arr) {
071: return HexDump.dump(arr);
072: }
073:
074: private String toString(short[] arr) {
075: StringBuffer out = new StringBuffer();
076:
077: out.append(ClassUtils.getShortClassName(arr.getClass()
078: .getComponentType())
079: + "[" + arr.length + "]");
080: if (arr.length > 0) {
081: out.append(": ");
082: int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW);
083: for (int i = 0; i < count; ++i) {
084: if (i > 0)
085: out.append(", ");
086: out.append("" + arr[i]);
087: }
088:
089: if (arr.length > count) {
090: out.append(", ...");
091: }
092: }
093:
094: return out.toString();
095: }
096:
097: private String toString(int[] arr) {
098: StringBuffer out = new StringBuffer();
099:
100: out.append(ClassUtils.getShortClassName(arr.getClass()
101: .getComponentType())
102: + "[" + arr.length + "]");
103: if (arr.length > 0) {
104: out.append(": ");
105: int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW);
106: for (int i = 0; i < count; ++i) {
107: if (i > 0)
108: out.append(", ");
109: out.append("" + arr[i]);
110: }
111:
112: if (arr.length > count) {
113: out.append(", ...");
114: }
115: }
116:
117: return out.toString();
118: }
119:
120: private String toString(long[] arr) {
121: StringBuffer out = new StringBuffer();
122:
123: out.append(ClassUtils.getShortClassName(arr.getClass()
124: .getComponentType())
125: + "[" + arr.length + "]");
126: if (arr.length > 0) {
127: out.append(": ");
128: int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW);
129: for (int i = 0; i < count; ++i) {
130: if (i > 0)
131: out.append(", ");
132: out.append("" + arr[i]);
133: }
134:
135: if (arr.length > count) {
136: out.append(", ...");
137: }
138: }
139:
140: return out.toString();
141: }
142:
143: private String toString(char[] arr) {
144: StringBuffer out = new StringBuffer();
145:
146: out.append(ClassUtils.getShortClassName(arr.getClass()
147: .getComponentType())
148: + "[" + arr.length + "]");
149: if (arr.length > 0) {
150: out.append(": ");
151: int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW);
152: for (int i = 0; i < count; ++i) {
153: if (i > 0)
154: out.append(", ");
155: out.append("" + arr[i]);
156: }
157:
158: if (arr.length > count) {
159: out.append(", ...");
160: }
161: }
162:
163: return out.toString();
164: }
165:
166: private String toString(float[] arr) {
167: StringBuffer out = new StringBuffer();
168:
169: out.append(ClassUtils.getShortClassName(arr.getClass()
170: .getComponentType())
171: + "[" + arr.length + "]");
172: if (arr.length > 0) {
173: out.append(": ");
174: int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW);
175: for (int i = 0; i < count; ++i) {
176: if (i > 0)
177: out.append(", ");
178: out.append("" + arr[i]);
179: }
180:
181: if (arr.length > count) {
182: out.append(", ...");
183: }
184: }
185:
186: return out.toString();
187: }
188:
189: private String toString(double[] arr) {
190: StringBuffer out = new StringBuffer();
191:
192: out.append(ClassUtils.getShortClassName(arr.getClass()
193: .getComponentType())
194: + "[" + arr.length + "]");
195: if (arr.length > 0) {
196: out.append(": ");
197: int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW);
198: for (int i = 0; i < count; ++i) {
199: if (i > 0)
200: out.append(", ");
201: out.append("" + arr[i]);
202: }
203:
204: if (arr.length > count) {
205: out.append(", ...");
206: }
207: }
208:
209: return out.toString();
210: }
211:
212: private String toString(boolean[] arr) {
213: StringBuffer out = new StringBuffer();
214:
215: out.append(ClassUtils.getShortClassName(arr.getClass()
216: .getComponentType())
217: + "[" + arr.length + "]");
218: if (arr.length > 0) {
219: out.append(": ");
220: int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW);
221: for (int i = 0; i < count; ++i) {
222: if (i > 0)
223: out.append(", ");
224: out.append("" + arr[i]);
225: }
226:
227: if (arr.length > count) {
228: out.append(", ...");
229: }
230: }
231:
232: return out.toString();
233: }
234:
235: }
|