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  /**
22   * Modifies the output of a pattern converter for a specified minimum
23   * and maximum width and alignment.
24   */
25  public final class FormattingInfo {
26      /**
27       * Array of spaces.
28       */
29      private static final char[] SPACES =
30          new char[]{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
31  
32      /**
33       * Default instance.
34       */
35      private static final FormattingInfo DEFAULT =
36          new FormattingInfo(false, 0, Integer.MAX_VALUE);
37  
38      /**
39       * Minimum length.
40       */
41      private final int minLength;
42  
43      /**
44       * Maximum length.
45       */
46      private final int maxLength;
47  
48      /**
49       * Alignment.
50       */
51      private final boolean leftAlign;
52  
53      /**
54       * Creates new instance.
55       *
56       * @param leftAlign left align if true.
57       * @param minLength minimum length.
58       * @param maxLength maximum length.
59       */
60      public FormattingInfo(final boolean leftAlign, final int minLength, final int maxLength) {
61          this.leftAlign = leftAlign;
62          this.minLength = minLength;
63          this.maxLength = maxLength;
64      }
65  
66      /**
67       * Gets default instance.
68       *
69       * @return default instance.
70       */
71      public static FormattingInfo getDefault() {
72          return DEFAULT;
73      }
74  
75      /**
76       * Determine if left aligned.
77       *
78       * @return true if left aligned.
79       */
80      public boolean isLeftAligned() {
81          return leftAlign;
82      }
83  
84      /**
85       * Get minimum length.
86       *
87       * @return minimum length.
88       */
89      public int getMinLength() {
90          return minLength;
91      }
92  
93      /**
94       * Get maximum length.
95       *
96       * @return maximum length.
97       */
98      public int getMaxLength() {
99          return maxLength;
100     }
101 
102     /**
103      * Adjust the content of the buffer based on the specified lengths and alignment.
104      *
105      * @param fieldStart start of field in buffer.
106      * @param buffer     buffer to be modified.
107      */
108     public void format(final int fieldStart, final StringBuilder buffer) {
109         final int rawLength = buffer.length() - fieldStart;
110 
111         if (rawLength > maxLength) {
112             buffer.delete(fieldStart, buffer.length() - maxLength);
113         } else if (rawLength < minLength) {
114             if (leftAlign) {
115                 final int fieldEnd = buffer.length();
116                 buffer.setLength(fieldStart + minLength);
117 
118                 for (int i = fieldEnd; i < buffer.length(); i++) {
119                     buffer.setCharAt(i, ' ');
120                 }
121             } else {
122                 int padLength = minLength - rawLength;
123 
124                 for (; padLength > SPACES.length; padLength -= SPACES.length) {
125                     buffer.insert(fieldStart, SPACES);
126                 }
127 
128                 buffer.insert(fieldStart, SPACES, 0, padLength);
129             }
130         }
131     }
132 }