[Change] CacheStore-API 调整 CacheKey 构造方法中的处理细节.

[Change] CacheKey 调整构造方法 '<init>(String, String...)' 中对 'keyStrings' 为 null 或 0 长度添加判断, 增加对 'keyStrings' 中包含 null 值的检查;
This commit is contained in:
LamGC 2020-11-12 21:33:52 +08:00
parent 76372adbf9
commit 80d47dd8cf
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D

View File

@ -27,9 +27,12 @@ import java.util.Objects;
*/ */
public final class CacheKey { public final class CacheKey {
/**
* 默认分隔符.
*/
public final static String DEFAULT_SEPARATOR = "."; public final static String DEFAULT_SEPARATOR = ".";
private final String[] key; private final String[] keys;
/** /**
* 创建一个缓存键名. * 创建一个缓存键名.
@ -39,10 +42,14 @@ public final class CacheKey {
*/ */
public CacheKey(String first, String... keyStrings) { public CacheKey(String first, String... keyStrings) {
Objects.requireNonNull(first); Objects.requireNonNull(first);
Objects.requireNonNull(keyStrings); if (keyStrings == null || keyStrings.length == 0) {
this.key = new String[keyStrings.length + 1]; this.keys = new String[] {first};
this.key[0] = first; } else {
System.arraycopy(keyStrings, 0, this.key, 1, keyStrings.length); checkKeyStrings(keyStrings);
this.keys = new String[keyStrings.length + 1];
this.keys[0] = first;
System.arraycopy(keyStrings, 0, this.keys, 1, keyStrings.length);
}
} }
/** /**
@ -51,10 +58,19 @@ public final class CacheKey {
*/ */
public CacheKey(String[] keyStrings) { public CacheKey(String[] keyStrings) {
Objects.requireNonNull(keyStrings); Objects.requireNonNull(keyStrings);
checkKeyStrings(keyStrings);
if (keyStrings.length == 0) { if (keyStrings.length == 0) {
throw new IllegalArgumentException("Provide at least one element that makes up the key"); throw new IllegalArgumentException("Provide at least one element that makes up the key");
} }
this.key = keyStrings; this.keys = keyStrings;
}
private void checkKeyStrings(String[] keyStrings) {
for (String keyString : keyStrings) {
if (keyString == null) {
throw new NullPointerException("KeyStrings contains null");
}
}
} }
/** /**
@ -62,7 +78,7 @@ public final class CacheKey {
* @return 返回用于组成 Key 的字符串数组. * @return 返回用于组成 Key 的字符串数组.
*/ */
public String[] getKeyArray() { public String[] getKeyArray() {
return key; return keys;
} }
/** /**
@ -71,7 +87,7 @@ public final class CacheKey {
* @return 返回组装后的完整 Key. * @return 返回组装后的完整 Key.
*/ */
public String join(String separator) { public String join(String separator) {
return String.join(separator, key); return String.join(separator, keys);
} }
@Override @Override
@ -88,11 +104,11 @@ public final class CacheKey {
return false; return false;
} }
CacheKey cacheKey = (CacheKey) o; CacheKey cacheKey = (CacheKey) o;
return Arrays.equals(key, cacheKey.key); return Arrays.equals(keys, cacheKey.keys);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Arrays.hashCode(key); return Arrays.hashCode(keys);
} }
} }