001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017
018package org.apache.logging.log4j.core.pattern;
019
020/**
021 * Modifies the output of a pattern converter for a specified minimum and maximum width and alignment.
022 */
023public final class FormattingInfo {
024    /**
025     * Array of spaces.
026     */
027    private static final char[] SPACES = new char[] { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
028
029    /**
030     * Default instance.
031     */
032    private static final FormattingInfo DEFAULT = new FormattingInfo(false, 0, Integer.MAX_VALUE);
033
034    /**
035     * Minimum length.
036     */
037    private final int minLength;
038
039    /**
040     * Maximum length.
041     */
042    private final int maxLength;
043
044    /**
045     * Alignment.
046     */
047    private final boolean leftAlign;
048
049    /**
050     * Creates new instance.
051     * 
052     * @param leftAlign
053     *            left align if true.
054     * @param minLength
055     *            minimum length.
056     * @param maxLength
057     *            maximum length.
058     */
059    public FormattingInfo(final boolean leftAlign, final int minLength, final int maxLength) {
060        this.leftAlign = leftAlign;
061        this.minLength = minLength;
062        this.maxLength = maxLength;
063    }
064
065    /**
066     * Gets default instance.
067     * 
068     * @return default instance.
069     */
070    public static FormattingInfo getDefault() {
071        return DEFAULT;
072    }
073
074    /**
075     * Determine if left aligned.
076     * 
077     * @return true if left aligned.
078     */
079    public boolean isLeftAligned() {
080        return leftAlign;
081    }
082
083    /**
084     * Get minimum length.
085     * 
086     * @return minimum length.
087     */
088    public int getMinLength() {
089        return minLength;
090    }
091
092    /**
093     * Get maximum length.
094     * 
095     * @return maximum length.
096     */
097    public int getMaxLength() {
098        return maxLength;
099    }
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        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}