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.log4j.pattern; 019 020 021/** 022 * Modifies the output of a pattern converter for a specified minimum 023 * and maximum width and alignment. 024 * 025 * 026 * @author <a href=mailto:jim_cakalic@na.biomerieux.com>Jim Cakalic</a> 027 * @author Ceki Gülcü 028 * @author Curt Arnold 029 * 030 */ 031public final class FormattingInfo { 032 /** 033 * Array of spaces. 034 */ 035 private static final char[] SPACES = 036 new char[] { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }; 037 038 /** 039 * Default instance. 040 */ 041 private static final FormattingInfo DEFAULT = 042 new FormattingInfo(false, 0, Integer.MAX_VALUE); 043 044 /** 045 * Minimum length. 046 */ 047 private final int minLength; 048 049 /** 050 * Maximum length. 051 */ 052 private final int maxLength; 053 054 /** 055 * Alignment. 056 */ 057 private final boolean leftAlign; 058 059 /** 060 * Creates new instance. 061 * @param leftAlign left align if true. 062 * @param minLength minimum length. 063 * @param maxLength maximum length. 064 */ 065 public FormattingInfo( 066 final boolean leftAlign, final int minLength, final int maxLength) { 067 this.leftAlign = leftAlign; 068 this.minLength = minLength; 069 this.maxLength = maxLength; 070 } 071 072 /** 073 * Gets default instance. 074 * @return default instance. 075 */ 076 public static FormattingInfo getDefault() { 077 return DEFAULT; 078 } 079 080 /** 081 * Determine if left aligned. 082 * @return true if left aligned. 083 */ 084 public boolean isLeftAligned() { 085 return leftAlign; 086 } 087 088 /** 089 * Get minimum length. 090 * @return minimum length. 091 */ 092 public int getMinLength() { 093 return minLength; 094 } 095 096 /** 097 * Get maximum length. 098 * @return maximum length. 099 */ 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}