Skip to content

Commit 4e10149

Browse files
dj080808kimmking
authored andcommittedSep 26, 2019
refactor some string operations with StringUtils (apache#5112)
refactor string empty check statements.
1 parent cf988f5 commit 4e10149

File tree

3 files changed

+18
-38
lines changed

3 files changed

+18
-38
lines changed
 

‎dubbo-common/src/main/java/org/apache/dubbo/common/URL.java

+14-34
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public List<URL> getBackupUrls() {
453453
}
454454

455455
static String appendDefaultPort(String address, int defaultPort) {
456-
if (address != null && address.length() > 0 && defaultPort > 0) {
456+
if (StringUtils.isNotEmpty(address) && defaultPort > 0) {
457457
int i = address.indexOf(':');
458458
if (i < 0) {
459459
return address + ":" + defaultPort;
@@ -508,7 +508,7 @@ public String[] getParameter(String key, String[] defaultValue) {
508508

509509
public List<String> getParameter(String key, List<String> defaultValue) {
510510
String value = getParameter(key);
511-
if (value == null || value.length() == 0) {
511+
if (StringUtils.isEmpty(value)) {
512512
return defaultValue;
513513
}
514514
String[] strArray = COMMA_SPLIT_PATTERN.split(value);
@@ -872,7 +872,7 @@ public boolean hasMethodParameter(String method, String key) {
872872
return false;
873873
}
874874
String value = getMethodParameter(method, key);
875-
return value != null && value.length() > 0;
875+
return StringUtils.isNotEmpty(value);
876876
}
877877

878878
public boolean isLocalHost() {
@@ -1167,7 +1167,7 @@ private void buildParameters(StringBuilder buf, boolean concat, String[] paramet
11671167
List<String> includes = (ArrayUtils.isEmpty(parameters) ? null : Arrays.asList(parameters));
11681168
boolean first = true;
11691169
for (Map.Entry<String, String> entry : new TreeMap<>(getParameters()).entrySet()) {
1170-
if (entry.getKey() != null && entry.getKey().length() > 0
1170+
if (StringUtils.isNotEmpty(entry.getKey())
11711171
&& (includes == null || includes.contains(entry.getKey()))) {
11721172
if (first) {
11731173
if (concat) {
@@ -1197,7 +1197,7 @@ private String buildString(boolean appendUser, boolean appendParameter, boolean
11971197
}
11981198
if (appendUser && StringUtils.isNotEmpty(username)) {
11991199
buf.append(username);
1200-
if (password != null && password.length() > 0) {
1200+
if (StringUtils.isNotEmpty(password)) {
12011201
buf.append(":");
12021202
buf.append(password);
12031203
}
@@ -1209,7 +1209,7 @@ private String buildString(boolean appendUser, boolean appendParameter, boolean
12091209
} else {
12101210
host = getHost();
12111211
}
1212-
if (host != null && host.length() > 0) {
1212+
if (StringUtils.isNotEmpty(host)) {
12131213
buf.append(host);
12141214
if (port > 0) {
12151215
buf.append(":");
@@ -1222,7 +1222,7 @@ private String buildString(boolean appendUser, boolean appendParameter, boolean
12221222
} else {
12231223
path = getPath();
12241224
}
1225-
if (path != null && path.length() > 0) {
1225+
if (StringUtils.isNotEmpty(path)) {
12261226
buf.append("/");
12271227
buf.append(path);
12281228
}
@@ -1295,11 +1295,11 @@ public String getPathKey() {
12951295

12961296
public static String buildKey(String path, String group, String version) {
12971297
StringBuilder buf = new StringBuilder();
1298-
if (group != null && group.length() > 0) {
1298+
if (StringUtils.isNotEmpty(group)) {
12991299
buf.append(group).append("/");
13001300
}
13011301
buf.append(path);
1302-
if (version != null && version.length() > 0) {
1302+
if (StringUtils.isNotEmpty(version)) {
13031303
buf.append(":").append(version);
13041304
}
13051305
return buf.toString();
@@ -1448,11 +1448,7 @@ public boolean equals(Object obj) {
14481448
return false;
14491449
}
14501450
URL other = (URL) obj;
1451-
if (host == null) {
1452-
if (other.host != null) {
1453-
return false;
1454-
}
1455-
} else if (!host.equals(other.host)) {
1451+
if(!StringUtils.isEquals(host, other.host)) {
14561452
return false;
14571453
}
14581454
if (parameters == null) {
@@ -1462,35 +1458,19 @@ public boolean equals(Object obj) {
14621458
} else if (!parameters.equals(other.parameters)) {
14631459
return false;
14641460
}
1465-
if (password == null) {
1466-
if (other.password != null) {
1467-
return false;
1468-
}
1469-
} else if (!password.equals(other.password)) {
1461+
if(!StringUtils.isEquals(password, other.password)) {
14701462
return false;
14711463
}
1472-
if (path == null) {
1473-
if (other.path != null) {
1474-
return false;
1475-
}
1476-
} else if (!path.equals(other.path)) {
1464+
if(!StringUtils.isEquals(path, other.path)) {
14771465
return false;
14781466
}
14791467
if (port != other.port) {
14801468
return false;
14811469
}
1482-
if (protocol == null) {
1483-
if (other.protocol != null) {
1484-
return false;
1485-
}
1486-
} else if (!protocol.equals(other.protocol)) {
1470+
if(!StringUtils.isEquals(protocol, other.protocol)) {
14871471
return false;
14881472
}
1489-
if (username == null) {
1490-
if (other.username != null) {
1491-
return false;
1492-
}
1493-
} else if (!username.equals(other.username)) {
1473+
if(!StringUtils.isEquals(username, other.username)) {
14941474
return false;
14951475
}
14961476
return true;

‎dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public URLBuilder clearParameters() {
339339

340340
public boolean hasParameter(String key) {
341341
String value = getParameter(key);
342-
return value != null && value.length() > 0;
342+
return StringUtils.isNotEmpty(value);
343343
}
344344

345345
public String getParameter(String key) {

‎dubbo-common/src/main/java/org/apache/dubbo/common/Version.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ public static String getVersion(Class<?> cls, String defaultVersion) {
162162
String version = null;
163163
if (pkg != null) {
164164
version = pkg.getImplementationVersion();
165-
if (!StringUtils.isEmpty(version)) {
165+
if (StringUtils.isNotEmpty(version)) {
166166
return version;
167167
}
168168

169169
version = pkg.getSpecificationVersion();
170-
if (!StringUtils.isEmpty(version)) {
170+
if (StringUtils.isNotEmpty(version)) {
171171
return version;
172172
}
173173
}
@@ -260,7 +260,7 @@ private static Set<String> getResources(String path) throws IOException {
260260
URL url = urls.nextElement();
261261
if (url != null) {
262262
String file = url.getFile();
263-
if (file != null && file.length() > 0) {
263+
if (StringUtils.isNotEmpty(file)) {
264264
files.add(file);
265265
}
266266
}

0 commit comments

Comments
 (0)
Please sign in to comment.