提交 815d73a5 编写于 作者: Z zhanghua

beautify code

上级 ca3a84cc
package github.javaguide.extension;
import github.javaguide.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
......@@ -51,7 +52,7 @@ public final class ExtensionLoader<T> {
}
public T getExtension(String name) {
if (name == null || name.isEmpty()) {
if (StringUtils.isBlank(name)) {
throw new IllegalArgumentException("Extension name should not be null or empty.");
}
// firstly get from cache, if not hit, create one
......
package github.javaguide.utils;
import java.util.Collection;
/**
* @Description:
* @Author: zhanghua
* @Date: 2022/3/6 12:58 上午
*/
public class CollectionUtils {
public static boolean isEmpty(Collection coll) {
return coll == null || coll.isEmpty();
}
public static boolean isNotEmpty(Collection coll) {
return isEmpty(coll);
}
}
package github.javaguide.utils;
/**
* @Description:
* @Author: zhanghua
* @Date: 2022/3/6 12:58 上午
*/
public class StringUtils {
public static boolean isBlank(String s){
int strLen ;
if(s == null || (strLen = s.length()) == 0){
return true;
}
for(int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
public static boolean isNotBlank(String s){
return !isBlank(s);
}
}
package github.javaguide.loadbalance;
import github.javaguide.remoting.dto.RpcRequest;
import github.javaguide.utils.CollectionUtils;
import java.util.List;
......@@ -13,7 +14,7 @@ import java.util.List;
public abstract class AbstractLoadBalance implements LoadBalance {
@Override
public String selectServiceAddress(List<String> serviceAddresses, RpcRequest rpcRequest) {
if (serviceAddresses == null || serviceAddresses.size() == 0) {
if (CollectionUtils.isEmpty(serviceAddresses)) {
return null;
}
if (serviceAddresses.size() == 1) {
......
......@@ -16,8 +16,9 @@ public interface LoadBalance {
/**
* Choose one from the list of existing service addresses list
*
* @param serviceAddresses Service address list
* @param serviceUrlList Service address list
* @param rpcRequest
* @return target service address
*/
String selectServiceAddress(List<String> serviceAddresses, RpcRequest rpcRequest);
String selectServiceAddress(List<String> serviceUrlList, RpcRequest rpcRequest);
}
......@@ -7,6 +7,7 @@ import github.javaguide.loadbalance.LoadBalance;
import github.javaguide.registry.ServiceDiscovery;
import github.javaguide.registry.zk.util.CuratorUtils;
import github.javaguide.remoting.dto.RpcRequest;
import github.javaguide.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
......@@ -32,7 +33,7 @@ public class ZkServiceDiscoveryImpl implements ServiceDiscovery {
String rpcServiceName = rpcRequest.getRpcServiceName();
CuratorFramework zkClient = CuratorUtils.getZkClient();
List<String> serviceUrlList = CuratorUtils.getChildrenNodes(zkClient, rpcServiceName);
if (serviceUrlList == null || serviceUrlList.size() == 0) {
if (CollectionUtils.isEmpty(serviceUrlList)) {
throw new RpcException(RpcErrorMessageEnum.SERVICE_CAN_NOT_BE_FOUND, rpcServiceName);
}
// load balancing
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册