You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2019/01/10 17:19:57 UTC
[cxf] 02/02: Make StringUtils.getFirstFound more efficient
This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 57025af84e7fd41e3a0832385adce91c086957e3
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Thu Jan 10 16:40:41 2019 +0000
Make StringUtils.getFirstFound more efficient
---
.../org/apache/cxf/common/util/StringUtils.java | 22 ++++++----------------
.../apache/cxf/common/util/StringUtilsTest.java | 8 ++++----
2 files changed, 10 insertions(+), 20 deletions(-)
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 c06db7b..48244f5 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
@@ -24,7 +24,6 @@ import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Matcher;
@@ -52,7 +51,7 @@ public final class StringUtils {
}
return true;
}
-
+
public static Predicate<String> notEmpty() {
return NOT_EMPTY;
}
@@ -72,30 +71,21 @@ public final class StringUtils {
return str1;
}
- public static List<String> getFound(String contents, String regex) {
+ public static String getFirstFound(String contents, String regex) {
if (isEmpty(regex) || isEmpty(contents)) {
return null;
}
- List<String> results = new ArrayList<>();
Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CASE);
Matcher matcher = pattern.matcher(contents);
- while (matcher.find()) {
+ if (matcher.find()) {
if (matcher.groupCount() > 0) {
- results.add(matcher.group(1));
+ return matcher.group(1);
} else {
- results.add(matcher.group());
+ return matcher.group();
}
}
- return results;
- }
-
- public static String getFirstFound(String contents, String regex) {
- List<String> founds = getFound(contents, regex);
- if (isEmpty(founds)) {
- return null;
- }
- return founds.get(0);
+ return null;
}
public static String addDefaultPortIfMissing(String urlString) {
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 5c4e452..689f51b 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
@@ -38,12 +38,12 @@ public class StringUtilsTest {
}
@Test
- public void testGetFound() throws Exception {
+ public void testGetFirstFound() throws Exception {
String regex = "velocity-\\d+\\.\\d+\\.jar";
- assertTrue(StringUtils.isEmpty(StringUtils.getFound("velocity-dep-1.4.jar", regex)));
- assertFalse(StringUtils.isEmpty(StringUtils.getFound("velocity-1.4.jar", regex)));
- assertTrue(StringUtils.isEmpty(StringUtils.getFound(null, regex)));
+ assertTrue(StringUtils.isEmpty(StringUtils.getFirstFound("velocity-dep-1.4.jar", regex)));
+ assertFalse(StringUtils.isEmpty(StringUtils.getFirstFound("velocity-1.4.jar", regex)));
+ assertTrue(StringUtils.isEmpty(StringUtils.getFirstFound(null, regex)));
}
@Test