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