View Javadoc
1   /*
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
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
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.
16   */
17  
18  package org.apache.logging.log4j.core.pattern;
19  
20  import org.apache.logging.log4j.util.PerformanceSensitive;
21  
22  /**
23   * Modifies the output of a pattern converter for a specified minimum and maximum width and alignment.
24   */
25  @PerformanceSensitive("allocation")
26  public final class FormattingInfo {
27      /**
28       * Array of spaces.
29       */
30      private static final char[] SPACES = new char[] { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
31  
32      /**
33       * Default instance.
34       */
35      private static final FormattingInfo DEFAULT = new FormattingInfo(false, 0, Integer.MAX_VALUE, true);
36  
37      /**
38       * Minimum length.
39       */
40      private final int minLength;
41  
42      /**
43       * Maximum length.
44       */
45      private final int maxLength;
46  
47      /**
48       * Alignment.
49       */
50      private final boolean leftAlign;
51  
52      /**
53       * Left vs. right-hand side truncation.
54       */
55      private final boolean leftTruncate;
56  
57      /**
58       * Creates new instance.
59       *
60       * @param leftAlign
61       *            left align if true.
62       * @param minLength
63       *            minimum length.
64       * @param maxLength
65       *            maximum length.
66       * @param leftTruncate
67       *            truncates to the left if true
68       */
69      public FormattingInfo(final boolean leftAlign, final int minLength, final int maxLength, final boolean leftTruncate) {
70          this.leftAlign = leftAlign;
71          this.minLength = minLength;
72          this.maxLength = maxLength;
73          this.leftTruncate = leftTruncate;
74      }
75  
76      /**
77       * Gets default instance.
78       *
79       * @return default instance.
80       */
81      public static FormattingInfo getDefault() {
82          return DEFAULT;
83      }
84  
85      /**
86       * Determine if left aligned.
87       *
88       * @return true if left aligned.
89       */
90      public boolean isLeftAligned() {
91          return leftAlign;
92      }
93  
94      /**
95       * Determine if left truncated.
96       *
97       * @return true if left truncated.
98       */
99      public boolean isLeftTruncate() {
100 		return leftTruncate;
101 	}
102 
103     /**
104      * Get minimum length.
105      *
106      * @return minimum length.
107      */
108     public int getMinLength() {
109         return minLength;
110     }
111 
112     /**
113      * Get maximum length.
114      *
115      * @return maximum length.
116      */
117     public int getMaxLength() {
118         return maxLength;
119     }
120 
121     /**
122      * Adjust the content of the buffer based on the specified lengths and alignment.
123      *
124      * @param fieldStart
125      *            start of field in buffer.
126      * @param buffer
127      *            buffer to be modified.
128      */
129     public void format(final int fieldStart, final StringBuilder buffer) {
130         final int rawLength = buffer.length() - fieldStart;
131 
132         if (rawLength > maxLength) {
133 			if (leftTruncate) {
134 				buffer.delete(fieldStart, buffer.length() - maxLength);
135 			} else {
136 				buffer.delete(fieldStart + maxLength, fieldStart + buffer.length());
137 			}
138         } else if (rawLength < minLength) {
139             if (leftAlign) {
140                 final int fieldEnd = buffer.length();
141                 buffer.setLength(fieldStart + minLength);
142 
143                 for (int i = fieldEnd; i < buffer.length(); i++) {
144                     buffer.setCharAt(i, ' ');
145                 }
146             } else {
147                 int padLength = minLength - rawLength;
148 
149                 for (; padLength > SPACES.length; padLength -= SPACES.length) {
150                     buffer.insert(fieldStart, SPACES);
151                 }
152 
153                 buffer.insert(fieldStart, SPACES, 0, padLength);
154             }
155         }
156     }
157 
158     /**
159      * Returns a String suitable for debugging.
160      *
161      * @return a String suitable for debugging.
162      */
163     @Override
164     public String toString() {
165         final StringBuilder sb = new StringBuilder();
166         sb.append(super.toString());
167         sb.append("[leftAlign=");
168         sb.append(leftAlign);
169         sb.append(", maxLength=");
170         sb.append(maxLength);
171         sb.append(", minLength=");
172         sb.append(minLength);
173         sb.append(", leftTruncate=");
174         sb.append(leftTruncate);
175         sb.append(']');
176         return sb.toString();
177     }
178 
179 }