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