Merge pull request #2142 from easyhaloo/code_optimization

refactor #2134 optimization code
This commit is contained in:
Peter Zhu 2019-12-12 22:42:20 +08:00 committed by GitHub
commit 264f686d1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 39 additions and 40 deletions

View File

@ -65,7 +65,7 @@ public class AddressServerGeneratorManager {
String[] ipAndPort = generateIpAndPort(ip); String[] ipAndPort = generateIpAndPort(ip);
Instance instance = new Instance(); Instance instance = new Instance();
instance.setIp(ipAndPort[0]); instance.setIp(ipAndPort[0]);
instance.setPort(Integer.valueOf(ipAndPort[1])); instance.setPort(Integer.parseInt(ipAndPort[1]));
instance.setClusterName(clusterName); instance.setClusterName(clusterName);
instance.setServiceName(serviceName); instance.setServiceName(serviceName);
instance.setTenant(Constants.DEFAULT_NAMESPACE_ID); instance.setTenant(Constants.DEFAULT_NAMESPACE_ID);

View File

@ -222,7 +222,7 @@ public class Instance {
} }
String value = getMetadata().get(key); String value = getMetadata().get(key);
if (!StringUtils.isEmpty(value) && value.matches(NUMBER_PATTERN)) { if (!StringUtils.isEmpty(value) && value.matches(NUMBER_PATTERN)) {
return Long.valueOf(value); return Long.parseLong(value);
} }
return defaultValue; return defaultValue;
} }

View File

@ -53,9 +53,9 @@ public class HistoryController {
@RequestParam(value = "pageSize", required = false) @RequestParam(value = "pageSize", required = false)
Integer pageSize, // Integer pageSize, //
ModelMap modelMap) { ModelMap modelMap) {
pageNo = null == pageNo ? Integer.valueOf(1) : pageNo; pageNo = null == pageNo ? 1 : pageNo;
pageSize = null == pageSize ? Integer.valueOf(100) : pageSize; pageSize = null == pageSize ? 100 : pageSize;
pageSize = pageSize > 500 ? Integer.valueOf(500) : pageSize; pageSize = Math.min(500,pageSize);
// configInfoBase没有appName字段 // configInfoBase没有appName字段
return persistService.findConfigHistory(dataId, group, tenant, pageNo, pageSize); return persistService.findConfigHistory(dataId, group, tenant, pageNo, pageSize);
} }

View File

@ -234,7 +234,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
if (result == null) { if (result == null) {
return false; return false;
} else { } else {
return result.intValue() == 0 ? true : false; return result == 0;
} }
} catch (CannotGetJdbcConnectionException e) { } catch (CannotGetJdbcConnectionException e) {
fatalLog.error("[db-error] " + e.toString(), e); fatalLog.error("[db-error] " + e.toString(), e);

View File

@ -865,7 +865,7 @@ public class PersistService {
ps.setString(index++, dataId); ps.setString(index++, dataId);
ps.setString(index++, group); ps.setString(index++, group);
ps.setString(index++, tenantTmp); ps.setString(index++, tenantTmp);
ps.setString(index++, datumId); ps.setString(index, datumId);
} }
}); });
} catch (CannotGetJdbcConnectionException e) { } catch (CannotGetJdbcConnectionException e) {
@ -888,7 +888,7 @@ public class PersistService {
int index = 1; int index = 1;
ps.setString(index++, dataId); ps.setString(index++, dataId);
ps.setString(index++, group); ps.setString(index++, group);
ps.setString(index++, tenantTmp); ps.setString(index, tenantTmp);
} }
}); });
} catch (CannotGetJdbcConnectionException e) { } catch (CannotGetJdbcConnectionException e) {
@ -993,7 +993,7 @@ public class PersistService {
if (isPublishOk == null) { if (isPublishOk == null) {
return false; return false;
} }
return isPublishOk.booleanValue(); return isPublishOk;
} catch (TransactionException e) { } catch (TransactionException e) {
fatalLog.error("[db-error] " + e.toString(), e); fatalLog.error("[db-error] " + e.toString(), e);
return false; return false;
@ -1035,7 +1035,7 @@ public class PersistService {
if (isReplaceOk == null) { if (isReplaceOk == null) {
return false; return false;
} }
return isReplaceOk.booleanValue(); return isReplaceOk;
} catch (TransactionException e) { } catch (TransactionException e) {
fatalLog.error("[db-error] " + e.toString(), e); fatalLog.error("[db-error] " + e.toString(), e);
return false; return false;
@ -2848,7 +2848,7 @@ public class PersistService {
sql.append(", "); sql.append(", ");
} }
sql.append("?"); sql.append("?");
paramList.add(Long.valueOf(tagArr[i])); paramList.add(Long.parseLong(tagArr[i]));
} }
sql.append(") "); sql.append(") ");
try { try {
@ -2957,7 +2957,7 @@ public class PersistService {
sql.append(", "); sql.append(", ");
} }
sql.append("?"); sql.append("?");
paramList.add(Long.valueOf(tagArr[i])); paramList.add(Long.parseLong(tagArr[i]));
} }
sql.append(") "); sql.append(") ");
try { try {

View File

@ -356,20 +356,19 @@ public class ServerListService implements ApplicationListener<WebServerInitializ
@Override @Override
public void completed(HttpResponse response) { public void completed(HttpResponse response) {
if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) { if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
serverIp2unhealthCount.put(serverIp, 0); serverListUnhealth.remove(serverIp);
if (serverListUnhealth.contains(serverIp)) {
serverListUnhealth.remove(serverIp);
}
HttpClientUtils.closeQuietly(response); HttpClientUtils.closeQuietly(response);
} }
} }
@Override @Override
public void failed(Exception ex) { public void failed(Exception ex) {
Integer failCount = serverIp2unhealthCount.get(serverIp); int failCount = serverIp2unhealthCount.compute(serverIp,(key,oldValue)->{
failCount = failCount == null ? Integer.valueOf(0) : failCount; if(oldValue == null){
failCount++; return 1;
serverIp2unhealthCount.put(serverIp, failCount); }
return oldValue+1;
});
if (failCount > maxFailCount) { if (failCount > maxFailCount) {
if (!serverListUnhealth.contains(serverIp)) { if (!serverListUnhealth.contains(serverIp)) {
serverListUnhealth.add(serverIp); serverListUnhealth.add(serverIp);
@ -381,10 +380,12 @@ public class ServerListService implements ApplicationListener<WebServerInitializ
@Override @Override
public void cancelled() { public void cancelled() {
Integer failCount = serverIp2unhealthCount.get(serverIp); int failCount = serverIp2unhealthCount.compute(serverIp,(key,oldValue)->{
failCount = failCount == null ? Integer.valueOf(0) : failCount; if(oldValue == null){
failCount++; return 1;
serverIp2unhealthCount.put(serverIp, failCount); }
return oldValue+1;
});
if (failCount > maxFailCount) { if (failCount > maxFailCount) {
if (!serverListUnhealth.contains(serverIp)) { if (!serverListUnhealth.contains(serverIp)) {
serverListUnhealth.add(serverIp); serverListUnhealth.add(serverIp);

View File

@ -145,8 +145,8 @@ public class MD5 {
byte[] data = new byte[16]; byte[] data = new byte[16];
char[] chs = str.toCharArray(); char[] chs = str.toCharArray();
for (int i = 0; i < DIGITS_COUNT; ++i) { for (int i = 0; i < DIGITS_COUNT; ++i) {
int h = rDigits.get(chs[i * 2]).intValue(); int h = rDigits.get(chs[i * 2]);
int l = rDigits.get(chs[i * 2 + 1]).intValue(); int l = rDigits.get(chs[i * 2 + 1]);
data[i] = (byte)((h & 0x0F) << 4 | (l & 0x0F)); data[i] = (byte)((h & 0x0F) << 4 | (l & 0x0F));
} }
return data; return data;

View File

@ -62,11 +62,10 @@ public class PaginationHelper<E> {
if (rowCountInt == null) { if (rowCountInt == null) {
throw new IllegalArgumentException("fetchPageLimit error"); throw new IllegalArgumentException("fetchPageLimit error");
} }
final int rowCount = rowCountInt.intValue();
// 计算页数 // 计算页数
int pageCount = rowCount / pageSize; int pageCount = rowCountInt / pageSize;
if (rowCount > pageSize * pageCount) { if (rowCountInt > pageSize * pageCount) {
pageCount++; pageCount++;
} }
@ -74,7 +73,7 @@ public class PaginationHelper<E> {
final Page<E> page = new Page<E>(); final Page<E> page = new Page<E>();
page.setPageNumber(pageNo); page.setPageNumber(pageNo);
page.setPagesAvailable(pageCount); page.setPagesAvailable(pageCount);
page.setTotalCount(rowCount); page.setTotalCount(rowCountInt);
if (pageNo > pageCount) { if (pageNo > pageCount) {
return null; return null;
@ -108,11 +107,10 @@ public class PaginationHelper<E> {
if (rowCountInt == null) { if (rowCountInt == null) {
throw new IllegalArgumentException("fetchPageLimit error"); throw new IllegalArgumentException("fetchPageLimit error");
} }
final int rowCount = rowCountInt.intValue();
// 计算页数 // 计算页数
int pageCount = rowCount / pageSize; int pageCount = rowCountInt / pageSize;
if (rowCount > pageSize * pageCount) { if (rowCountInt > pageSize * pageCount) {
pageCount++; pageCount++;
} }
@ -120,7 +118,7 @@ public class PaginationHelper<E> {
final Page<E> page = new Page<E>(); final Page<E> page = new Page<E>();
page.setPageNumber(pageNo); page.setPageNumber(pageNo);
page.setPagesAvailable(pageCount); page.setPagesAvailable(pageCount);
page.setTotalCount(rowCount); page.setTotalCount(rowCountInt);
if (pageNo > pageCount) { if (pageNo > pageCount) {
return null; return null;
@ -150,11 +148,10 @@ public class PaginationHelper<E> {
if (rowCountInt == null) { if (rowCountInt == null) {
throw new IllegalArgumentException("fetchPageLimit error"); throw new IllegalArgumentException("fetchPageLimit error");
} }
final int rowCount = rowCountInt.intValue();
// 计算页数 // 计算页数
int pageCount = rowCount / pageSize; int pageCount = rowCountInt / pageSize;
if (rowCount > pageSize * pageCount) { if (rowCountInt > pageSize * pageCount) {
pageCount++; pageCount++;
} }
@ -162,7 +159,7 @@ public class PaginationHelper<E> {
final Page<E> page = new Page<E>(); final Page<E> page = new Page<E>();
page.setPageNumber(pageNo); page.setPageNumber(pageNo);
page.setPagesAvailable(pageCount); page.setPagesAvailable(pageCount);
page.setTotalCount(rowCount); page.setTotalCount(rowCountInt);
if (pageNo > pageCount) { if (pageNo > pageCount) {
return null; return null;

View File

@ -322,8 +322,9 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
while (currentInstanceIds.contains(String.valueOf(id))) { while (currentInstanceIds.contains(String.valueOf(id))) {
id++; id++;
} }
currentInstanceIds.add(String.valueOf(id)); String idStr = String.valueOf(id);
return String.valueOf(id); currentInstanceIds.add(idStr);
return idStr;
} }
public void validate() throws NacosException { public void validate() throws NacosException {