01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.text;
05:
06: public class StringFormatter {
07:
08: private final String nl;
09:
10: public StringFormatter() {
11: nl = System.getProperty("line.separator");
12: }
13:
14: public String newline() {
15: return nl;
16: }
17:
18: public String leftPad(int size, Object s) {
19: return pad(false, size, s);
20: }
21:
22: public String leftPad(int size, int i) {
23: return leftPad(size, "" + i);
24: }
25:
26: public String rightPad(int size, Object s) {
27: return pad(true, size, s);
28: }
29:
30: public String rightPad(int size, int i) {
31: return rightPad(size, "" + i);
32: }
33:
34: private String pad(boolean right, int size, Object s) {
35: StringBuffer buf = new StringBuffer();
36: buf.append(s);
37: while (buf.length() < size) {
38: if (right)
39: buf.append(" ");
40: else
41: buf.insert(0, " ");
42: }
43: while (buf.length() > size) {
44: buf.deleteCharAt(buf.length() - 1);
45: if (buf.length() == size) {
46: buf.deleteCharAt(buf.length() - 1).append("~");
47: }
48: }
49: return buf.toString();
50: }
51:
52: }
|