1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.pattern;
19
20
21
22
23
24
25
26
27
28
29
30
31 public final class FormattingInfo {
32
33
34
35 private static final char[] SPACES =
36 new char[] { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
37
38
39
40
41 private static final FormattingInfo DEFAULT =
42 new FormattingInfo(false, 0, Integer.MAX_VALUE);
43
44
45
46
47 private final int minLength;
48
49
50
51
52 private final int maxLength;
53
54
55
56
57 private final boolean leftAlign;
58
59
60
61
62
63
64
65 public FormattingInfo(
66 final boolean leftAlign, final int minLength, final int maxLength) {
67 this.leftAlign = leftAlign;
68 this.minLength = minLength;
69 this.maxLength = maxLength;
70 }
71
72
73
74
75
76 public static FormattingInfo getDefault() {
77 return DEFAULT;
78 }
79
80
81
82
83
84 public boolean isLeftAligned() {
85 return leftAlign;
86 }
87
88
89
90
91
92 public int getMinLength() {
93 return minLength;
94 }
95
96
97
98
99
100 public int getMaxLength() {
101 return maxLength;
102 }
103
104
105
106
107
108
109
110 public void format(final int fieldStart, final StringBuffer buffer) {
111 final int rawLength = buffer.length() - fieldStart;
112
113 if (rawLength > maxLength) {
114 buffer.delete(fieldStart, buffer.length() - maxLength);
115 } else if (rawLength < minLength) {
116 if (leftAlign) {
117 final int fieldEnd = buffer.length();
118 buffer.setLength(fieldStart + minLength);
119
120 for (int i = fieldEnd; i < buffer.length(); i++) {
121 buffer.setCharAt(i, ' ');
122 }
123 } else {
124 int padLength = minLength - rawLength;
125
126 for (; padLength > 8; padLength -= 8) {
127 buffer.insert(fieldStart, SPACES);
128 }
129
130 buffer.insert(fieldStart, SPACES, 0, padLength);
131 }
132 }
133 }
134 }