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/12/13 22:37:18 UTC

[logging-log4j2] branch release-2.x updated: Pushdown varargs to downstream methods.

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 fbf8e92  Pushdown varargs to downstream methods.
fbf8e92 is described below

commit fbf8e92234ac2cb4a4a5914409e72251ce796626
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Dec 13 17:37:15 2021 -0500

    Pushdown varargs to downstream methods.
    
    Slight copy improvement by reusing PathCondition.EMPTY_ARRAY when input
    is empty.
---
 .../rolling/action/IfAccumulatedFileCount.java     |  2 +-
 .../rolling/action/IfAccumulatedFileSize.java      |  2 +-
 .../core/appender/rolling/action/IfFileName.java   |  2 +-
 .../appender/rolling/action/PathCondition.java     |  4 +-
 .../appender/rolling/action/PathConditionTest.java | 43 ++++++++++++++++++++++
 5 files changed, 48 insertions(+), 5 deletions(-)

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 6813e01..1b2e2b2 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
@@ -40,7 +40,7 @@ public final class IfAccumulatedFileCount implements PathCondition {
     private int count;
     private final PathCondition[] nestedConditions;
 
-    private IfAccumulatedFileCount(final int thresholdParam, final PathCondition[] nestedConditions) {
+    private IfAccumulatedFileCount(final int thresholdParam, final PathCondition... nestedConditions) {
         if (thresholdParam <= 0) {
             throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdParam);
         }
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 220c8c5..684d3fb 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
@@ -41,7 +41,7 @@ public final class IfAccumulatedFileSize implements PathCondition {
     private long accumulatedSize;
     private final PathCondition[] nestedConditions;
 
-    private IfAccumulatedFileSize(final long thresholdSize, final PathCondition[] nestedConditions) {
+    private IfAccumulatedFileSize(final long thresholdSize, final PathCondition... nestedConditions) {
         if (thresholdSize <= 0) {
             throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdSize);
         }
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 704b790..e0f1136 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
@@ -57,7 +57,7 @@ public final class IfFileName implements PathCondition {
      * @param regex the regular expression that matches the baseDir-relative path of the file(s) to delete
      * @param nestedConditions nested conditions to evaluate if this condition accepts a path
      */
-    private IfFileName(final String glob, final String regex, final PathCondition[] nestedConditions) {
+    private IfFileName(final String glob, final String regex, final PathCondition... nestedConditions) {
         if (regex == null && glob == null) {
             throw new IllegalArgumentException("Specify either a path glob or a regular expression. "
                     + "Both cannot be null.");
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 d00a93a..f9be983 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
@@ -38,8 +38,8 @@ public interface PathCondition {
      * @param source What to copy
      * @return a copy, never null.
      */
-    static PathCondition[] copy(PathCondition[] source) {
-        return source == null ? EMPTY_ARRAY : Arrays.copyOf(source, source.length);
+    static PathCondition[] copy(PathCondition... source) {
+        return source == null || source.length == 0 ? EMPTY_ARRAY : Arrays.copyOf(source, source.length);
     }
     
     /**
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/PathConditionTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/PathConditionTest.java
new file mode 100644
index 0000000..caa5262
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/PathConditionTest.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender.rolling.action;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+
+import org.junit.jupiter.api.Test;
+
+public class PathConditionTest {
+
+    private static final PathCondition[] EMPTY_FIXTURE = {};
+    private static final PathCondition[] NULL_FIXTURE = null;
+
+    @Test
+    public void testCopy() {
+        assertArrayEquals(EMPTY_FIXTURE, PathCondition.copy(NULL_FIXTURE));
+        assertArrayEquals(EMPTY_FIXTURE, PathCondition.copy(EMPTY_FIXTURE));
+        assertArrayEquals(EMPTY_FIXTURE, PathCondition.copy(PathCondition.EMPTY_ARRAY));
+        assertSame(PathCondition.EMPTY_ARRAY, PathCondition.copy(PathCondition.EMPTY_ARRAY));
+        assertSame(PathCondition.EMPTY_ARRAY, PathCondition.copy(NULL_FIXTURE));
+        assertSame(PathCondition.EMPTY_ARRAY, PathCondition.copy(EMPTY_FIXTURE));
+        //
+        CountingCondition cc = new CountingCondition(true);
+        assertNotSame(cc, PathCondition.copy(cc));
+    }
+    
+}