FormattingInfo xref

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.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     * Creates new instance.
    61     * @param leftAlign left align if true.
    62     * @param minLength minimum length.
    63     * @param maxLength maximum length.
    64     */
    65    public FormattingInfo(
    66      final boolean leftAlign, final int minLength, final int maxLength) {
    67      this.leftAlign = leftAlign;
    68      this.minLength = minLength;
    69      this.maxLength = maxLength;
    70    }
    71
    72    /**
    73     * Gets default instance.
    74     * @return default instance.
    75     */
    76    public static FormattingInfo getDefault() {
    77      return DEFAULT;
    78    }
    79
    80    /**
    81     * Determine if left aligned.
    82     * @return true if left aligned.
    83     */
    84    public boolean isLeftAligned() {
    85      return leftAlign;
    86    }
    87
    88    /**
    89     * Get minimum length.
    90     * @return minimum length.
    91     */
    92    public int getMinLength() {
    93      return minLength;
    94    }
    95
    96    /**
    97     * Get maximum length.
    98     * @return maximum length.
    99     */
    100   public int getMaxLength() {
    101     return maxLength;
    102   }
    103
    104   /**
    105    * Adjust the content of the buffer based on the specified lengths and alignment.
    106    *
    107    * @param fieldStart start of field in buffer.
    108    * @param buffer buffer to be modified.
    109    */
    110   public void format(final int fieldStart, final StringBuffer buffer) {
    111     final int rawLength = buffer.length() - fieldStart;
    112
    113     if (rawLength > maxLength) {
    114       buffer.delete(fieldStart, buffer.length() - maxLength);
    115     } else if (rawLength < minLength) {
    116       if (leftAlign) {
    117         final int fieldEnd = buffer.length();
    118         buffer.setLength(fieldStart + minLength);
    119
    120         for (int i = fieldEnd; i < buffer.length(); i++) {
    121           buffer.setCharAt(i, ' ');
    122         }
    123       } else {
    124         int padLength = minLength - rawLength;
    125
    126         for (; padLength > 8; padLength -= 8) {
    127           buffer.insert(fieldStart, SPACES);
    128         }
    129
    130         buffer.insert(fieldStart, SPACES, 0, padLength);
    131       }
    132     }
    133   }
    134 }