-
Notifications
You must be signed in to change notification settings - Fork 3
/
StringAlign.java
86 lines (81 loc) · 2.17 KB
/
StringAlign.java
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
package chapter3;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
public class StringAlign extends Format {
private static final long serialVersionUID = 1L;
public enum Justify {
/* Constant for left justification. */
LEFT,
/* Constant for centering. */
CENTER,
/** Constant for right-justified Strings. */
RIGHT,
}
/** Current justification */
private Justify just;
/** Current max lengths */
private int maxChars;
/** Construct a StringAlign formatter; length and alignment are
* passed to the Constructor instead of each format() call as the
* expected common use is in repetitive formatting e.g., page numbers.
* @param maxChars - the maximum length of the output
* @param just - one of the enum values LEFT, CENTER or RIGHT
*/
public StringAlign(int maxChars, Justify just) {
switch(just) {
case LEFT:
case CENTER:
case RIGHT:
this.just = just;
break;
default:
throw new IllegalArgumentException("invalid justification arg.");
}
if (maxChars < 0) {
throw new IllegalArgumentException("maxChars must be positive.");
}
this.maxChars = maxChars;
}
/** Format a String.
* @param input - the string to be aligned.
* @parm where - the StringBuffer to append it to.
* @param ignore - a FieldPosition (may be null, not used but
* specified by the general contract of Format).
*/
public StringBuffer format(
Object input, StringBuffer where, FieldPosition ignore) {
String s = input.toString();
String wanted = s.substring(0, Math.min(s.length(), maxChars));
// Get the spaces in the right place.
switch (just) {
case RIGHT:
pad(where, maxChars - wanted.length());
where.append(wanted);
break;
case CENTER:
int toAdd = maxChars - wanted.length();
pad(where, toAdd/2);
where.append(wanted);
pad(where, toAdd - toAdd/2);
break;
case LEFT:
where.append(wanted);
pad(where, maxChars - wanted.length());
break;
}
return where;
}
protected final void pad(StringBuffer to, int howMany) {
for (int i=0; i<howMany; i++)
to.append(' ');
}
/** Convenience Routine */
String format(String s) {
return format(s, new StringBuffer(), null).toString();
}
/** ParseObject is required, but not useful here. */
public Object parseObject (String source, ParsePosition pos) {
return source;
}
}