1
14 package ch.qos.logback.classic.pattern;
15
16 import ch.qos.logback.classic.ClassicConstants;
17 import ch.qos.logback.core.CoreConstants;
18
19 public class TargetLengthBasedClassNameAbbreviator implements Abbreviator {
20
21 final int targetLength;
22
23 public TargetLengthBasedClassNameAbbreviator(int targetLength) {
24 this.targetLength = targetLength;
25 }
26
27 public String abbreviate(String fqClassName) {
28 StringBuilder buf = new StringBuilder(targetLength);
29 if (fqClassName == null) {
30 throw new IllegalArgumentException("Class name may not be null");
31 }
32
33 int inLen = fqClassName.length();
34 if (inLen < targetLength) {
35 return fqClassName;
36 }
37
38 int[] dotIndexesArray = new int[ClassicConstants.MAX_DOTS];
39
40
41 int[] lengthArray = new int[ClassicConstants.MAX_DOTS + 1];
42
43 int dotCount = computeDotIndexes(fqClassName, dotIndexesArray);
44
45
46
47
48 if (dotCount == 0) {
49 return fqClassName;
50 }
51
52 computeLengthArray(fqClassName, dotIndexesArray, lengthArray, dotCount);
53
54 for (int i = 0; i <= dotCount; i++) {
55 if (i == 0) {
56 buf.append(fqClassName.substring(0, lengthArray[i] - 1));
57 } else {
58 buf.append(fqClassName.substring(dotIndexesArray[i - 1], dotIndexesArray[i - 1] + lengthArray[i]));
59 }
60
61 }
62
63 return buf.toString();
64 }
65
66 static int computeDotIndexes(final String className, int[] dotArray) {
67 int dotCount = 0;
68 int k = 0;
69 while (true) {
70
71
72 k = className.indexOf(CoreConstants.DOT, k);
73 if (k != -1 && dotCount < ClassicConstants.MAX_DOTS) {
74 dotArray[dotCount] = k;
75 dotCount++;
76 k++;
77 } else {
78 break;
79 }
80 }
81 return dotCount;
82 }
83
84 void computeLengthArray(final String className, int[] dotArray, int[] lengthArray, int dotCount) {
85 int toTrim = className.length() - targetLength;
86
87
88
89
90 int len;
91 for (int i = 0; i < dotCount; i++) {
92 int previousDotPosition = -1;
93 if (i > 0) {
94 previousDotPosition = dotArray[i - 1];
95 }
96 int available = dotArray[i] - previousDotPosition - 1;
97
98
99 len = (available < 1) ? available : 1;
100
101
102 if (toTrim > 0) {
103 len = (available < 1) ? available : 1;
104 } else {
105 len = available;
106 }
107 toTrim -= (available - len);
108 lengthArray[i] = len + 1;
109 }
110
111 int lastDotIndex = dotCount - 1;
112 lengthArray[dotCount] = className.length() - dotArray[lastDotIndex];
113 }
114
115 static void printArray(String msg, int[] ia) {
116 System.out.print(msg);
117 for (int i = 0; i < ia.length; i++) {
118 if (i == 0) {
119 System.out.print(ia[i]);
120 } else {
121 System.out.print(", " + ia[i]);
122 }
123 }
124 System.out.println();
125 }
126 }