From 13fc8e40b51ed1cd5a1fb10d717091597a9a37cc Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 27 Jun 2023 01:57:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/util/AbstractMapWrapper.java | 35 +++++++++++++------ .../plusone/commons/util/MapWrapper.java | 2 +- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java index dea4355..31623e5 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java @@ -70,10 +70,25 @@ public abstract class AbstractMapWrapper get(K key) { - if (this.map.containsKey(key)) { - return Optional.ofNullable(this.map.get(key)); + if (!this.map.containsKey(key)) { + throw new IllegalArgumentException("Key does not exist"); } - throw new IllegalArgumentException("Key does not exist"); + return Optional.ofNullable(this.map.get(key)); + } + + /** + * 获取 {@code map} 中的值。如果 {@code key} 不存在,则抛出异常。 + * + * @param key 键 + * @return 值 + * @throws IllegalArgumentException key 不存在时抛出。 + */ + @Nullable + public V getOrNull(K key) { + if (!this.map.containsKey(key)) { + throw new IllegalArgumentException("Key does not exist"); + } + return this.map.get(key); } @SuppressWarnings("unchecked") @@ -149,7 +164,7 @@ public abstract class AbstractMapWrapper { + protected abstract static class Builder> { protected final Map map; protected Consumer keyChecker; protected Consumer valueChecker; @@ -158,17 +173,17 @@ public abstract class AbstractMapWrapper keyChecker(@Nullable Consumer keyChecker) { + public Builder keyChecker(@Nullable Consumer keyChecker) { this.keyChecker = keyChecker; return this; } - public Builder valueChecker(@Nullable Consumer valueChecker) { + public Builder valueChecker(@Nullable Consumer valueChecker) { this.valueChecker = valueChecker; return this; } - public Builder put(K key, V value) { + public Builder put(K key, V value) { if (this.keyChecker != null) { this.keyChecker.accept(key); } @@ -179,15 +194,15 @@ public abstract class AbstractMapWrapper putAll(Map m) { + public Builder putAll(Map m) { for (Entry entry : m.entrySet()) { put(entry.getKey(), entry.getValue()); } return this; } - public abstract MapWrapper build(); + public abstract T build(); - public abstract MapWrapper buildUnmodifiableMap(); + public abstract T buildUnmodifiableMap(); } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java index 62a936f..151b0a9 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java @@ -61,7 +61,7 @@ public final class MapWrapper extends AbstractMapWrapper(new TreeMap<>(comparator)); } - public static final class Builder extends AbstractMapWrapper.Builder { + public static final class Builder extends AbstractMapWrapper.Builder> { private Builder(Map map) { super(map);