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