From 80d47dd8cf7a12b160181b9947716f9176b0c978 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 12 Nov 2020 21:33:52 +0800 Subject: [PATCH] =?UTF-8?q?[Change]=20CacheStore-API=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=20CacheKey=20=E6=9E=84=E9=80=A0=E6=96=B9=E6=B3=95=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86=E7=BB=86=E8=8A=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Change] CacheKey 调整构造方法 '(String, String...)' 中对 'keyStrings' 为 null 或 0 长度添加判断, 增加对 'keyStrings' 中包含 null 值的检查; --- .../net/lamgc/cgj/bot/cache/CacheKey.java | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/ContentGrabbingJi-CacheStore-api/src/main/java/net/lamgc/cgj/bot/cache/CacheKey.java b/ContentGrabbingJi-CacheStore-api/src/main/java/net/lamgc/cgj/bot/cache/CacheKey.java index ecab7a0..695b4a6 100644 --- a/ContentGrabbingJi-CacheStore-api/src/main/java/net/lamgc/cgj/bot/cache/CacheKey.java +++ b/ContentGrabbingJi-CacheStore-api/src/main/java/net/lamgc/cgj/bot/cache/CacheKey.java @@ -27,9 +27,12 @@ import java.util.Objects; */ public final class CacheKey { + /** + * 默认分隔符. + */ 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) { Objects.requireNonNull(first); - Objects.requireNonNull(keyStrings); - this.key = new String[keyStrings.length + 1]; - this.key[0] = first; - System.arraycopy(keyStrings, 0, this.key, 1, keyStrings.length); + if (keyStrings == null || keyStrings.length == 0) { + this.keys = new String[] {first}; + } else { + 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) { Objects.requireNonNull(keyStrings); + checkKeyStrings(keyStrings); if (keyStrings.length == 0) { 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 的字符串数组. */ public String[] getKeyArray() { - return key; + return keys; } /** @@ -71,7 +87,7 @@ public final class CacheKey { * @return 返回组装后的完整 Key. */ public String join(String separator) { - return String.join(separator, key); + return String.join(separator, keys); } @Override @@ -88,11 +104,11 @@ public final class CacheKey { return false; } CacheKey cacheKey = (CacheKey) o; - return Arrays.equals(key, cacheKey.key); + return Arrays.equals(keys, cacheKey.keys); } @Override public int hashCode() { - return Arrays.hashCode(key); + return Arrays.hashCode(keys); } }