Skip to content

Commit 7509369

Browse files
code4wtCrazyHZM
authored andcommitted
Simplify the code of StringUtils to make it more graceful. (apache#2740)
* Simplify the code of StringUtils to make it more graceful. * Add Apache license
1 parent 9bf85b3 commit 7509369

File tree

4 files changed

+191
-45
lines changed

4 files changed

+191
-45
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dubbo.common.utils;
19+
20+
/**
21+
* Contains some methods to check array.
22+
*/
23+
public final class ArrayUtils {
24+
25+
private ArrayUtils() {
26+
}
27+
28+
/**
29+
* <p>Checks if the array is null or empty. <p/>
30+
*
31+
* @param array th array to check
32+
* @return {@code true} if the array is null or empty.
33+
*/
34+
public static boolean isEmpty(final Object[] array) {
35+
return array == null || array.length == 0;
36+
}
37+
38+
/**
39+
* <p>Checks if the array is not null or empty. <p/>
40+
*
41+
* @param array th array to check
42+
* @return {@code true} if the array is not null or empty.
43+
*/
44+
public static boolean isNotEmpty(final Object[] array) {
45+
return !isEmpty(array);
46+
}
47+
}

dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java

Lines changed: 76 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public static String repeat(final String str, final String separator, final int
174174
* {@code null} if null String input
175175
*/
176176
public static String removeEnd(final String str, final String remove) {
177-
if (isEmpty(str) || isEmpty(remove)) {
177+
if (isAnyEmpty(str, remove)) {
178178
return str;
179179
}
180180
if (str.endsWith(remove)) {
@@ -314,7 +314,7 @@ public static String replace(final String text, final String searchString, final
314314
* {@code null} if null String input
315315
*/
316316
public static String replace(final String text, final String searchString, final String replacement, int max) {
317-
if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0) {
317+
if (isAnyEmpty(text, searchString) || replacement == null || max == 0) {
318318
return text;
319319
}
320320
int start = 0;
@@ -340,10 +340,7 @@ public static String replace(final String text, final String searchString, final
340340
}
341341

342342
public static boolean isBlank(String str) {
343-
if (str == null || str.length() == 0) {
344-
return true;
345-
}
346-
return false;
343+
return isEmpty(str);
347344
}
348345

349346
/**
@@ -353,10 +350,57 @@ public static boolean isBlank(String str) {
353350
* @return is empty.
354351
*/
355352
public static boolean isEmpty(String str) {
356-
if (str == null || str.length() == 0) {
357-
return true;
353+
return str == null || str.isEmpty();
354+
}
355+
356+
/**
357+
* <p>Checks if the strings contain empty or null elements. <p/>
358+
*
359+
* <pre>
360+
* StringUtils.isNoneEmpty(null) = false
361+
* StringUtils.isNoneEmpty("") = false
362+
* StringUtils.isNoneEmpty(" ") = true
363+
* StringUtils.isNoneEmpty("abc") = true
364+
* StringUtils.isNoneEmpty("abc", "def") = true
365+
* StringUtils.isNoneEmpty("abc", null) = false
366+
* StringUtils.isNoneEmpty("abc", "") = false
367+
* StringUtils.isNoneEmpty("abc", " ") = true
368+
* </pre>
369+
*
370+
* @param ss the strings to check
371+
* @return {@code true} if all strings are not empty or null
372+
*/
373+
public static boolean isNoneEmpty(final String... ss) {
374+
if (ArrayUtils.isEmpty(ss)) {
375+
return false;
358376
}
359-
return false;
377+
for (final String s : ss){
378+
if (isEmpty(s)) {
379+
return false;
380+
}
381+
}
382+
return true;
383+
}
384+
385+
/**
386+
* <p>Checks if the strings contain at least on empty or null element. <p/>
387+
*
388+
* <pre>
389+
* StringUtils.isAnyEmpty(null) = true
390+
* StringUtils.isAnyEmpty("") = true
391+
* StringUtils.isAnyEmpty(" ") = false
392+
* StringUtils.isAnyEmpty("abc") = false
393+
* StringUtils.isAnyEmpty("abc", "def") = false
394+
* StringUtils.isAnyEmpty("abc", null) = true
395+
* StringUtils.isAnyEmpty("abc", "") = true
396+
* StringUtils.isAnyEmpty("abc", " ") = false
397+
* </pre>
398+
*
399+
* @param ss the strings to check
400+
* @return {@code true} if at least one in the strings is empty or null
401+
*/
402+
public static boolean isAnyEmpty(final String... ss) {
403+
return !isNoneEmpty(ss);
360404
}
361405

362406
/**
@@ -366,7 +410,7 @@ public static boolean isEmpty(String str) {
366410
* @return is not empty.
367411
*/
368412
public static boolean isNotEmpty(String str) {
369-
return str != null && str.length() > 0;
413+
return !isEmpty(str);
370414
}
371415

372416
/**
@@ -391,25 +435,19 @@ public static boolean isEquals(String s1, String s2) {
391435
* @return is integer
392436
*/
393437
public static boolean isInteger(String str) {
394-
if (str == null || str.length() == 0) {
395-
return false;
396-
}
397-
return INT_PATTERN.matcher(str).matches();
438+
return isNotEmpty(str) && INT_PATTERN.matcher(str).matches();
398439
}
399440

400441
public static int parseInteger(String str) {
401-
if (!isInteger(str)) {
402-
return 0;
403-
}
404-
return Integer.parseInt(str);
442+
return isInteger(str) ? Integer.parseInt(str) : 0;
405443
}
406444

407445
/**
408446
* Returns true if s is a legal Java identifier.<p>
409447
* <a href="http://www.exampledepot.com/egs/java.lang/IsJavaId.html">more info.</a>
410448
*/
411449
public static boolean isJavaIdentifier(String s) {
412-
if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) {
450+
if (isEmpty(s) || !Character.isJavaIdentifierStart(s.charAt(0))) {
413451
return false;
414452
}
415453
for (int i = 1; i < s.length(); i++) {
@@ -421,10 +459,7 @@ public static boolean isJavaIdentifier(String s) {
421459
}
422460

423461
public static boolean isContains(String values, String value) {
424-
if (values == null || values.length() == 0) {
425-
return false;
426-
}
427-
return isContains(Constants.COMMA_SPLIT_PATTERN.split(values), value);
462+
return isNotEmpty(values) && isContains(Constants.COMMA_SPLIT_PATTERN.split(values), value);
428463
}
429464

430465
/**
@@ -433,7 +468,7 @@ public static boolean isContains(String values, String value) {
433468
* @return contains
434469
*/
435470
public static boolean isContains(String[] values, String value) {
436-
if (value != null && value.length() > 0 && values != null && values.length > 0) {
471+
if (isNotEmpty(value) && ArrayUtils.isNotEmpty(values)) {
437472
for (String v : values) {
438473
if (value.equals(v)) {
439474
return true;
@@ -449,7 +484,7 @@ public static boolean isNumeric(String str) {
449484
}
450485
int sz = str.length();
451486
for (int i = 0; i < sz; i++) {
452-
if (Character.isDigit(str.charAt(i)) == false) {
487+
if (!Character.isDigit(str.charAt(i))) {
453488
return false;
454489
}
455490
}
@@ -494,14 +529,14 @@ public static String toString(String msg, Throwable e) {
494529
}
495530

496531
/**
497-
* translat.
532+
* translate.
498533
*
499534
* @param src source string.
500535
* @param from src char table.
501536
* @param to target char table.
502537
* @return String.
503538
*/
504-
public static String translat(String src, String from, String to) {
539+
public static String translate(String src, String from, String to) {
505540
if (isEmpty(src)) {
506541
return src;
507542
}
@@ -561,8 +596,8 @@ public static String[] split(String str, char ch) {
561596
* @return String.
562597
*/
563598
public static String join(String[] array) {
564-
if (array.length == 0) {
565-
return "";
599+
if (ArrayUtils.isEmpty(array)) {
600+
return EMPTY;
566601
}
567602
StringBuilder sb = new StringBuilder();
568603
for (String s : array) {
@@ -579,8 +614,8 @@ public static String join(String[] array) {
579614
* @return String.
580615
*/
581616
public static String join(String[] array, char split) {
582-
if (array.length == 0) {
583-
return "";
617+
if (ArrayUtils.isEmpty(array)) {
618+
return EMPTY;
584619
}
585620
StringBuilder sb = new StringBuilder();
586621
for (int i = 0; i < array.length; i++) {
@@ -600,8 +635,8 @@ public static String join(String[] array, char split) {
600635
* @return String.
601636
*/
602637
public static String join(String[] array, String split) {
603-
if (array.length == 0) {
604-
return "";
638+
if (ArrayUtils.isEmpty(array)) {
639+
return EMPTY;
605640
}
606641
StringBuilder sb = new StringBuilder();
607642
for (int i = 0; i < array.length; i++) {
@@ -614,8 +649,8 @@ public static String join(String[] array, String split) {
614649
}
615650

616651
public static String join(Collection<String> coll, String split) {
617-
if (coll.isEmpty()) {
618-
return "";
652+
if (CollectionUtils.isEmpty(coll)) {
653+
return EMPTY;
619654
}
620655

621656
StringBuilder sb = new StringBuilder();
@@ -643,7 +678,7 @@ private static Map<String, String> parseKeyValuePair(String str, String itemSepa
643678
Map<String, String> map = new HashMap<String, String>(tmp.length);
644679
for (int i = 0; i < tmp.length; i++) {
645680
Matcher matcher = KVP_PATTERN.matcher(tmp[i]);
646-
if (matcher.matches() == false) {
681+
if (!matcher.matches()) {
647682
continue;
648683
}
649684
map.put(matcher.group(1), matcher.group(2));
@@ -663,7 +698,7 @@ public static String getQueryStringValue(String qs, String key) {
663698
* @return Parameters instance.
664699
*/
665700
public static Map<String, String> parseQueryString(String qs) {
666-
if (qs == null || qs.length() == 0) {
701+
if (isEmpty(qs)) {
667702
return new HashMap<String, String>();
668703
}
669704
return parseKeyValuePair(qs, "\\&");
@@ -672,12 +707,12 @@ public static Map<String, String> parseQueryString(String qs) {
672707
public static String getServiceKey(Map<String, String> ps) {
673708
StringBuilder buf = new StringBuilder();
674709
String group = ps.get(Constants.GROUP_KEY);
675-
if (group != null && group.length() > 0) {
710+
if (isNotEmpty(group)) {
676711
buf.append(group).append("/");
677712
}
678713
buf.append(ps.get(Constants.INTERFACE_KEY));
679714
String version = ps.get(Constants.VERSION_KEY);
680-
if (version != null && version.length() > 0) {
715+
if (isNotEmpty(group)) {
681716
buf.append(":").append(version);
682717
}
683718
return buf.toString();
@@ -689,8 +724,7 @@ public static String toQueryString(Map<String, String> ps) {
689724
for (Map.Entry<String, String> entry : new TreeMap<String, String>(ps).entrySet()) {
690725
String key = entry.getKey();
691726
String value = entry.getValue();
692-
if (key != null && key.length() > 0
693-
&& value != null && value.length() > 0) {
727+
if (isNoneEmpty(key, value)) {
694728
if (buf.length() > 0) {
695729
buf.append("&");
696730
}
@@ -704,7 +738,7 @@ public static String toQueryString(Map<String, String> ps) {
704738
}
705739

706740
public static String camelToSplitName(String camelName, String split) {
707-
if (camelName == null || camelName.length() == 0) {
741+
if (isEmpty(camelName)) {
708742
return camelName;
709743
}
710744
StringBuilder buf = null;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dubbo.common.utils;
19+
20+
import static junit.framework.TestCase.assertFalse;
21+
import static junit.framework.TestCase.assertTrue;
22+
23+
import org.junit.Test;
24+
25+
public class ArrayUtilsTest {
26+
27+
@Test
28+
public void isEmpty() throws Exception {
29+
assertTrue(ArrayUtils.isEmpty(null));
30+
assertTrue(ArrayUtils.isEmpty(new Object[0]));
31+
assertFalse(ArrayUtils.isEmpty(new Object[]{"abc"}));
32+
}
33+
34+
@Test
35+
public void isNotEmpty() throws Exception {
36+
assertFalse(ArrayUtils.isNotEmpty(null));
37+
assertFalse(ArrayUtils.isNotEmpty(new Object[0]));
38+
assertTrue(ArrayUtils.isNotEmpty(new Object[]{"abc"}));
39+
}
40+
41+
}

0 commit comments

Comments
 (0)