package jnpf.bizcommon.audit.service.support;
|
|
import java.util.Locale;
|
|
/** 为同名字段附加物理表作用域的可持久化复合键。 */
|
public final class AuditDiffKey {
|
|
private static final String SCOPED_PREFIX = "@table:";
|
|
private AuditDiffKey() {
|
}
|
|
/**
|
* 长度前缀让表名和字段名的边界无歧义,同时只使用 PostgreSQL JSONB 可接受的可打印字符。
|
*/
|
public static String of(String targetTable, String field) {
|
String normalizedField = normalize(field);
|
String normalizedTable = normalize(targetTable);
|
if (normalizedTable == null) {
|
return normalizedField == null ? "" : normalizedField;
|
}
|
String effectiveField = normalizedField == null ? "" : normalizedField;
|
return SCOPED_PREFIX + normalizedTable.length() + ":" + normalizedTable + ":" + effectiveField;
|
}
|
|
public static boolean isScoped(String key) {
|
if (key == null || !key.startsWith(SCOPED_PREFIX)) {
|
return false;
|
}
|
int lengthEnd = key.indexOf(':', SCOPED_PREFIX.length());
|
if (lengthEnd < 0) {
|
return false;
|
}
|
try {
|
int tableLength = Integer.parseInt(key.substring(SCOPED_PREFIX.length(), lengthEnd));
|
long fieldSeparator = (long) lengthEnd + 1L + tableLength;
|
return tableLength >= 0 && fieldSeparator < key.length()
|
&& key.charAt((int) fieldSeparator) == ':';
|
} catch (NumberFormatException ignored) {
|
return false;
|
}
|
}
|
|
/** 只用于兼容已加载到内存的旧键;返回值不会再携带旧 NUL 分隔符。 */
|
public static String normalizeStored(String key) {
|
if (key == null) {
|
return null;
|
}
|
int separator = key.indexOf(Character.MIN_VALUE);
|
if (separator < 0) {
|
return key;
|
}
|
return of(key.substring(0, separator), key.substring(separator + 1));
|
}
|
|
private static String normalize(String value) {
|
if (value == null) {
|
return null;
|
}
|
String normalized = value.trim().toLowerCase(Locale.ROOT);
|
return normalized.isEmpty() ? null : normalized;
|
}
|
}
|