[ISSUE #7220] replace with lambda (#7222)

* replace with lambda

* replace with lambda
This commit is contained in:
李晓双 Li Xiao Shuang 2021-11-12 10:20:54 +08:00 committed by GitHub
parent 0719200c38
commit 80874749bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,10 +44,8 @@ import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.Random; import java.util.Random;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -65,14 +63,11 @@ public class ServerListManager implements Closeable {
private final NacosRestTemplate nacosRestTemplate = ConfigHttpClientManager.getInstance().getNacosRestTemplate(); private final NacosRestTemplate nacosRestTemplate = ConfigHttpClientManager.getInstance().getNacosRestTemplate();
private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, r -> {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r); Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.ServerListManager"); t.setName("com.alibaba.nacos.client.ServerListManager");
t.setDaemon(true); t.setDaemon(true);
return t; return t;
}
}); });
/** /**
@ -109,7 +104,7 @@ public class ServerListManager implements Closeable {
private String serverListName = ParamUtil.getDefaultNodesPath(); private String serverListName = ParamUtil.getDefaultNodesPath();
volatile List<String> serverUrls = new ArrayList<String>(); volatile List<String> serverUrls = new ArrayList<>();
private volatile String currentServerAddr; private volatile String currentServerAddr;
@ -134,7 +129,7 @@ public class ServerListManager implements Closeable {
public ServerListManager(List<String> fixed, String namespace) { public ServerListManager(List<String> fixed, String namespace) {
this.isFixed = true; this.isFixed = true;
this.isStarted = true; this.isStarted = true;
List<String> serverAddrs = new ArrayList<String>(); List<String> serverAddrs = new ArrayList<>();
for (String serverAddr : fixed) { for (String serverAddr : fixed) {
String[] serverAddrArr = InternetAddressUtil.splitIPPortStr(serverAddr); String[] serverAddrArr = InternetAddressUtil.splitIPPortStr(serverAddr);
if (serverAddrArr.length == 1) { if (serverAddrArr.length == 1) {
@ -143,7 +138,7 @@ public class ServerListManager implements Closeable {
serverAddrs.add(serverAddr); serverAddrs.add(serverAddr);
} }
} }
this.serverUrls = new ArrayList<String>(serverAddrs); this.serverUrls = new ArrayList<>(serverAddrs);
if (StringUtils.isBlank(namespace)) { if (StringUtils.isBlank(namespace)) {
this.name = FIXED_NAME + "-" + getFixedNameSuffix(serverAddrs.toArray(new String[serverAddrs.size()])); this.name = FIXED_NAME + "-" + getFixedNameSuffix(serverAddrs.toArray(new String[serverAddrs.size()]));
} else { } else {
@ -200,7 +195,7 @@ public class ServerListManager implements Closeable {
if (StringUtils.isNotEmpty(serverAddrsStr)) { if (StringUtils.isNotEmpty(serverAddrsStr)) {
this.isFixed = true; this.isFixed = true;
List<String> serverAddrs = new ArrayList<String>(); List<String> serverAddrs = new ArrayList<>();
StringTokenizer serverAddrsTokens = new StringTokenizer(this.serverAddrsStr, ",;"); StringTokenizer serverAddrsTokens = new StringTokenizer(this.serverAddrsStr, ",;");
while (serverAddrsTokens.hasMoreTokens()) { while (serverAddrsTokens.hasMoreTokens()) {
String serverAddr = serverAddrsTokens.nextToken().trim(); String serverAddr = serverAddrsTokens.nextToken().trim();
@ -263,12 +258,7 @@ public class ServerListManager implements Closeable {
String endpointPortTmp = TemplateUtils String endpointPortTmp = TemplateUtils
.stringEmptyAndThenExecute(System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT), .stringEmptyAndThenExecute(System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT),
new Callable<String>() { () -> properties.getProperty(PropertyKeyConst.ENDPOINT_PORT));
@Override
public String call() {
return properties.getProperty(PropertyKeyConst.ENDPOINT_PORT);
}
});
if (StringUtils.isNotBlank(endpointPortTmp)) { if (StringUtils.isNotBlank(endpointPortTmp)) {
this.endpointPort = Integer.parseInt(endpointPortTmp); this.endpointPort = Integer.parseInt(endpointPortTmp);
@ -370,7 +360,7 @@ public class ServerListManager implements Closeable {
return; return;
} }
List<String> newServerAddrList = new ArrayList<String>(); List<String> newServerAddrList = new ArrayList<>();
for (String server : newList) { for (String server : newList) {
if (server.startsWith(HTTP) || server.startsWith(HTTPS)) { if (server.startsWith(HTTP) || server.startsWith(HTTPS)) {
newServerAddrList.add(server); newServerAddrList.add(server);
@ -385,7 +375,7 @@ public class ServerListManager implements Closeable {
if (newServerAddrList.equals(serverUrls)) { if (newServerAddrList.equals(serverUrls)) {
return; return;
} }
serverUrls = new ArrayList<String>(newServerAddrList); serverUrls = new ArrayList<>(newServerAddrList);
iterator = iterator(); iterator = iterator();
currentServerAddr = iterator.next(); currentServerAddr = iterator.next();
@ -403,7 +393,7 @@ public class ServerListManager implements Closeable {
EnvUtil.setSelfEnv(httpResult.getHeader().getOriginalResponseHeader()); EnvUtil.setSelfEnv(httpResult.getHeader().getOriginalResponseHeader());
} }
List<String> lines = IoUtils.readLines(new StringReader(httpResult.getData())); List<String> lines = IoUtils.readLines(new StringReader(httpResult.getData()));
List<String> result = new ArrayList<String>(lines.size()); List<String> result = new ArrayList<>(lines.size());
for (String serverAddr : lines) { for (String serverAddr : lines) {
if (StringUtils.isNotBlank(serverAddr)) { if (StringUtils.isNotBlank(serverAddr)) {
String[] ipPort = InternetAddressUtil.splitIPPortStr(serverAddr.trim()); String[] ipPort = InternetAddressUtil.splitIPPortStr(serverAddr.trim());
@ -543,7 +533,7 @@ public class ServerListManager implements Closeable {
} }
public ServerAddressIterator(List<String> source) { public ServerAddressIterator(List<String> source) {
sorted = new ArrayList<RandomizedServerAddress>(); sorted = new ArrayList<>();
for (String address : source) { for (String address : source) {
sorted.add(new RandomizedServerAddress(address)); sorted.add(new RandomizedServerAddress(address));
} }