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       * @param leftTruncate
64       *            truncates to the left if true
65       */
66      public FormattingInfo(final boolean leftAlign, final int minLength, final int maxLength, final boolean leftTruncate) {
67          this.leftAlign = leftAlign;
68          this.minLength = minLength;
69          this.maxLength = maxLength;
70          this.leftTruncate = leftTruncate;
71      }
72  
73      /**
74       * Gets default instance.
75       *
76       * @return default instance.
77       */
78      public static FormattingInfo getDefault() {
79          return DEFAULT;
80      }
81  
82      /**
83       * Determine if left aligned.
84       *
85       * @return true if left aligned.
86       */
87      public boolean isLeftAligned() {
88          return leftAlign;
89      }
90  
91      /**
92       * Determine if left truncated.
93       *
94       * @return true if left truncated.
95       */
96      public boolean isLeftTruncate() {
97  		return leftTruncate;
98  	}
99  
100     /**
101      * Get minimum length.
102      *
103      * @return minimum length.
104      */
105     public int getMinLength() {
106         return minLength;
107     }
108 
109     /**
110      * Get maximum length.
111      *
112      * @return maximum length.
113      */
114     public int getMaxLength() {
115         return maxLength;
116     }
117 
118     /**
119      * Adjust the content of the buffer based on the specified lengths and alignment.
120      *
121      * @param fieldStart
122      *            start of field in buffer.
123      * @param buffer
124      *            buffer to be modified.
125      */
126     public void format(final int fieldStart, final StringBuilder buffer) {
127         final int rawLength = buffer.length() - fieldStart;
128 
129         if (rawLength > maxLength) {
130 			if (leftTruncate) {
131 				buffer.delete(fieldStart, buffer.length() - maxLength);
132 			} else {
133 				buffer.delete(fieldStart + maxLength, fieldStart + buffer.length());
134 			}
135         } else if (rawLength < minLength) {
136             if (leftAlign) {
137                 final int fieldEnd = buffer.length();
138                 buffer.setLength(fieldStart + minLength);
139 
140                 for (int i = fieldEnd; i < buffer.length(); i++) {
141                     buffer.setCharAt(i, ' ');
142                 }
143             } else {
144                 int padLength = minLength - rawLength;
145 
146                 for (; padLength > SPACES.length; padLength -= SPACES.length) {
147                     buffer.insert(fieldStart, SPACES);
148                 }
149 
150                 buffer.insert(fieldStart, SPACES, 0, padLength);
151             }
152         }
153     }
154 
155     /**
156      * Returns a String suitable for debugging.
157      *
158      * @return a String suitable for debugging.
159      */
160     @Override
161     public String toString() {
162         final StringBuilder sb = new StringBuilder();
163         sb.append(super.toString());
164         sb.append("[leftAlign=");
165         sb.append(leftAlign);
166         sb.append(", maxLength=");
167         sb.append(maxLength);
168         sb.append(", minLength=");
169         sb.append(minLength);
170         sb.append(", leftTruncate=");
171         sb.append(leftTruncate);
172         sb.append(']');
173         return sb.toString();
174     }
175 
176 }