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