From ebacc622daa999e41ca74b15ef03495a725379c0 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 4 Dec 2024 19:37:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20MapWrapper=20=E7=9A=84?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collection/AbstractMapWrapper.java | 26 ++++++++++++++----- .../commons/collection/MapWrapper.java | 10 +++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java index bd9c967..93b4242 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java @@ -27,12 +27,26 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; import java.util.function.Function; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import com.google.common.annotations.Beta; +import xyz.zhouxy.plusone.commons.util.AssertTools; import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapTools; +/** + * AbstractMapWrapper + * + *

+ * 包装了一个 {@link Map}。 + * 主要在于获取值时,如果 Key 不存在,是抛出异常;如果 Key 存在,则将 value 包装在 {@link Optional} 中 返回。 + * 此外可以定义 Key 和 Value 的检查规则。 + *

+ * + * @author ZhouXY + * @since 1.0 + */ @Beta public abstract class AbstractMapWrapper> { @@ -40,7 +54,9 @@ public abstract class AbstractMapWrapper keyChecker; private final Consumer valueChecker; - protected AbstractMapWrapper(Map map, @Nullable Consumer keyChecker, @Nullable Consumer valueChecker) { + protected AbstractMapWrapper(Map map, + @Nullable Consumer keyChecker, + @Nullable Consumer valueChecker) { this.map = map; this.keyChecker = keyChecker; this.valueChecker = valueChecker; @@ -71,10 +87,9 @@ public abstract class AbstractMapWrapper get(K key) { - if (!this.map.containsKey(key)) { - throw new IllegalArgumentException("Key does not exist"); - } + AssertTools.checkArgument(this.map.containsKey(key), "Key does not exist"); return Optional.ofNullable(this.map.get(key)); } @@ -180,8 +195,7 @@ public abstract class AbstractMapWrapper other = (AbstractMapWrapper) obj; return Objects.equals(map, other.map); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/MapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/MapWrapper.java index c928ae6..026bd9c 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/MapWrapper.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/MapWrapper.java @@ -26,6 +26,16 @@ import java.util.function.Consumer; import com.google.common.annotations.Beta; +/** + * MapWrapper + * + *

+ * Map 包装器的默认实现 + *

+ * + * @author ZhouXY + * @since 0.1.0 + */ @Beta public final class MapWrapper extends AbstractMapWrapper> {