From b732578e6d8b1608861754c83e21ecac029fe55a Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 31 May 2023 20:01:43 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/util/AbstractMapWrapper.java | 66 ++++++++++++++++++- .../plusone/commons/util/MapWrapper.java | 40 ++--------- 2 files changed, 69 insertions(+), 37 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 6f6c0af..8ebd436 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java @@ -60,7 +60,16 @@ public abstract class AbstractMapWrapper get(K key) { + /** + * 获取 {@code map} 中的值。如果 {@code key} 不存在,则抛出异常。 + * 将 {@code value}(可为 {@code null})装进 {@link Optional} 中后返回。 + * 为了这碟醋包的这盘饺子。 + * + * @param key 键 + * @return 可缺失的值 + * @throws IllegalArgumentException key 不存在时抛出。 + */ + public Optional get(K key) { if (this.map.containsKey(key)) { return Optional.ofNullable(this.map.get(key)); } @@ -112,6 +121,19 @@ public abstract class AbstractMapWrapper mappingFunction) { + V v = this.map.get(key); + if (null == v) { + this.map.putIfAbsent(key, mappingFunction.apply(key)); + v = this.map.get(key); + } + return v; + } + public final Map exportMap() { return this.map; } @@ -121,4 +143,46 @@ public abstract class AbstractMapWrapper { + protected final Map map; + protected Consumer keyChecker; + protected Consumer valueChecker; + + protected Builder(Map map) { + this.map = map; + } + + public Builder keyChecker(@Nullable Consumer keyChecker) { + this.keyChecker = keyChecker; + return this; + } + + public Builder valueChecker(@Nullable Consumer valueChecker) { + this.valueChecker = valueChecker; + return this; + } + + public Builder put(K key, V value) { + if (this.keyChecker != null) { + this.keyChecker.accept(key); + } + if (this.valueChecker != null) { + this.valueChecker.accept(value); + } + this.map.put(key, value); + return this; + } + + public Builder putAll(Map m) { + for (Entry entry : m.entrySet()) { + put(entry.getKey(), entry.getValue()); + } + return this; + } + + public abstract MapWrapper build(); + + public abstract MapWrapper 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 faf75da..62a936f 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java @@ -20,13 +20,10 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.SortedMap; import java.util.TreeMap; import java.util.function.Consumer; -import javax.annotation.Nullable; - import com.google.common.annotations.Beta; @Beta @@ -64,47 +61,18 @@ public final class MapWrapper extends AbstractMapWrapper(new TreeMap<>(comparator)); } - public static final class Builder { - private final Map map; - private Consumer keyChecker; - private Consumer valueChecker; + public static final class Builder extends AbstractMapWrapper.Builder { private Builder(Map map) { - this.map = map; - } - - public Builder keyChecker(@Nullable Consumer keyChecker) { - this.keyChecker = keyChecker; - return this; - } - - public Builder valueChecker(@Nullable Consumer valueChecker) { - this.valueChecker = valueChecker; - return this; - } - - public Builder put(K key, V value) { - if (this.keyChecker != null) { - this.keyChecker.accept(key); - } - if (this.valueChecker != null) { - this.valueChecker.accept(value); - } - this.map.put(key, value); - return this; - } - - public Builder putAll(Map m) { - for (Entry entry : m.entrySet()) { - put(entry.getKey(), entry.getValue()); - } - return this; + super(map); } + @Override public MapWrapper build() { return new MapWrapper<>(map, keyChecker, valueChecker); } + @Override public MapWrapper buildUnmodifiableMap() { return new MapWrapper<>(Collections.unmodifiableMap(map), keyChecker, valueChecker); }