This is an automated email from the ASF dual-hosted git repository.
iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new aed0102 Simplify the code of StringUtils to make it more graceful.
(#2740)
aed0102 is described below
commit aed0102f0a5a1d5d795bbe4589562d4346f41e70
Author: 田小波 <[email protected]>
AuthorDate: Tue Nov 6 17:09:00 2018 +0800
Simplify the code of StringUtils to make it more graceful. (#2740)
* Simplify the code of StringUtils to make it more graceful.
* Add Apache license
---
.../org/apache/dubbo/common/utils/ArrayUtils.java | 47 ++++++++
.../org/apache/dubbo/common/utils/StringUtils.java | 118 +++++++++++++--------
.../apache/dubbo/common/utils/ArrayUtilsTest.java | 41 +++++++
.../apache/dubbo/common/utils/StringUtilsTest.java | 30 +++++-
4 files changed, 191 insertions(+), 45 deletions(-)
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ArrayUtils.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ArrayUtils.java
new file mode 100644
index 0000000..a0b62bd
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ArrayUtils.java
@@ -0,0 +1,47 @@
+/*
+ * 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.dubbo.common.utils;
+
+/**
+ * Contains some methods to check array.
+ */
+public final class ArrayUtils {
+
+ private ArrayUtils() {
+ }
+
+ /**
+ * <p>Checks if the array is null or empty. <p/>
+ *
+ * @param array th array to check
+ * @return {@code true} if the array is null or empty.
+ */
+ public static boolean isEmpty(final Object[] array) {
+ return array == null || array.length == 0;
+ }
+
+ /**
+ * <p>Checks if the array is not null or empty. <p/>
+ *
+ * @param array th array to check
+ * @return {@code true} if the array is not null or empty.
+ */
+ public static boolean isNotEmpty(final Object[] array) {
+ return !isEmpty(array);
+ }
+}
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java
index c2c708f..f599218 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java
@@ -174,7 +174,7 @@ public final class StringUtils {
* {@code null} if null String input
*/
public static String removeEnd(final String str, final String remove) {
- if (isEmpty(str) || isEmpty(remove)) {
+ if (isAnyEmpty(str, remove)) {
return str;
}
if (str.endsWith(remove)) {
@@ -314,7 +314,7 @@ public final class StringUtils {
* {@code null} if null String input
*/
public static String replace(final String text, final String searchString,
final String replacement, int max) {
- if (isEmpty(text) || isEmpty(searchString) || replacement == null ||
max == 0) {
+ if (isAnyEmpty(text, searchString) || replacement == null || max == 0)
{
return text;
}
int start = 0;
@@ -340,10 +340,7 @@ public final class StringUtils {
}
public static boolean isBlank(String str) {
- if (str == null || str.length() == 0) {
- return true;
- }
- return false;
+ return isEmpty(str);
}
/**
@@ -353,10 +350,57 @@ public final class StringUtils {
* @return is empty.
*/
public static boolean isEmpty(String str) {
- if (str == null || str.length() == 0) {
- return true;
+ return str == null || str.isEmpty();
+ }
+
+ /**
+ * <p>Checks if the strings contain empty or null elements. <p/>
+ *
+ * <pre>
+ * StringUtils.isNoneEmpty(null) = false
+ * StringUtils.isNoneEmpty("") = false
+ * StringUtils.isNoneEmpty(" ") = true
+ * StringUtils.isNoneEmpty("abc") = true
+ * StringUtils.isNoneEmpty("abc", "def") = true
+ * StringUtils.isNoneEmpty("abc", null) = false
+ * StringUtils.isNoneEmpty("abc", "") = false
+ * StringUtils.isNoneEmpty("abc", " ") = true
+ * </pre>
+ *
+ * @param ss the strings to check
+ * @return {@code true} if all strings are not empty or null
+ */
+ public static boolean isNoneEmpty(final String... ss) {
+ if (ArrayUtils.isEmpty(ss)) {
+ return false;
}
- return false;
+ for (final String s : ss){
+ if (isEmpty(s)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * <p>Checks if the strings contain at least on empty or null element. <p/>
+ *
+ * <pre>
+ * StringUtils.isAnyEmpty(null) = true
+ * StringUtils.isAnyEmpty("") = true
+ * StringUtils.isAnyEmpty(" ") = false
+ * StringUtils.isAnyEmpty("abc") = false
+ * StringUtils.isAnyEmpty("abc", "def") = false
+ * StringUtils.isAnyEmpty("abc", null) = true
+ * StringUtils.isAnyEmpty("abc", "") = true
+ * StringUtils.isAnyEmpty("abc", " ") = false
+ * </pre>
+ *
+ * @param ss the strings to check
+ * @return {@code true} if at least one in the strings is empty or null
+ */
+ public static boolean isAnyEmpty(final String... ss) {
+ return !isNoneEmpty(ss);
}
/**
@@ -366,7 +410,7 @@ public final class StringUtils {
* @return is not empty.
*/
public static boolean isNotEmpty(String str) {
- return str != null && str.length() > 0;
+ return !isEmpty(str);
}
/**
@@ -391,17 +435,11 @@ public final class StringUtils {
* @return is integer
*/
public static boolean isInteger(String str) {
- if (str == null || str.length() == 0) {
- return false;
- }
- return INT_PATTERN.matcher(str).matches();
+ return isNotEmpty(str) && INT_PATTERN.matcher(str).matches();
}
public static int parseInteger(String str) {
- if (!isInteger(str)) {
- return 0;
- }
- return Integer.parseInt(str);
+ return isInteger(str) ? Integer.parseInt(str) : 0;
}
/**
@@ -409,7 +447,7 @@ public final class StringUtils {
* <a href="http://www.exampledepot.com/egs/java.lang/IsJavaId.html">more
info.</a>
*/
public static boolean isJavaIdentifier(String s) {
- if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) {
+ if (isEmpty(s) || !Character.isJavaIdentifierStart(s.charAt(0))) {
return false;
}
for (int i = 1; i < s.length(); i++) {
@@ -421,10 +459,7 @@ public final class StringUtils {
}
public static boolean isContains(String values, String value) {
- if (values == null || values.length() == 0) {
- return false;
- }
- return isContains(Constants.COMMA_SPLIT_PATTERN.split(values), value);
+ return isNotEmpty(values) &&
isContains(Constants.COMMA_SPLIT_PATTERN.split(values), value);
}
/**
@@ -433,7 +468,7 @@ public final class StringUtils {
* @return contains
*/
public static boolean isContains(String[] values, String value) {
- if (value != null && value.length() > 0 && values != null &&
values.length > 0) {
+ if (isNotEmpty(value) && ArrayUtils.isNotEmpty(values)) {
for (String v : values) {
if (value.equals(v)) {
return true;
@@ -449,7 +484,7 @@ public final class StringUtils {
}
int sz = str.length();
for (int i = 0; i < sz; i++) {
- if (Character.isDigit(str.charAt(i)) == false) {
+ if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
@@ -494,14 +529,14 @@ public final class StringUtils {
}
/**
- * translat.
+ * translate.
*
* @param src source string.
* @param from src char table.
* @param to target char table.
* @return String.
*/
- public static String translat(String src, String from, String to) {
+ public static String translate(String src, String from, String to) {
if (isEmpty(src)) {
return src;
}
@@ -561,8 +596,8 @@ public final class StringUtils {
* @return String.
*/
public static String join(String[] array) {
- if (array.length == 0) {
- return "";
+ if (ArrayUtils.isEmpty(array)) {
+ return EMPTY;
}
StringBuilder sb = new StringBuilder();
for (String s : array) {
@@ -579,8 +614,8 @@ public final class StringUtils {
* @return String.
*/
public static String join(String[] array, char split) {
- if (array.length == 0) {
- return "";
+ if (ArrayUtils.isEmpty(array)) {
+ return EMPTY;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
@@ -600,8 +635,8 @@ public final class StringUtils {
* @return String.
*/
public static String join(String[] array, String split) {
- if (array.length == 0) {
- return "";
+ if (ArrayUtils.isEmpty(array)) {
+ return EMPTY;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
@@ -614,8 +649,8 @@ public final class StringUtils {
}
public static String join(Collection<String> coll, String split) {
- if (coll.isEmpty()) {
- return "";
+ if (CollectionUtils.isEmpty(coll)) {
+ return EMPTY;
}
StringBuilder sb = new StringBuilder();
@@ -643,7 +678,7 @@ public final class StringUtils {
Map<String, String> map = new HashMap<String, String>(tmp.length);
for (int i = 0; i < tmp.length; i++) {
Matcher matcher = KVP_PATTERN.matcher(tmp[i]);
- if (matcher.matches() == false) {
+ if (!matcher.matches()) {
continue;
}
map.put(matcher.group(1), matcher.group(2));
@@ -663,7 +698,7 @@ public final class StringUtils {
* @return Parameters instance.
*/
public static Map<String, String> parseQueryString(String qs) {
- if (qs == null || qs.length() == 0) {
+ if (isEmpty(qs)) {
return new HashMap<String, String>();
}
return parseKeyValuePair(qs, "\\&");
@@ -672,12 +707,12 @@ public final class StringUtils {
public static String getServiceKey(Map<String, String> ps) {
StringBuilder buf = new StringBuilder();
String group = ps.get(Constants.GROUP_KEY);
- if (group != null && group.length() > 0) {
+ if (isNotEmpty(group)) {
buf.append(group).append("/");
}
buf.append(ps.get(Constants.INTERFACE_KEY));
String version = ps.get(Constants.VERSION_KEY);
- if (version != null && version.length() > 0) {
+ if (isNotEmpty(group)) {
buf.append(":").append(version);
}
return buf.toString();
@@ -689,8 +724,7 @@ public final class StringUtils {
for (Map.Entry<String, String> entry : new TreeMap<String,
String>(ps).entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
- if (key != null && key.length() > 0
- && value != null && value.length() > 0) {
+ if (isNoneEmpty(key, value)) {
if (buf.length() > 0) {
buf.append("&");
}
@@ -704,7 +738,7 @@ public final class StringUtils {
}
public static String camelToSplitName(String camelName, String split) {
- if (camelName == null || camelName.length() == 0) {
+ if (isEmpty(camelName)) {
return camelName;
}
StringBuilder buf = null;
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ArrayUtilsTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ArrayUtilsTest.java
new file mode 100644
index 0000000..76fb788
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ArrayUtilsTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.dubbo.common.utils;
+
+import static junit.framework.TestCase.assertFalse;
+import static junit.framework.TestCase.assertTrue;
+
+import org.junit.Test;
+
+public class ArrayUtilsTest {
+
+ @Test
+ public void isEmpty() throws Exception {
+ assertTrue(ArrayUtils.isEmpty(null));
+ assertTrue(ArrayUtils.isEmpty(new Object[0]));
+ assertFalse(ArrayUtils.isEmpty(new Object[]{"abc"}));
+ }
+
+ @Test
+ public void isNotEmpty() throws Exception {
+ assertFalse(ArrayUtils.isNotEmpty(null));
+ assertFalse(ArrayUtils.isNotEmpty(new Object[0]));
+ assertTrue(ArrayUtils.isNotEmpty(new Object[]{"abc"}));
+ }
+
+}
\ No newline at end of file
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StringUtilsTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StringUtilsTest.java
index fa95bfb..1115441 100644
---
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StringUtilsTest.java
+++
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StringUtilsTest.java
@@ -112,6 +112,30 @@ public class StringUtilsTest {
}
@Test
+ public void testIsNoneEmpty() throws Exception {
+ assertFalse(StringUtils.isNoneEmpty(null));
+ assertFalse(StringUtils.isNoneEmpty(""));
+ assertTrue(StringUtils.isNoneEmpty(" "));
+ assertTrue(StringUtils.isNoneEmpty("abc"));
+ assertTrue(StringUtils.isNoneEmpty("abc", "def"));
+ assertFalse(StringUtils.isNoneEmpty("abc", null));
+ assertFalse(StringUtils.isNoneEmpty("abc", ""));
+ assertTrue(StringUtils.isNoneEmpty("abc", " "));
+ }
+
+ @Test
+ public void testIsAnyEmpty() throws Exception {
+ assertTrue(StringUtils.isAnyEmpty(null));
+ assertTrue(StringUtils.isAnyEmpty(""));
+ assertFalse(StringUtils.isAnyEmpty(" "));
+ assertFalse(StringUtils.isAnyEmpty("abc"));
+ assertFalse(StringUtils.isAnyEmpty("abc", "def"));
+ assertTrue(StringUtils.isAnyEmpty("abc", null));
+ assertTrue(StringUtils.isAnyEmpty("abc", ""));
+ assertFalse(StringUtils.isAnyEmpty("abc", " "));
+ }
+
+ @Test
public void testIsNotEmpty() throws Exception {
assertFalse(StringUtils.isNotEmpty(null));
assertFalse(StringUtils.isNotEmpty(""));
@@ -200,10 +224,10 @@ public class StringUtilsTest {
}
@Test
- public void testTranslat() throws Exception {
+ public void testTranslate() throws Exception {
String s = "16314";
- assertEquals(StringUtils.translat(s, "123456", "abcdef"), "afcad");
- assertEquals(StringUtils.translat(s, "123456", "abcd"), "acad");
+ assertEquals(StringUtils.translate(s, "123456", "abcdef"), "afcad");
+ assertEquals(StringUtils.translate(s, "123456", "abcd"), "acad");
}
@Test