File: AbstractFormatter.java

package info (click to toggle)
libcolt-free-java 1.2.0%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,836 kB
  • sloc: java: 30,337; xml: 893; makefile: 26; sh: 3
file content (503 lines) | stat: -rw-r--r-- 16,334 bytes parent folder | download | duplicates (3)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
Copyright (c) 1999 CERN - European Organization for Nuclear Research.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
is hereby granted without fee, provided that the above copyright notice appear in all copies and 
that both that copyright notice and this permission notice appear in supporting documentation. 
CERN makes no representations about the suitability of this software for any purpose. 
It is provided "as is" without expressed or implied warranty.
*/
package cern.colt.matrix.impl;

/** 
Abstract base class for flexible, well human readable matrix print formatting.
Value type independent.
A single cell is formatted via a format string.
Columns can be aligned left, centered, right and by decimal point. 
<p>A column can be broader than specified by the parameter <tt>minColumnWidth</tt> 
  (because a cell may not fit into that width) but a column is never smaller than 
  <tt>minColumnWidth</tt>. Normally one does not need to specify <tt>minColumnWidth</tt>.
Cells in a row are separated by a separator string, similar separators can be set for rows and slices.
For more info, see the concrete subclasses.
 
@author wolfgang.hoschek@cern.ch
@version 1.0, 09/24/99
*/
public abstract class AbstractFormatter extends cern.colt.PersistentObject {
 	/**
 	 * The alignment string aligning the cells of a column to the left.
 	 */
 	public static final String LEFT = "left";
	
 	/**
 	 * The alignment string aligning the cells of a column to its center.
 	 */
 	public static final String CENTER = "center";
	
 	/**
 	 * The alignment string aligning the cells of a column to the right.
 	 */
 	public static final String RIGHT = "right";
	
 	/**
 	 * The alignment string aligning the cells of a column to the decimal point.
 	 */
 	public static final String DECIMAL = "decimal";

 	/**
 	 * The default minimum number of characters a column may have; currently <tt>1</tt>.
 	 */
	public static final int DEFAULT_MIN_COLUMN_WIDTH = 1;
	
 	/**
 	 * The default string separating any two columns from another; currently <tt>" "</tt>.
 	 */
	public static final String DEFAULT_COLUMN_SEPARATOR = " ";

 	/**
 	 * The default string separating any two rows from another; currently <tt>"\n"</tt>.
 	 */
	public static final String DEFAULT_ROW_SEPARATOR = "\n";

	/**
 	 * The default string separating any two slices from another; currently <tt>"\n\n"</tt>.
 	 */
	public static final String DEFAULT_SLICE_SEPARATOR = "\n\n";

	
 	/**
 	 * The default format string for formatting a single cell value; currently <tt>"%G"</tt>.
 	 */
 	protected String alignment = LEFT;
	
 	/**
 	 * The default format string for formatting a single cell value; currently <tt>"%G"</tt>.
 	 */
 	protected String format = "%G";
	
 	/**
 	 * The default minimum number of characters a column may have; currently <tt>1</tt>.
 	 */
	protected int minColumnWidth = DEFAULT_MIN_COLUMN_WIDTH;
	
 	/**
 	 * The default string separating any two columns from another; currently <tt>" "</tt>.
 	 */
	protected String columnSeparator= DEFAULT_COLUMN_SEPARATOR;

 	/**
 	 * The default string separating any two rows from another; currently <tt>"\n"</tt>.
 	 */
	protected String rowSeparator = DEFAULT_ROW_SEPARATOR;

	/**
 	 * The default string separating any two slices from another; currently <tt>"\n\n"</tt>.
 	 */
	protected String sliceSeparator = DEFAULT_SLICE_SEPARATOR;

	/**
 	 * Tells whether String representations are to be preceded with summary of the shape; currently <tt>true</tt>.
 	 */
	protected boolean printShape = true;
	

	private static String[] blanksCache; // for efficient String manipulations

	protected static final FormerFactory factory = new FormerFactory();

	static {
		setupBlanksCache();
	}
/**
 * Makes this class non instantiable, but still let's others inherit from it.
 */
protected AbstractFormatter() {}
/**
 * Modifies the strings in a column of the string matrix to be aligned (left,centered,right,decimal).
 */
protected void align(String[][] strings) {
	int rows = strings.length;
	int columns = 0;
	if (rows>0) columns = strings[0].length;

	int[] maxColWidth = new int[columns];
	int[] maxColLead = null;
	boolean isDecimal = alignment.equals(DECIMAL);
	if (isDecimal) maxColLead = new int[columns];
	//int[] maxColTrail = new int[columns];

	// for each column, determine alignment parameters
	for (int column=0; column<columns; column++) {
		int maxWidth = minColumnWidth;
		int maxLead  = Integer.MIN_VALUE;
		//int maxTrail = Integer.MIN_VALUE;
		for (int row=0; row<rows; row++) {
			String s = strings[row][column];
			maxWidth = Math.max(maxWidth, s.length());
			if (isDecimal) maxLead = Math.max(maxLead, lead(s));
			//maxTrail = Math.max(maxTrail, trail(s));
		}
		maxColWidth[column] = maxWidth;
		if (isDecimal) maxColLead[column] = maxLead;
		//maxColTrail[column] = maxTrail;
	}

	// format each row according to alignment parameters
	//StringBuffer total = new StringBuffer();
	for (int row=0; row<rows; row++) {
		alignRow(strings[row], maxColWidth, maxColLead);
	}

}
/**
 * Converts a row into a string.
 */
protected int alignmentCode(String alignment) {
	//{-1,0,1,2} = {left,centered,right,decimal point}
	if (alignment.equals(LEFT)) return -1;
	else if (alignment.equals(CENTER)) return 0;
	else if (alignment.equals(RIGHT)) return 1;
	else if (alignment.equals(DECIMAL)) return 2;
	else throw new IllegalArgumentException("unknown alignment: "+alignment);
}
/**
 * Modifies the strings the string matrix to be aligned (left,centered,right,decimal).
 */
protected void alignRow(String[] row, int[] maxColWidth, int[] maxColLead) {
	int align = alignmentCode(alignment); //{-1,0,1,2} = {left,centered,right,decimal point}
	StringBuffer s = new StringBuffer();

	int columns = row.length;
	for (int column=0; column<columns; column++) {
		s.setLength(0);
		String c = row[column];
		//if (alignment==1) {
		if (alignment.equals(RIGHT)) {
			s.append(blanks(maxColWidth[column] - s.length()));
			s.append(c);
		}
		//else if (alignment==2) {
		else if (alignment.equals(DECIMAL)) {
			s.append(blanks(maxColLead[column] - lead(c)));
			s.append(c);
			s.append(blanks(maxColWidth[column] - s.length()));
		}
		//else if (align==0) {
		else if (alignment.equals(CENTER)) {
			s.append(blanks((maxColWidth[column] - c.length()) / 2));
			s.append(c);
			s.append(blanks(maxColWidth[column] - s.length()));

		}
		//else if (align<0) {
		else if (alignment.equals(LEFT)) {
			s.append(c);
			s.append(blanks(maxColWidth[column] - s.length()));
		}
		else throw new InternalError();
		
		row[column] = s.toString();
	}
}
/**
 * Returns a String with <tt>length</tt> blanks.
 */
protected String blanks(int length) {
	if (length < 0) length = 0;
	if (length < blanksCache.length) return blanksCache[length];
	
	StringBuffer buf = new StringBuffer(length);
	for (int k = 0; k < length; k++) {
		buf.append(' ');
	}
	return buf.toString();
}
/**
 * Demonstrates how to use this class.
 */
public static void demo1() {
/*
// parameters
Object[][] values = {
	{3,     0,        -3.4, 0},
	{5.1   ,0,        +3.0123456789, 0},
	{16.37, 0.0,       2.5, 0},
	{-16.3, 0,        -3.012345678E-4, -1},
	{1236.3456789, 0,  7, -1.2}
};
String[] formats =         {"%G", "%1.10G", "%f", "%1.2f", "%0.2e", null};


// now the processing
int size = formats.length;
ObjectMatrix2D matrix = cern.colt.matrix.ObjectFactory2D.dense.make(values);
String[] strings = new String[size];
String[] sourceCodes = new String[size];
String[] htmlStrings = new String[size];
String[] htmlSourceCodes = new String[size];

for (int i=0; i<size; i++) {
	String format = formats[i];
	strings[i] = toString(matrix,format);
	sourceCodes[i] = toSourceCode(matrix,format);

	// may not compile because of packages not included in the distribution
	//htmlStrings[i] = cern.colt.matrixpattern.Converting.toHTML(strings[i]);
	//htmlSourceCodes[i] = cern.colt.matrixpattern.Converting.toHTML(sourceCodes[i]);
}

System.out.println("original:\n"+toString(matrix));

// may not compile because of packages not included in the distribution
for (int i=0; i<size; i++) {
	//System.out.println("\nhtmlString("+formats[i]+"):\n"+htmlStrings[i]);
	//System.out.println("\nhtmlSourceCode("+formats[i]+"):\n"+htmlSourceCodes[i]);
}

for (int i=0; i<size; i++) {
	System.out.println("\nstring("+formats[i]+"):\n"+strings[i]);
	System.out.println("\nsourceCode("+formats[i]+"):\n"+sourceCodes[i]);
}
*/
}
/**
 * Demonstrates how to use this class.
 */
public static void demo2() {
/*
// parameters
Object[] values = {
	//5, 0.0, -0.0, -Object.NaN, Object.NaN, 0.0/0.0, Object.NEGATIVE_INFINITY, Object.POSITIVE_INFINITY, Object.MIN_VALUE, Object.MAX_VALUE
	5, 0.0, -0.0, -Object.NaN, Object.NaN, 0.0/0.0, Object.MIN_VALUE, Object.MAX_VALUE , Object.NEGATIVE_INFINITY, Object.POSITIVE_INFINITY
	//Object.MIN_VALUE, Object.MAX_VALUE //, Object.NEGATIVE_INFINITY, Object.POSITIVE_INFINITY
};
//String[] formats =         {"%G", "%1.10G", "%f", "%1.2f", "%0.2e"};
String[] formats =         {"%G", "%1.19G"};


// now the processing
int size = formats.length;
ObjectMatrix1D matrix = new DenseObjectMatrix1D(values);

String[] strings = new String[size];
//String[] javaStrings = new String[size];

for (int i=0; i<size; i++) {
	String format = formats[i];
	strings[i] = toString(matrix,format);
	for (int j=0; j<matrix.size(); j++) {
		System.out.println(String.valueOf(matrix.get(j)));
	}
}

System.out.println("original:\n"+toString(matrix));

for (int i=0; i<size; i++) {
	System.out.println("\nstring("+formats[i]+"):\n"+strings[i]);
}
*/
}
/**
 * Demonstrates how to use this class.
 */
public static void demo3(int size, Object value) {
	/*
	cern.colt.Timer timer = new cern.colt.Timer();
	String s;
	StringBuffer buf;
	ObjectMatrix2D matrix = cern.colt.matrix.ObjectFactory2D.dense.make(size,size, value);

	timer.reset().start();
	buf = new StringBuffer();
	for (int i=size; --i >= 0; ) {
		for (int j=size; --j >= 0; ) {
			buf.append(matrix.getQuick(i,j));
		}
	}
	buf = null;
	timer.stop().display();

	timer.reset().start();
	corejava.Format format = new corejava.Format("%G");
	buf = new StringBuffer();
	for (int i=size; --i >= 0; ) {
		for (int j=size; --j >= 0; ) {
			buf.append(format.form(matrix.getQuick(i,j)));
		}
	}
	buf = null;
	timer.stop().display();

	timer.reset().start();
	s = Formatting.toString(matrix, null);
	//System.out.println(s);
	s = null;
	timer.stop().display();

	timer.reset().start();
	s = Formatting.toString(matrix, "%G");
	//System.out.println(s);
	s = null;
	timer.stop().display();
	*/
}
/**
 * Converts a given cell to a String; no alignment considered.
 */
protected abstract String form(AbstractMatrix1D matrix, int index, Former formatter);
/**
 * Returns a string representations of all cells; no alignment considered.
 */
protected abstract String[][] format(AbstractMatrix2D matrix);
/**
 * Returns a string representations of all cells; no alignment considered.
 */
protected String[] formatRow(AbstractMatrix1D vector) {
	Former formatter = null;
	formatter = factory.create(format);
	int s = vector.size();
	String[] strings = new String[s];
	for (int i=0; i<s; i++) {
		strings[i] = form(vector,i,formatter);
	}
	return strings;
}
/**
 * Returns the number of characters or the number of characters before the decimal point.
 */
protected int lead(String s) {
	return s.length();
}
/**
 * Returns a String with the given character repeated <tt>length</tt> times.
 */
protected String repeat(char character, int length) {
	if (character==' ') return blanks(length);
	if (length < 0) length = 0;
	StringBuffer buf = new StringBuffer(length);
	for (int k = 0; k < length; k++) {
		buf.append(character);
	}
	return buf.toString();
}
/**
 * Sets the column alignment (left,center,right,decimal).
 * @param alignment the new alignment to be used; must be one of <tt>{LEFT,CENTER,RIGHT,DECIMAL}</tt>.
 */
public void setAlignment(String alignment) {
	this.alignment = alignment;
}
/**
 * Sets the string separating any two columns from another.
 * @param columnSeparator the new columnSeparator to be used.
 */
public void setColumnSeparator(String columnSeparator) {
	this.columnSeparator = columnSeparator;
}
/**
 * Sets the way a <i>single</i> cell value is to be formatted.
 * @param format the new format to be used.
 */
public void setFormat(String format) {
	this.format = format;
}
/**
 * Sets the minimum number of characters a column may have.
 * @param minColumnWidth the new minColumnWidth to be used.
 */
public void setMinColumnWidth(int minColumnWidth) {
	if (minColumnWidth<0) throw new IllegalArgumentException();
	this.minColumnWidth = minColumnWidth;
}
/**
 * Specifies whether a string representation of a matrix is to be preceded with a summary of its shape.
 * @param printShape <tt>true</tt> shape summary is printed, otherwise not printed.
 */
public void setPrintShape(boolean printShape) {
	this.printShape = printShape;
}
/**
 * Sets the string separating any two rows from another.
 * @param rowSeparator the new rowSeparator to be used.
 */
public void setRowSeparator(String rowSeparator) {
	this.rowSeparator = rowSeparator;
}
/**
 * Sets the string separating any two slices from another.
 * @param sliceSeparator the new sliceSeparator to be used.
 */
public void setSliceSeparator(String sliceSeparator) {
	this.sliceSeparator = sliceSeparator;
}
/**
 * Cache for faster string processing.
 */
protected static void setupBlanksCache() {
	// Pre-fabricate 40 static strings with 0,1,2,..,39 blanks, for usage within method blanks(length).
	// Now, we don't need to construct and fill them on demand, and garbage collect them again.
	// All 40 strings share the identical char[] array, only with different offset and length --> somewhat smaller static memory footprint
	int size = 40;
	blanksCache = new String[size];
	StringBuffer buf = new StringBuffer(size);
	for (int i=size; --i >= 0; ) buf.append(' ');
	String str = buf.toString();
	for (int i=size; --i >= 0; ) {
		blanksCache[i] = str.substring(0,i);
		//System.out.println(i+"-"+blanksCache[i]+"-");
	}
}
/**
 * Returns a short string representation describing the shape of the matrix.
 */
public static String shape(AbstractMatrix1D matrix) {
	//return "Matrix1D of size="+matrix.size();
	//return matrix.size()+" element matrix";
	//return "matrix("+matrix.size()+")";
	return matrix.size()+" matrix";
}
/**
 * Returns a short string representation describing the shape of the matrix.
 */
public static String shape(AbstractMatrix2D matrix) {
	return matrix.rows()+" x "+matrix.columns()+" matrix";
}
/**
 * Returns a short string representation describing the shape of the matrix.
 */
public static String shape(AbstractMatrix3D matrix) {
	return matrix.slices()+" x "+matrix.rows()+" x "+matrix.columns()+" matrix";
}
/**
 * Returns a single string representation of the given string matrix.
 * @param strings the matrix to be converted to a single string.
 */
protected String toString(String[][] strings) {
	int rows = strings.length;
	int columns = strings.length<=0 ? 0: strings[0].length;

	StringBuffer total = new StringBuffer();
	StringBuffer s = new StringBuffer();
	for (int row=0; row<rows; row++) {
		s.setLength(0);
		for (int column=0; column<columns; column++) {
			s.append(strings[row][column]);
			if (column<columns-1) s.append(columnSeparator);
		}
		total.append(s);
		if (row<rows-1) total.append(rowSeparator);
	}

	return total.toString();
}
/**
 * Returns a string representation of the given matrix.
 * @param matrix the matrix to convert.
 */
protected String toString(AbstractMatrix2D matrix) {
	String[][] strings = this.format(matrix);
	align(strings);
	StringBuffer total = new StringBuffer(toString(strings));
	if (printShape) total.insert(0, shape(matrix) + "\n");
	return total.toString();
}
}