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(); }