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