Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6d3096148dae9cc773a85804a9a6104d2e62a326 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package org.eclipse.jdt.internal.core.util;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
import java.io.*;

/**
 * A dumper knows how to dump objects in a human-readable representation to a PrintWriter.
 * Objects which implement IDumpable know how to dump themselves to a Dumper, otherwise
 * the dumper just uses toString() on the object.
 * Example:
 * <pre>
 *   Dumper dumper = new Dumper(System.out);
 *   dumper.dump(myObject);
 *   dumper.flush();
 * </pre>
 *
 * @see IDumpable
 * @see PrintWriter
 */
public class Dumper {
	int fTabLevel = 0;
	PrintWriter fWriter;
/**
 * Creates a dumper which sends output to System.out.
 */
public Dumper() {
	this(System.out);
}
/**
 * Creates a dumper which sends output to the given OutputStream.
 */
public Dumper(OutputStream out) {
	this(new PrintWriter(out));
}
/**
 * Creates a dumper which sends output to the given PrintWriter.
 */
public Dumper(PrintWriter writer) {
	fWriter = writer;
}
/**
 * Returns the class name to use for the given object.
 */
String classNameFor(Object val) {
	String name = val.getClass().getName();
	int i = name.lastIndexOf('.');
	if (i != -1) {
		name = name.substring(i+1);
	}
	return name;
}
/**
 * Dumps the given array.
 * Prints a maximum of maxPerLine items per line.
 */
public void dump(int[] val, int maxPerLine) {
	int len = val.length;
	boolean oneLine = (len <= maxPerLine);
	if (!oneLine) {
		++fTabLevel;
	}
	fWriter.print("[");
	for (int i = 0; i < len; ++i) {
		if (!oneLine) {
			if ((i % maxPerLine) == 0) {
				fWriter.println();
				tabIfNeeded();
				fWriter.print(i);
				fWriter.print(": ");
			}
		}
		fWriter.print(val[i]);
		if (i + 1 < len) {
			fWriter.print(", ");
		}
	}
	if (oneLine) {
		fWriter.print("]");
		fWriter.println();
	}
	else {
		fWriter.println();
		--fTabLevel;
		tabIfNeeded();
		fWriter.print("]");
		fWriter.println();
	}
}
/**
 * Dumps the given array.
 */
public void dump(Object[] val) {
	int len = val.length;
	fWriter.print("[");
	if (len > 0) {
		fWriter.println();
		++fTabLevel;
		for (int i = 0; i < len; ++i) {
			dump(val[i]);
		}
		--fTabLevel;
		tabIfNeeded();
	}
	fWriter.println("]");
}
/**
 * Dumps the given array.
 * Prints a maximum of maxPerLine items per line.
 */
public void dump(String[] val, int maxPerLine) {
	int len = val.length;
	boolean oneLine = (len <= maxPerLine);
	if (!oneLine) {
		++fTabLevel;
	}
	fWriter.print("[");
	boolean newLine = !oneLine;
	for (int i = 0; i < len; ++i) {
		if (newLine) {
			fWriter.println();
			tabIfNeeded();
		}
		fWriter.print("\"" + val[i] + "\"");
		if (i + 1 < len) {
			fWriter.print(", ");
		}
		newLine = (i != 0 && (i % maxPerLine) == 0);
	}
	fWriter.print("]");
	if (oneLine || newLine) {
		fWriter.println();
	}
	if (!oneLine) {
		--fTabLevel;
	}
}
/**
 * Dumps the given value.
 */
public void dump(Object val) {
	tabIfNeeded();
	if (val instanceof IDumpable) {
		IDumpable dumpable = (IDumpable) val;
		fWriter.println(classNameFor(val) + " {");
		int originalLevel = fTabLevel;
		++fTabLevel;
		try {
			dumpable.dump(this);
		}
		catch (Throwable t) {
			fWriter.println("*ERR*");
		}
		fTabLevel = originalLevel;
		tabIfNeeded();
		fWriter.println("}");
	}
	else {
		fWriter.println(val);
	}
}
/**
 * Dumps the given value, with the given name as a prefix.
 */
public void dump(String name, int[] val) {
	dump(name, val, 10);
}
/**
 * Dumps the given array, with the given name as a prefix.
 * Prints a maximum of maxPerLine items per line.
 */
public void dump(String name, int[] val, int maxPerLine) {
	prefix(name);
	dump(val, maxPerLine);
}
/**
 * Dumps the given value, with the given name as a prefix.
 */
public void dump(String name, Object[] val) {
	prefix(name);
	dump(val);
}
/**
 * Dumps the given value, with the given name as a prefix.
 */
public void dump(String name, String[] val) {
	prefix(name);
	dump(val, 5);
}
/**
 * Dumps the given value, with the given name as a prefix.
 */
public void dump(String name, int val) {
	prefix(name);
	fWriter.println(val);
}
/**
 * Dumps the given value, with the given name as a prefix.
 */
public void dump(String name, Object val) {
	prefix(name);
	fWriter.println();
	++fTabLevel;
	dump(val);
	--fTabLevel;
}
/**
 * Dumps the given value, with the given name as a prefix.
 */
public void dump(String name, String val) {
	prefix(name);
	fWriter.println(val == null ? "null" : "\"" + val + "\"");
}
/**
 * Dumps the given value, with the given name as a prefix.
 */
public void dump(String name, boolean val) {
	prefix(name);
	fWriter.println(val);
}
/**
 * Dumps the given message, with the given name as a prefix.
 */
public void dumpMessage(String name, String msg) {
	prefix(name);
	fWriter.println(msg);
}
/**
 * Flushes the output writer.
 */
public void flush() {
	fWriter.flush();
}
/**
 * Returns the current tab level.
 */
public int getTabLevel() {
	return fTabLevel;
}
/**
 * Returns the underlying PrintWriter.
 */
public PrintWriter getWriter() {
	return fWriter;
}
/**
 * Increase the current tab level.
 */
public void indent() {
	++fTabLevel;
}
/**
 * Decrease the current tab level.
 */
public void outdent() {
	if (--fTabLevel < 0) 
		fTabLevel = 0;
}
/**
 * Outputs the given prefix, tabbing first if needed.
 */
void prefix(String name) {
	tabIfNeeded();
	fWriter.print(name);
	fWriter.print(": ");
}
/**
 * Print an object directly, without a newline.
 */
public void print(Object obj) {
	fWriter.print(obj);
}
/**
 * Print a newline directly.
 */
public void println() {
	fWriter.println();
}
/**
 * Print an object directly, with a newline.
 */
public void println(Object obj) {
	fWriter.println(obj);
}
/**
 * Outputs tabs if needed.
 */
void tabIfNeeded() {
	for (int i = 0; i < fTabLevel; ++i) {
		fWriter.print("  ");
	}
}
}

Back to the top