refactor: 局部变量是线程安全的;

urlPattern改成static final;
          GroupKey重构
This commit is contained in:
xianlaioy 2019-09-09 20:15:19 +08:00
parent 7b5c6e9273
commit 08e69a0b21
8 changed files with 39 additions and 49 deletions

View File

@ -24,33 +24,28 @@ import com.alibaba.nacos.client.utils.StringUtils;
*/
public class GroupKey {
static public String getKey(String dataId, String group) {
StringBuilder sb = new StringBuilder();
urlEncode(dataId, sb);
sb.append('+');
urlEncode(group, sb);
return sb.toString();
public static String getKey(String dataId, String group) {
return doGetKey(dataId, group, "");
}
static public String getKeyTenant(String dataId, String group, String tenant) {
StringBuilder sb = new StringBuilder();
urlEncode(dataId, sb);
sb.append('+');
urlEncode(group, sb);
if (StringUtils.isNotEmpty(tenant)) {
sb.append('+');
urlEncode(tenant, sb);
}
return sb.toString();
public static String getKeyTenant(String dataId, String group, String tenant) {
return doGetKey(dataId, group, tenant);
}
static public String getKey(String dataId, String group, String datumStr) {
public static String getKey(String dataId, String group, String datumStr) {
return doGetKey(dataId, group, datumStr);
}
private static String doGetKey(String dataId, String group, String datumStr) {
StringBuilder sb = new StringBuilder();
urlEncode(dataId, sb);
sb.append('+');
urlEncode(group, sb);
if (StringUtils.isNotEmpty(datumStr)) {
sb.append('+');
urlEncode(datumStr, sb);
}
return sb.toString();
}

View File

@ -157,7 +157,7 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
sqlFileIn = new FileInputStream(file);
}
StringBuffer sqlSb = new StringBuffer();
StringBuilder sqlSb = new StringBuilder();
byte[] buff = new byte[1024];
int byteRead = 0;
while ((byteRead = sqlFileIn.read(buff)) != -1) {

View File

@ -22,37 +22,32 @@ package com.alibaba.nacos.config.server.utils;
*/
public class GroupKey {
static public String getKey(String dataId, String group) {
StringBuilder sb = new StringBuilder();
urlEncode(dataId, sb);
sb.append('+');
urlEncode(group, sb);
return sb.toString();
public static String getKey(String dataId, String group) {
return doGetKey(dataId, group, "");
}
static public String getKeyTenant(String dataId, String group, String tenant) {
StringBuilder sb = new StringBuilder();
urlEncode(dataId, sb);
sb.append('+');
urlEncode(group, sb);
if (StringUtils.isNotEmpty(tenant)) {
sb.append('+');
urlEncode(tenant, sb);
}
return sb.toString();
public static String getKeyTenant(String dataId, String group, String tenant) {
return doGetKey(dataId, group, tenant);
}
static public String getKey(String dataId, String group, String datumStr) {
public static String getKey(String dataId, String group, String datumStr) {
return doGetKey(dataId, group, datumStr);
}
private static String doGetKey(String dataId, String group, String datumStr) {
StringBuilder sb = new StringBuilder();
urlEncode(dataId, sb);
sb.append('+');
urlEncode(group, sb);
if (StringUtils.isNotEmpty(datumStr)) {
sb.append('+');
urlEncode(datumStr, sb);
}
return sb.toString();
}
static public String[] parseKey(String groupKey) {
public static String[] parseKey(String groupKey) {
StringBuilder sb = new StringBuilder();
String dataId = null;
String group = null;
@ -97,7 +92,7 @@ public class GroupKey {
}
}
return new String[] {dataId, group, tenant};
return new String[]{dataId, group, tenant};
}
/**

View File

@ -37,7 +37,7 @@ public class RegexParser {
if (regex == null) {
throw new NullPointerException("regex string can't be null");
}
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
result.append("^");
for (int i = 0; i < regex.length(); i++) {
char ch = regex.charAt(i);

View File

@ -33,7 +33,7 @@ public class TimeUtils {
return new Timestamp(date.getTime());
}
static public String getCurrentTimeStr() {
public static String getCurrentTimeStr() {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

View File

@ -59,7 +59,7 @@ public class TimeoutUtils {
/**
* 累计总的时间
*
* @param timeout
* @param time
*/
public void addTotalTime(long time) {
totalTime.addAndGet(time);

View File

@ -27,7 +27,7 @@ import java.util.regex.Pattern;
*/
public class UrlAnalysisUtils {
private static Pattern urlPattern = Pattern.compile("^(\\w+://)?([\\w\\.]+:)(\\d*)?(\\??.*)");
private static final Pattern URL_PATTERN = Pattern.compile("^(\\w+://)?([\\w\\.]+:)(\\d*)?(\\??.*)");
public static String getContentIdentity(String content) {
@ -35,7 +35,7 @@ public class UrlAnalysisUtils {
return null;
}
Matcher matcher = urlPattern.matcher(content);
Matcher matcher = URL_PATTERN.matcher(content);
StringBuilder buf = new StringBuilder();
if (matcher.find()) {
String scheme = matcher.group(1);

View File

@ -27,6 +27,7 @@ public class RandomUtils {
private static Random rd = new Random();
private static int UNICODE_START = 19968;
private static int UNICODE_END = 40864;
private static final String STRING_POOL = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private RandomUtils() {
}
@ -132,13 +133,12 @@ public class RandomUtils {
}
public static String getRandomString(int length) {
StringBuffer buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
Random random = new Random();
int range = buffer.length();
int range = STRING_POOL.length();
for(int i = 0; i < length; ++i) {
sb.append(buffer.charAt(random.nextInt(range)));
sb.append(STRING_POOL.charAt(random.nextInt(range)));
}
return sb.toString();