提交 0242b5ec authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Optimize ValueUuid to String conversion

上级 9cd06d96
...@@ -1079,6 +1079,26 @@ public class StringUtils { ...@@ -1079,6 +1079,26 @@ public class StringUtils {
return builder; return builder;
} }
/**
* Appends specified number of trailing bytes from unsigned long value to a
* specified string builder.
*
* @param builder
* string builder to append to
* @param x
* value to append
* @param bytes
* number of bytes to append
* @return the specified string builder
*/
public static StringBuilder appendHex(StringBuilder builder, long x, int bytes) {
char[] hex = HEX;
for (int i = bytes * 8; i > 0;) {
builder.append(hex[(int) (x >> (i -= 4)) & 0xf]).append(hex[(int) (x >> (i -= 4)) & 0xf]);
}
return builder;
}
/** /**
* Check if this string is a decimal number. * Check if this string is a decimal number.
* *
......
...@@ -141,29 +141,17 @@ public class ValueUuid extends Value { ...@@ -141,29 +141,17 @@ public class ValueUuid extends Value {
return PRECISION; return PRECISION;
} }
private static void appendHex(StringBuilder buff, long x, int bytes) {
for (int i = bytes * 8 - 4; i >= 0; i -= 8) {
buff.append(Integer.toHexString((int) (x >> i) & 0xf)).
append(Integer.toHexString((int) (x >> (i - 4)) & 0xf));
}
}
@Override @Override
public String getString() { public String getString() {
return addString(new StringBuilder(36)).toString(); return addString(new StringBuilder(36)).toString();
} }
private StringBuilder addString(StringBuilder builder) { private StringBuilder addString(StringBuilder builder) {
appendHex(builder, high >> 32, 4); StringUtils.appendHex(builder, high >> 32, 4).append('-');
builder.append('-'); StringUtils.appendHex(builder, high >> 16, 2).append('-');
appendHex(builder, high >> 16, 2); StringUtils.appendHex(builder, high, 2).append('-');
builder.append('-'); StringUtils.appendHex(builder, low >> 48, 2).append('-');
appendHex(builder, high, 2); return StringUtils.appendHex(builder, low, 6);
builder.append('-');
appendHex(builder, low >> 48, 2);
builder.append('-');
appendHex(builder, low, 6);
return builder;
} }
@Override @Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论