2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.log4j.pattern;
22 * Modifies the output of a pattern converter for a specified minimum
23 * and maximum width and alignment.
26 * @author <a href=mailto:jim_cakalic@na.biomerieux.com>Jim Cakalic</a>
27 * @author Ceki Gülcü
31 public final class FormattingInfo {
35 private static final char[] SPACES =
36 new char[] { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
41 private static final FormattingInfo DEFAULT =
42 new FormattingInfo(false, 0, Integer.MAX_VALUE);
47 private final int minLength;
52 private final int maxLength;
57 private final boolean leftAlign;
63 private final boolean rightTruncate;
66 * Creates new instance.
67 * @param leftAlign left align if true.
68 * @param minLength minimum length.
69 * @param maxLength maximum length.
70 * @deprecated since 1.2.17
72 public FormattingInfo(
73 final boolean leftAlign,
75 final int maxLength) {
76 this.leftAlign = leftAlign;
77 this.minLength = minLength;
78 this.maxLength = maxLength;
79 this.rightTruncate = false;
83 * Creates new instance.
84 * @param leftAlign left align if true.
85 * @param rightTruncate right truncate if true.
86 * @param minLength minimum length.
87 * @param maxLength maximum length.
90 public FormattingInfo(
91 final boolean leftAlign,
92 final boolean rightTruncate,
94 final int maxLength) {
95 this.leftAlign = leftAlign;
96 this.minLength = minLength;
97 this.maxLength = maxLength;
98 this.rightTruncate = rightTruncate;
102 * Gets default instance.
103 * @return default instance.
105 public static FormattingInfo getDefault() {
110 * Determine if left aligned.
111 * @return true if left aligned.
113 public boolean isLeftAligned() {
118 * Determine if right truncated.
119 * @return true if right truncated.
122 public boolean isRightTruncated() {
123 return rightTruncate;
127 * Get minimum length.
128 * @return minimum length.
130 public int getMinLength() {
135 * Get maximum length.
136 * @return maximum length.
138 public int getMaxLength() {
143 * Adjust the content of the buffer based on the specified lengths and alignment.
145 * @param fieldStart start of field in buffer.
146 * @param buffer buffer to be modified.
148 public void format(final int fieldStart, final StringBuffer buffer) {
149 final int rawLength = buffer.length() - fieldStart;
151 if (rawLength > maxLength) {
153 buffer.setLength(fieldStart + maxLength);
155 buffer.delete(fieldStart, buffer.length() - maxLength);
157 } else if (rawLength < minLength) {
159 final int fieldEnd = buffer.length();
160 buffer.setLength(fieldStart + minLength);
162 for (int i = fieldEnd; i < buffer.length(); i++) {
163 buffer.setCharAt(i, ' ');
166 int padLength = minLength - rawLength;
168 for (; padLength > 8; padLength -= 8) {
169 buffer.insert(fieldStart, SPACES);
172 buffer.insert(fieldStart, SPACES, 0, padLength);