提交 ad6dc91e authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Split DataUtils.appendMap() for different arguments

上级 a335281e
......@@ -566,16 +566,30 @@ public final class DataUtils {
* @param map the map
* @return the string builder
*/
public static StringBuilder appendMap(StringBuilder buff,
HashMap<String, ?> map) {
public static StringBuilder appendMap(StringBuilder buff, HashMap<String, ?> map) {
Object[] keys = map.keySet().toArray();
Arrays.sort(keys);
for (Object k : keys) {
appendMap(buff, (String) k, map.get(k));
String key = (String) k;
Object value = map.get(key);
if (value instanceof Long) {
appendMap(buff, key, (long) value);
} else if (value instanceof Integer) {
appendMap(buff, key, (int) value);
} else {
appendMap(buff, key, value.toString());
}
}
return buff;
}
private static StringBuilder appendMapKey(StringBuilder buff, String key) {
if (buff.length() > 0) {
buff.append(',');
}
return buff.append(key).append(':');
}
/**
* Append a key-value pair to the string builder. Keys may not contain a
* colon. Values that contain a comma or a double quote are enclosed in
......@@ -585,25 +599,14 @@ public final class DataUtils {
* @param key the key
* @param value the value
*/
public static void appendMap(StringBuilder buff, String key, Object value) {
if (buff.length() > 0) {
buff.append(',');
}
buff.append(key).append(':');
String v;
if (value instanceof Long) {
v = Long.toHexString((Long) value);
} else if (value instanceof Integer) {
v = Integer.toHexString((Integer) value);
} else {
v = value.toString();
}
if (v.indexOf(',') < 0 && v.indexOf('\"') < 0) {
buff.append(v);
public static void appendMap(StringBuilder buff, String key, String value) {
appendMapKey(buff, key);
if (value.indexOf(',') < 0 && value.indexOf('\"') < 0) {
buff.append(value);
} else {
buff.append('\"');
for (int i = 0, size = v.length(); i < size; i++) {
char c = v.charAt(i);
for (int i = 0, size = value.length(); i < size; i++) {
char c = value.charAt(i);
if (c == '\"') {
buff.append('\\');
}
......@@ -613,6 +616,30 @@ public final class DataUtils {
}
}
/**
* Append a key-value pair to the string builder. Keys may not contain a
* colon.
*
* @param buff the target buffer
* @param key the key
* @param value the value
*/
public static void appendMap(StringBuilder buff, String key, long value) {
appendMapKey(buff, key).append(Long.toHexString(value));
}
/**
* Append a key-value pair to the string builder. Keys may not contain a
* colon.
*
* @param buff the target buffer
* @param key the key
* @param value the value
*/
public static void appendMap(StringBuilder buff, String key, int value) {
appendMapKey(buff, key).append(Integer.toHexString(value));
}
/**
* @param buff output buffer, should be empty
* @param s parsed string
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论