From c13f839279134c499cfa767eea676fe1d62b9700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E5=B0=8F=E6=B3=A2?= Date: Tue, 6 Nov 2018 17:09:00 +0800 Subject: [PATCH] Simplify the code of StringUtils to make it more graceful. (#2740) * Simplify the code of StringUtils to make it more graceful. * Add Apache license --- .../apache/dubbo/common/utils/ArrayUtils.java | 47 +++++++ .../dubbo/common/utils/StringUtils.java | 118 +++++++++++------- .../dubbo/common/utils/ArrayUtilsTest.java | 41 ++++++ .../dubbo/common/utils/StringUtilsTest.java | 30 ++++- 4 files changed, 191 insertions(+), 45 deletions(-) create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/utils/ArrayUtils.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/utils/ArrayUtilsTest.java 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 000000000000..a0b62bd402c3 --- /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() { + } + + /** + *

Checks if the array is null or empty.

+ * + * @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; + } + + /** + *

Checks if the array is not null or empty.

+ * + * @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 c2c708f0a609..f599218f4108 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 static String repeat(final String str, final String separator, final int * {@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 static String replace(final String text, final String searchString, final * {@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 static String replace(final String text, final String searchString, final } public static boolean isBlank(String str) { - if (str == null || str.length() == 0) { - return true; - } - return false; + return isEmpty(str); } /** @@ -353,10 +350,57 @@ public static boolean isBlank(String str) { * @return is empty. */ public static boolean isEmpty(String str) { - if (str == null || str.length() == 0) { - return true; + return str == null || str.isEmpty(); + } + + /** + *

Checks if the strings contain empty or null elements.

+ * + *

+     * 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
+     * 
+ * + * @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; + } + + /** + *

Checks if the strings contain at least on empty or null element.

+ * + *

+     * 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
+     * 
+ * + * @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 static boolean isEmpty(String str) { * @return is not empty. */ public static boolean isNotEmpty(String str) { - return str != null && str.length() > 0; + return !isEmpty(str); } /** @@ -391,17 +435,11 @@ public static boolean isEquals(String s1, String s2) { * @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 static int parseInteger(String str) { * more info. */ 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 static boolean isJavaIdentifier(String s) { } 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 static boolean isContains(String values, String value) { * @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 static boolean isNumeric(String str) { } 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 static String toString(String msg, Throwable e) { } /** - * 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 static String[] split(String str, char ch) { * @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 static String join(String[] array) { * @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 static String join(String[] array, char split) { * @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 static String join(String[] array, String split) { } public static String join(Collection coll, String split) { - if (coll.isEmpty()) { - return ""; + if (CollectionUtils.isEmpty(coll)) { + return EMPTY; } StringBuilder sb = new StringBuilder(); @@ -643,7 +678,7 @@ private static Map parseKeyValuePair(String str, String itemSepa Map map = new HashMap(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 static String getQueryStringValue(String qs, String key) { * @return Parameters instance. */ public static Map parseQueryString(String qs) { - if (qs == null || qs.length() == 0) { + if (isEmpty(qs)) { return new HashMap(); } return parseKeyValuePair(qs, "\\&"); @@ -672,12 +707,12 @@ public static Map parseQueryString(String qs) { public static String getServiceKey(Map 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 static String toQueryString(Map ps) { for (Map.Entry entry : new TreeMap(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 static String toQueryString(Map ps) { } 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 000000000000..76fb7884bd1e --- /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 fa95bfb834c4..111544155b96 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 @@ -111,6 +111,30 @@ public void testIsEmpty() throws Exception { assertFalse(StringUtils.isEmpty("abc")); } + @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)); @@ -200,10 +224,10 @@ public void testSplit() throws Exception { } @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