Skip to content

Commit

Permalink
call the util method (#3230)
Browse files Browse the repository at this point in the history
* Code optimization, call the util method

* mofidy

* modify *

* import package
  • Loading branch information
CrazyHZM authored and beiwei30 committed Jan 16, 2019
1 parent 416c575 commit 458a450
Show file tree
Hide file tree
Showing 72 changed files with 306 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static String toParameterString(ConfigItem item) {
sb.append(item.getSide());
}
Map<String, String> parameters = item.getParameters();
if (parameters == null || parameters.isEmpty()) {
if (CollectionUtils.isEmptyMap(parameters)) {
throw new IllegalStateException("Invalid configurator rule, please specify at least one parameter " +
"you want to change in the rule.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcException;
Expand Down Expand Up @@ -48,8 +49,8 @@ public StaticDirectory(URL url, List<Invoker<T>> invokers) {
}

public StaticDirectory(URL url, List<Invoker<T>> invokers, RouterChain<T> routerChain) {
super(url == null && invokers != null && !invokers.isEmpty() ? invokers.get(0).getUrl() : url, routerChain);
if (invokers == null || invokers.isEmpty()) {
super(url == null && CollectionUtils.isNotEmpty(invokers) ? invokers.get(0).getUrl() : url, routerChain);
if (CollectionUtils.isEmpty(invokers)) {
throw new IllegalArgumentException("invokers == null");
}
this.invokers = invokers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.cluster.LoadBalance;
Expand All @@ -44,7 +45,7 @@ static int calculateWarmupWeight(int uptime, int warmup, int weight) {

@Override
public <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) {
if (invokers == null || invokers.isEmpty()) {
if (CollectionUtils.isEmpty(invokers)) {
return null;
}
if (invokers.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.common.utils.UrlUtils;
Expand Down Expand Up @@ -101,7 +102,7 @@ private static Map<String, MatchPair> parseRule(String rule)
String separator = matcher.group(1);
String content = matcher.group(2);
// Start part of the condition expression.
if (separator == null || separator.length() == 0) {
if (StringUtils.isEmpty(separator)) {
pair = new MatchPair();
condition.put(content, pair);
}
Expand Down Expand Up @@ -163,7 +164,7 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
return invokers;
}

if (invokers == null || invokers.isEmpty()) {
if (CollectionUtils.isEmpty(invokers)) {
return invokers;
}
try {
Expand Down Expand Up @@ -205,11 +206,11 @@ public URL getUrl() {
}

boolean matchWhen(URL url, Invocation invocation) {
return whenCondition == null || whenCondition.isEmpty() || matchCondition(whenCondition, url, null, invocation);
return CollectionUtils.isEmptyMap(whenCondition) || matchCondition(whenCondition, url, null, invocation);
}

private boolean matchThen(URL url, URL param) {
return !(thenCondition == null || thenCondition.isEmpty()) && matchCondition(thenCondition, url, param, null);
return CollectionUtils.isNotEmptyMap(thenCondition) && matchCondition(thenCondition, url, param, null);
}

private boolean matchCondition(Map<String, MatchPair> condition, URL url, URL param, Invocation invocation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcContext;
Expand Down Expand Up @@ -58,10 +59,10 @@ public ScriptRouter(URL url) {
String type = url.getParameter(Constants.TYPE_KEY);
this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
String rule = url.getParameterAndDecoded(Constants.RULE_KEY);
if (type == null || type.length() == 0) {
if (StringUtils.isEmpty(type)) {
type = Constants.DEFAULT_SCRIPT_TYPE_KEY;
}
if (rule == null || rule.length() == 0) {
if (StringUtils.isEmpty(rule)) {
throw new IllegalStateException("route rule can not be empty. rule:" + rule);
}
ScriptEngine engine = engines.get(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public void setApplication(String app) {

@Override
public <T> void notify(List<Invoker<T>> invokers) {
if (invokers == null || invokers.isEmpty()) {
if (CollectionUtils.isEmpty(invokers)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
Expand Down Expand Up @@ -103,7 +104,7 @@ private Result doMockInvoke(Invocation invocation, RpcException e) {
Invoker<T> minvoker;

List<Invoker<T>> mockInvokers = selectMockInvoker(invocation);
if (mockInvokers == null || mockInvokers.isEmpty()) {
if (CollectionUtils.isEmpty(mockInvokers)) {
minvoker = (Invoker<T>) new MockInvoker(directory.getUrl());
} else {
minvoker = mockInvokers.get(0);
Expand Down
34 changes: 17 additions & 17 deletions dubbo-common/src/main/java/org/apache/dubbo/common/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,37 +96,37 @@ public String getDecodedParameter(String key, String defaultValue) {

public String getParameter(String key) {
String value = parameters.get(key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
value = parameters.get(Constants.HIDE_KEY_PREFIX + key);
}
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
value = parameters.get(Constants.DEFAULT_KEY_PREFIX + key);
}
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
value = parameters.get(Constants.HIDE_KEY_PREFIX + Constants.DEFAULT_KEY_PREFIX + key);
}
return value;
}

public String getParameter(String key, String defaultValue) {
String value = getParameter(key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return defaultValue;
}
return value;
}

public int getIntParameter(String key) {
String value = getParameter(key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return 0;
}
return Integer.parseInt(value);
}

public int getIntParameter(String key, int defaultValue) {
String value = getParameter(key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return defaultValue;
}
return Integer.parseInt(value);
Expand All @@ -137,7 +137,7 @@ public int getPositiveIntParameter(String key, int defaultValue) {
throw new IllegalArgumentException("defaultValue <= 0");
}
String value = getParameter(key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return defaultValue;
}
int i = Integer.parseInt(value);
Expand All @@ -149,15 +149,15 @@ public int getPositiveIntParameter(String key, int defaultValue) {

public boolean getBooleanParameter(String key) {
String value = getParameter(key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return false;
}
return Boolean.parseBoolean(value);
}

public boolean getBooleanParameter(String key, boolean defaultValue) {
String value = getParameter(key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return defaultValue;
}
return Boolean.parseBoolean(value);
Expand All @@ -170,34 +170,34 @@ public boolean hasParamter(String key) {

public String getMethodParameter(String method, String key) {
String value = parameters.get(method + "." + key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
value = parameters.get(Constants.HIDE_KEY_PREFIX + method + "." + key);
}
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return getParameter(key);
}
return value;
}

public String getMethodParameter(String method, String key, String defaultValue) {
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return defaultValue;
}
return value;
}

public int getMethodIntParameter(String method, String key) {
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return 0;
}
return Integer.parseInt(value);
}

public int getMethodIntParameter(String method, String key, int defaultValue) {
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return defaultValue;
}
return Integer.parseInt(value);
Expand All @@ -208,7 +208,7 @@ public int getMethodPositiveIntParameter(String method, String key, int defaultV
throw new IllegalArgumentException("defaultValue <= 0");
}
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return defaultValue;
}
int i = Integer.parseInt(value);
Expand All @@ -220,15 +220,15 @@ public int getMethodPositiveIntParameter(String method, String key, int defaultV

public boolean getMethodBooleanParameter(String method, String key) {
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return false;
}
return Boolean.parseBoolean(value);
}

public boolean getMethodBooleanParameter(String method, String key, boolean defaultValue) {
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return defaultValue;
}
return Boolean.parseBoolean(value);
Expand Down
Loading

0 comments on commit 458a450

Please sign in to comment.