Skip to content

Commit

Permalink
Simplify the code of StringUtils to make it more graceful. (apache#2740
Browse files Browse the repository at this point in the history
)

* Simplify the code of StringUtils to make it more graceful.

* Add Apache license
  • Loading branch information
code4wt authored and CrazyHZM committed Dec 6, 2018
1 parent 9bf85b3 commit 7509369
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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();
}

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

/**
Expand All @@ -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);
}

/**
Expand All @@ -391,25 +435,19 @@ 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;
}

/**
* Returns true if s is a legal Java identifier.<p>
* <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++) {
Expand All @@ -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);
}

/**
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -614,8 +649,8 @@ public static String join(String[] array, String split) {
}

public static String join(Collection<String> coll, String split) {
if (coll.isEmpty()) {
return "";
if (CollectionUtils.isEmpty(coll)) {
return EMPTY;
}

StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -643,7 +678,7 @@ private static Map<String, String> parseKeyValuePair(String str, String itemSepa
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));
Expand All @@ -663,7 +698,7 @@ public static String getQueryStringValue(String qs, String key) {
* @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, "\\&");
Expand All @@ -672,12 +707,12 @@ public static Map<String, String> parseQueryString(String qs) {
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();
Expand All @@ -689,8 +724,7 @@ public static String toQueryString(Map<String, String> ps) {
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("&");
}
Expand All @@ -704,7 +738,7 @@ public static String toQueryString(Map<String, String> ps) {
}

public static String camelToSplitName(String camelName, String split) {
if (camelName == null || camelName.length() == 0) {
if (isEmpty(camelName)) {
return camelName;
}
StringBuilder buf = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -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"}));
}

}
Loading

0 comments on commit 7509369

Please sign in to comment.