From 85ababc4827f99dc3928fd56ed75b06e60d6a3b3 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 9 May 2023 01:03:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=20MapWrapper=20=E5=8C=85?= =?UTF-8?q?=E8=A3=85=20Map=20=E7=9A=84=E6=93=8D=E4=BD=9C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/util/AbstractMapWrapper.java | 108 ++++++++++++++++++ .../plusone/commons/util/MapWrapper.java | 101 ++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java new file mode 100644 index 0000000..2b187db --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java @@ -0,0 +1,108 @@ +package xyz.zhouxy.plusone.commons.util; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.Set; +import java.util.function.Consumer; +import java.util.function.Function; + +import javax.annotation.Nullable; + +import com.google.common.annotations.Beta; + +@Beta +public abstract class AbstractMapWrapper> { + + private final Map map; + private final Consumer keyChecker; + private final Consumer valueChecker; + + protected AbstractMapWrapper(Map map, @Nullable Consumer keyChecker, @Nullable Consumer valueChecker) { + this.map = map; + this.keyChecker = keyChecker; + this.valueChecker = valueChecker; + } + + public final T 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 getSelf(); + } + + public final T putAll(Map m) { + for (Entry entry : m.entrySet()) { + put(entry.getKey(), entry.getValue()); + } + return getSelf(); + } + + public final Optional get(K key) { + if (this.map.containsKey(key)) { + return Optional.ofNullable(this.map.get(key)); + } + throw new IllegalArgumentException("Key does not exist"); + } + + @SuppressWarnings("unchecked") + public final Optional getAndConvert(K key) { + return get(key).map(v -> (R) v); + } + + public final Optional getAndConvert(K key, Function mapper) { + return get(key).map(mapper); + } + + public final boolean containsKey(Object key) { + return this.map.containsKey(key); + } + + public final int size() { + return this.map.size(); + } + + public final Set keySet() { + return this.map.keySet(); + } + + public final Collection values() { + return this.map.values(); + } + + public final Set> entrySet() { + return this.map.entrySet(); + } + + public final void clear() { + this.map.clear(); + } + + public final boolean containsValue(Object value) { + return this.map.containsValue(value); + } + + public final boolean isEmpty() { + return this.map.isEmpty(); + } + + public final V remove(Object key) { + return this.map.remove(key); + } + + public final Map exportMap() { + return this.map; + } + + public final Map exportUnmodifiableMapMap() { + return Collections.unmodifiableMap(this.map); + } + + protected abstract T getSelf(); +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java new file mode 100644 index 0000000..4031bae --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java @@ -0,0 +1,101 @@ +package xyz.zhouxy.plusone.commons.util; + +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 +public final class MapWrapper extends AbstractMapWrapper> { + + private MapWrapper(Map map, Consumer keyChecker, Consumer valueChecker) { + super(map, keyChecker, valueChecker); + } + + public static Builder wrap(Map map) { + return new Builder<>(map); + } + + public static Builder wrapHashMap() { + return new Builder<>(new HashMap<>()); + } + + public static Builder wrapHashMap(int initialCapacity) { + return new Builder<>(new HashMap<>(initialCapacity)); + } + + public static Builder wrapHashMap(int initialCapacity, float loadFactor) { + return new Builder<>(new HashMap<>(initialCapacity, loadFactor)); + } + + public static Builder wrapTreeMap() { + return new Builder<>(new TreeMap<>()); + } + + public static Builder wrapTreeMap(SortedMap m) { + return new Builder<>(new TreeMap<>(m)); + } + + public static Builder wrapTreeMap(Comparator comparator) { + return new Builder<>(new TreeMap<>(comparator)); + } + + public static final class Builder { + private final Map map; + private Consumer keyChecker; + private Consumer valueChecker; + + 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; + } + + public MapWrapper build() { + return new MapWrapper<>(map, keyChecker, valueChecker); + } + + public MapWrapper buildUnmodifiableMap() { + return new MapWrapper<>(Collections.unmodifiableMap(map), keyChecker, valueChecker); + } + } + + @Override + protected MapWrapper getSelf() { + return this; + } +}