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