You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by GitBox <gi...@apache.org> on 2018/10/24 07:17:09 UTC

[GitHub] amarkevich closed pull request #465: StringUtils: lazy Pattern evaluation

amarkevich closed pull request #465: StringUtils: lazy Pattern evaluation
URL: https://github.com/apache/cxf/pull/465
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/org/apache/cxf/common/util/StringUtils.java b/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
index 762b017714a..1a964e16ce5 100644
--- a/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
@@ -29,13 +29,14 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.function.Predicate;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Stream;
 
 public final class StringUtils {
-    public static final Map<String, Pattern> PATTERN_MAP = new HashMap<>();
+    private static final Map<String, Pattern> PATTERN_MAP = new HashMap<>();
     static {
         String patterns[] = {"/", " ", ":", ",", ";", "=", "\\.", "\\+"};
         for (String p : patterns) {
@@ -51,13 +52,13 @@ private StringUtils() {
         return split(s, regex, 0);
     }
     public static String[] split(String s, String regex, int limit) {
-        Pattern p = PATTERN_MAP.getOrDefault(regex, Pattern.compile(regex));
-        return p.split(s, limit);
+        return getPattern(regex).split(s, limit);
     }
-    
     public static Stream<String> splitAsStream(String s, String regex) {
-        Pattern p = PATTERN_MAP.getOrDefault(regex, Pattern.compile(regex));
-        return p.splitAsStream(s);
+        return getPattern(regex).splitAsStream(s);
+    }
+    static Pattern getPattern(String regex) {
+        return Optional.ofNullable(PATTERN_MAP.get(regex)).orElseGet(() -> Pattern.compile(regex));
     }
 
     public static boolean isFileExist(String file) {
diff --git a/core/src/test/java/org/apache/cxf/common/util/StringUtilsTest.java b/core/src/test/java/org/apache/cxf/common/util/StringUtilsTest.java
index a346e9635c3..74191de93e6 100644
--- a/core/src/test/java/org/apache/cxf/common/util/StringUtilsTest.java
+++ b/core/src/test/java/org/apache/cxf/common/util/StringUtilsTest.java
@@ -73,13 +73,19 @@ public void testGetPartsWithManySpaces() throws Exception {
     @Test
     public void testSplitWithDot() throws Exception {
         String str = "a.b.c";
-        String[] parts = StringUtils.split(str, "\\.", -1);
+        String[] parts = StringUtils.split(str, "\\.");
         assertEquals(3, parts.length);
         assertEquals("a", parts[0]);
         assertEquals("b", parts[1]);
         assertEquals("c", parts[2]);
     }
 
+    @Test
+    public void testGetPattern() throws Exception {
+        assertSame(StringUtils.getPattern("\\."), StringUtils.getPattern("\\."));
+        assertNotSame(StringUtils.getPattern("123"), StringUtils.getPattern("123"));
+    }
+
     @Test
     public void testGetFound() throws Exception {
         String regex = "velocity-\\d+\\.\\d+\\.jar";


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services