/apache-log4j-1.2.17/src/main/java/org/apache/log4j/pattern/FormattingInfo.java

# · Java · 134 lines · 48 code · 17 blank · 69 comment · 7 complexity · 213ad30e6a427e409ef70fe6742a95b7 MD5 · raw file

  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. package org.apache.log4j.pattern;
  18. /**
  19. * Modifies the output of a pattern converter for a specified minimum
  20. * and maximum width and alignment.
  21. *
  22. *
  23. * @author <a href=mailto:jim_cakalic@na.biomerieux.com>Jim Cakalic</a>
  24. * @author Ceki G&uuml;lc&uuml;
  25. * @author Curt Arnold
  26. *
  27. */
  28. public final class FormattingInfo {
  29. /**
  30. * Array of spaces.
  31. */
  32. private static final char[] SPACES =
  33. new char[] { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
  34. /**
  35. * Default instance.
  36. */
  37. private static final FormattingInfo DEFAULT =
  38. new FormattingInfo(false, 0, Integer.MAX_VALUE);
  39. /**
  40. * Minimum length.
  41. */
  42. private final int minLength;
  43. /**
  44. * Maximum length.
  45. */
  46. private final int maxLength;
  47. /**
  48. * Alignment.
  49. */
  50. private final boolean leftAlign;
  51. /**
  52. * Creates new instance.
  53. * @param leftAlign left align if true.
  54. * @param minLength minimum length.
  55. * @param maxLength maximum length.
  56. */
  57. public FormattingInfo(
  58. final boolean leftAlign, final int minLength, final int maxLength) {
  59. this.leftAlign = leftAlign;
  60. this.minLength = minLength;
  61. this.maxLength = maxLength;
  62. }
  63. /**
  64. * Gets default instance.
  65. * @return default instance.
  66. */
  67. public static FormattingInfo getDefault() {
  68. return DEFAULT;
  69. }
  70. /**
  71. * Determine if left aligned.
  72. * @return true if left aligned.
  73. */
  74. public boolean isLeftAligned() {
  75. return leftAlign;
  76. }
  77. /**
  78. * Get minimum length.
  79. * @return minimum length.
  80. */
  81. public int getMinLength() {
  82. return minLength;
  83. }
  84. /**
  85. * Get maximum length.
  86. * @return maximum length.
  87. */
  88. public int getMaxLength() {
  89. return maxLength;
  90. }
  91. /**
  92. * Adjust the content of the buffer based on the specified lengths and alignment.
  93. *
  94. * @param fieldStart start of field in buffer.
  95. * @param buffer buffer to be modified.
  96. */
  97. public void format(final int fieldStart, final StringBuffer buffer) {
  98. final int rawLength = buffer.length() - fieldStart;
  99. if (rawLength > maxLength) {
  100. buffer.delete(fieldStart, buffer.length() - maxLength);
  101. } else if (rawLength < minLength) {
  102. if (leftAlign) {
  103. final int fieldEnd = buffer.length();
  104. buffer.setLength(fieldStart + minLength);
  105. for (int i = fieldEnd; i < buffer.length(); i++) {
  106. buffer.setCharAt(i, ' ');
  107. }
  108. } else {
  109. int padLength = minLength - rawLength;
  110. for (; padLength > 8; padLength -= 8) {
  111. buffer.insert(fieldStart, SPACES);
  112. }
  113. buffer.insert(fieldStart, SPACES, 0, padLength);
  114. }
  115. }
  116. }
  117. }