You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2021/07/10 22:38:40 UTC
[logging-log4j2] branch release-2.x updated: Reduce code
duplication and memory usage by using a singleton for an empty array of
PathCondition.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new 0599af5 Reduce code duplication and memory usage by using a singleton for an empty array of PathCondition.
0599af5 is described below
commit 0599af5e4aa5c4511cb868ff1d31e13f0e251e2a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jul 10 18:38:36 2021 -0400
Reduce code duplication and memory usage by using a singleton for an
empty array of PathCondition.
---
.../core/appender/rolling/DefaultRolloverStrategy.java | 2 +-
.../appender/rolling/DirectWriteRolloverStrategy.java | 2 +-
.../appender/rolling/action/IfAccumulatedFileCount.java | 3 +--
.../appender/rolling/action/IfAccumulatedFileSize.java | 3 +--
.../log4j/core/appender/rolling/action/IfFileName.java | 3 +--
.../core/appender/rolling/action/IfLastModified.java | 3 +--
.../core/appender/rolling/action/PathCondition.java | 16 ++++++++++++++++
7 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
index a5f3dbf..458008f 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
@@ -581,7 +581,7 @@ public class DefaultRolloverStrategy extends AbstractRolloverStrategy {
.withBasePath(compressedName)
.withFollowLinks(false)
.withMaxDepth(1)
- .withPathConditions(new PathCondition[0])
+ .withPathConditions(PathCondition.EMPTY_ARRAY)
.withSubst(getStrSubstitutor())
.withFilePermissions(manager.getFilePermissions())
.withFileOwner(manager.getFileOwner())
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DirectWriteRolloverStrategy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DirectWriteRolloverStrategy.java
index 1951658..3cf72a7 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DirectWriteRolloverStrategy.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DirectWriteRolloverStrategy.java
@@ -384,7 +384,7 @@ public class DirectWriteRolloverStrategy extends AbstractRolloverStrategy implem
.withBasePath(compressedName)
.withFollowLinks(false)
.withMaxDepth(1)
- .withPathConditions(new PathCondition[0])
+ .withPathConditions(PathCondition.EMPTY_ARRAY)
.withSubst(getStrSubstitutor())
.withFilePermissions(manager.getFilePermissions())
.withFileOwner(manager.getFileOwner())
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java
index f5761bb..6813e01 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java
@@ -45,8 +45,7 @@ public final class IfAccumulatedFileCount implements PathCondition {
throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdParam);
}
this.threshold = thresholdParam;
- this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
- nestedConditions.length);
+ this.nestedConditions = PathCondition.copy(nestedConditions);
}
public int getThresholdCount() {
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java
index 6cc6ab6..220c8c5 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java
@@ -46,8 +46,7 @@ public final class IfAccumulatedFileSize implements PathCondition {
throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdSize);
}
this.thresholdBytes = thresholdSize;
- this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
- nestedConditions.length);
+ this.nestedConditions = PathCondition.copy(nestedConditions);
}
public long getThresholdBytes() {
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileName.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileName.java
index 34ee066..704b790 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileName.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileName.java
@@ -64,8 +64,7 @@ public final class IfFileName implements PathCondition {
}
this.syntaxAndPattern = createSyntaxAndPatternString(glob, regex);
this.pathMatcher = FileSystems.getDefault().getPathMatcher(syntaxAndPattern);
- this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
- nestedConditions.length);
+ this.nestedConditions = PathCondition.copy(nestedConditions);
}
static String createSyntaxAndPatternString(final String glob, final String regex) {
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java
index eff6536..ea042b0 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java
@@ -47,8 +47,7 @@ public final class IfLastModified implements PathCondition {
private IfLastModified(final Duration age, final PathCondition[] nestedConditions) {
this.age = Objects.requireNonNull(age, "age");
- this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
- nestedConditions.length);
+ this.nestedConditions = PathCondition.copy(nestedConditions);
}
public Duration getAge() {
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathCondition.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathCondition.java
index 8b8944c..5308177 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathCondition.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathCondition.java
@@ -20,13 +20,29 @@ package org.apache.logging.log4j.core.appender.rolling.action;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
+import java.util.Arrays;
/**
* Filter that accepts or rejects a candidate {@code Path} for deletion.
*/
public interface PathCondition {
+
+ /**
+ * Empty array.
+ */
+ static final PathCondition[] EMPTY_ARRAY = {};
/**
+ * Copies the given input.
+ *
+ * @param source What to copy
+ * @return a copy, never null.
+ */
+ static PathCondition[] copy(PathCondition[] source) {
+ return source == null ? new PathCondition[0] : Arrays.copyOf(source, source.length);
+ }
+
+ /**
* Invoked before a new {@linkplain Files#walkFileTree(Path, java.util.Set, int, java.nio.file.FileVisitor) file
* tree walk} is started. Stateful PathConditions can reset their state when this method is called.
*/