You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2021/12/09 16:00:13 UTC

[logging-log4j2] branch master updated: Fix NPE in SetUtils#prefixSet(). (#612)

This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new 6a94972  Fix NPE in SetUtils#prefixSet(). (#612)
6a94972 is described below

commit 6a9497243b4841448d77fce822f291db1137fd52
Author: Volkan Yazıcı <vo...@yazi.ci>
AuthorDate: Thu Dec 9 16:59:59 2021 +0100

    Fix NPE in SetUtils#prefixSet(). (#612)
---
 .../apache/logging/log4j/core/util/SetUtils.java   | 26 ++++++++++++----------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SetUtils.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SetUtils.java
index 8ca5486..e1a5218 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SetUtils.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SetUtils.java
@@ -16,30 +16,32 @@
  */
 package org.apache.logging.log4j.core.util;
 
-import java.util.HashSet;
 import java.util.Set;
 
 /**
  * Set-related convenience methods.
  */
 public final class SetUtils {
-    private SetUtils() {
-    }
+
+    private static final String[] EMPTY_STRINGS = new String[0];
+
+    private SetUtils() {}
 
     /**
-     * Extracts the Strings from a Set that start with a given prefix.
+     * Collects strings starting with the given {@code prefix} from the given {@code set}.
      *
-     * @param set a Set of Strings (assumed to all be non-{@code null})
-     * @param prefix the prefix to look for in the string set
+     * @param set a (nullable) set of strings
+     * @param prefix a prefix to look for in the string set
      * @return an array of the matching strings from the given set
      */
     public static String[] prefixSet(final Set<String> set, final String prefix) {
-        final Set<String> prefixSet = new HashSet<>();
-        for (final String str : set) {
-            if (str.startsWith(prefix)) {
-                prefixSet.add(str);
-            }
+        if (set == null) {
+            return EMPTY_STRINGS;
         }
-        return prefixSet.toArray(new String[prefixSet.size()]);
+        return set
+                .stream()
+                .filter(string -> string.startsWith(prefix))
+                .toArray(String[]::new);
     }
+
 }