You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2016/06/18 04:49:47 UTC
logging-log4j2 git commit: Refactor some more,
inspired by Gary: use Strategy design pattern and delegate to
Strategy instance.
Repository: logging-log4j2
Updated Branches:
refs/heads/master c80735fad -> db47cde3d
Refactor some more, inspired by Gary: use Strategy design pattern and delegate to Strategy instance.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/db47cde3
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/db47cde3
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/db47cde3
Branch: refs/heads/master
Commit: db47cde3dee34601accbb6d6e23c04b07560bb25
Parents: c80735f
Author: rpopma <rp...@apache.org>
Authored: Sat Jun 18 13:49:47 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Sat Jun 18 13:49:47 2016 +0900
----------------------------------------------------------------------
.../log4j/core/pattern/NameAbbreviator.java | 97 +++++++++-----------
1 file changed, 42 insertions(+), 55 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/db47cde3/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/NameAbbreviator.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/NameAbbreviator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/NameAbbreviator.java
index a0eaa1b..bb43d9e 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/NameAbbreviator.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/NameAbbreviator.java
@@ -142,7 +142,6 @@ public abstract class NameAbbreviator {
*
* @param original the text to abbreviate, may not be null.
* @param destination StringBuilder to write the result to
- * @return The abbreviated String.
*/
public abstract void abbreviate(final String original, final StringBuilder destination);
@@ -176,23 +175,48 @@ public abstract class NameAbbreviator {
* 2. Retain a given number of parts starting from the end - called RETAIN
*/
private enum Strategy {
-
- DROP {
- @Override
- int getMinCount() {
- return 0;
+ DROP(0) {
+ void abbreviate(final int count, final String original, final StringBuilder destination) {
+ // If a path does not contain enough path elements to drop, none will be dropped.
+ int start = 0;
+ int nextStart;
+ for (int i = 0; i < count; i++) {
+ nextStart = original.indexOf('.', start);
+ if (nextStart == -1) {
+ destination.append(original);
+ return;
+ }
+ start = nextStart + 1;
+ }
+ destination.append(original, start, original.length());
}
},
- RETAIN {
- @Override
- int getMinCount() {
- return 1;
+ RETAIN(1) {
+ void abbreviate(final int count, final String original, final StringBuilder destination) {
+ // We subtract 1 from 'len' when assigning to 'end' to avoid out of
+ // bounds exception in return r.substring(end+1, len). This can happen if
+ // precision is 1 and the category name ends with a dot.
+ int end = original.length() - 1;
+
+ for (int i = count; i > 0; i--) {
+ end = original.lastIndexOf('.', end - 1);
+ if (end == -1) {
+ destination.append(original);
+ return;
+ }
+ }
+ destination.append(original, end + 1, original.length());
}
};
-
- abstract int getMinCount();
- };
+ final int minCount;
+
+ Strategy(final int minCount) {
+ this.minCount = minCount;
+ }
+
+ abstract void abbreviate(final int count, final String original, final StringBuilder destination);
+ }
/**
* Maximum number of path elements to output.
@@ -211,8 +235,7 @@ public abstract class NameAbbreviator {
* @param strategy drop or retain
*/
public MaxElementAbbreviator(final int count, final Strategy strategy) {
- final int minCount = strategy.getMinCount();
- this.count = count < minCount ? minCount : count;
+ this.count = Math.max(count, strategy.minCount);
this.strategy = strategy;
}
@@ -220,47 +243,11 @@ public abstract class NameAbbreviator {
* Abbreviate name.
*
* @param original The String to abbreviate.
- * @param destination
- * @return the abbreviated String.
+ * @param destination the buffer to write the abbreviated name into
*/
@Override
public void abbreviate(final String original, final StringBuilder destination) {
- if (Strategy.DROP == strategy) {
- abbreviateForDrop(original, destination);
- } else { // Strategy.RETAIN
- abbreviateForRetain(original, destination);
- }
- }
-
- private void abbreviateForDrop(final String original, final StringBuilder destination) {
- // If a path does not contain enough path elements to drop, none will be dropped.
- int start = 0;
- int nextStart = 0;
- for (int i = 0; i < count; i++) {
- nextStart = original.indexOf('.', start);
- if (nextStart == -1) {
- destination.append(original);
- return;
- }
- start = nextStart + 1;
- }
- destination.append(original, start, original.length());
- }
-
- private void abbreviateForRetain(final String original, final StringBuilder destination) {
- // We subtract 1 from 'len' when assigning to 'end' to avoid out of
- // bounds exception in return r.substring(end+1, len). This can happen if
- // precision is 1 and the category name ends with a dot.
- int end = original.length() - 1;
-
- for (int i = count; i > 0; i--) {
- end = original.lastIndexOf('.', end - 1);
- if (end == -1) {
- destination.append(original);
- return;
- }
- }
- destination.append(original, end + 1, original.length());
+ strategy.abbreviate(count, original, destination);
}
}
@@ -352,8 +339,8 @@ public abstract class NameAbbreviator {
/**
* Abbreviates name.
*
- * @param original buffer that abbreviated name is appended.
- * @param destination
+ * @param original the original string to abbreviate
+ * @param destination buffer that abbreviated name is appended to
*/
@Override
public void abbreviate(final String original, final StringBuilder destination) {