创建 MapWrapper 包装 Map 的操作。

feature/net-util
ZhouXY108 2023-05-09 01:03:21 +08:00
parent 5f3e5c4807
commit 85ababc482
2 changed files with 209 additions and 0 deletions

View File

@ -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<K, V, T extends AbstractMapWrapper<K, V, T>> {
private final Map<K, V> map;
private final Consumer<K> keyChecker;
private final Consumer<V> valueChecker;
protected AbstractMapWrapper(Map<K, V> map, @Nullable Consumer<K> keyChecker, @Nullable Consumer<V> 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<? extends K, ? extends V> m) {
for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
put(entry.getKey(), entry.getValue());
}
return getSelf();
}
public final Optional<V> 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 <R> Optional<R> getAndConvert(K key) {
return get(key).map(v -> (R) v);
}
public final <R> Optional<R> getAndConvert(K key, Function<V, R> 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<K> keySet() {
return this.map.keySet();
}
public final Collection<V> values() {
return this.map.values();
}
public final Set<Entry<K, V>> 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<K, V> exportMap() {
return this.map;
}
public final Map<K, V> exportUnmodifiableMapMap() {
return Collections.unmodifiableMap(this.map);
}
protected abstract T getSelf();
}

View File

@ -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<K, V> extends AbstractMapWrapper<K, V, MapWrapper<K, V>> {
private MapWrapper(Map<K, V> map, Consumer<K> keyChecker, Consumer<V> valueChecker) {
super(map, keyChecker, valueChecker);
}
public static <K, V> Builder<K, V> wrap(Map<K, V> map) {
return new Builder<>(map);
}
public static <K, V> Builder<K, V> wrapHashMap() {
return new Builder<>(new HashMap<>());
}
public static <K, V> Builder<K, V> wrapHashMap(int initialCapacity) {
return new Builder<>(new HashMap<>(initialCapacity));
}
public static <K, V> Builder<K, V> wrapHashMap(int initialCapacity, float loadFactor) {
return new Builder<>(new HashMap<>(initialCapacity, loadFactor));
}
public static <K, V> Builder<K, V> wrapTreeMap() {
return new Builder<>(new TreeMap<>());
}
public static <K, V> Builder<K, V> wrapTreeMap(SortedMap<K, ? extends V> m) {
return new Builder<>(new TreeMap<>(m));
}
public static <K, V> Builder<K, V> wrapTreeMap(Comparator<? super K> comparator) {
return new Builder<>(new TreeMap<>(comparator));
}
public static final class Builder<K, V> {
private final Map<K, V> map;
private Consumer<K> keyChecker;
private Consumer<V> valueChecker;
private Builder(Map<K, V> map) {
this.map = map;
}
public Builder<K, V> keyChecker(@Nullable Consumer<K> keyChecker) {
this.keyChecker = keyChecker;
return this;
}
public Builder<K, V> valueChecker(@Nullable Consumer<V> valueChecker) {
this.valueChecker = valueChecker;
return this;
}
public Builder<K, V> 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<K, V> putAll(Map<? extends K, ? extends V> m) {
for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
put(entry.getKey(), entry.getValue());
}
return this;
}
public MapWrapper<K, V> build() {
return new MapWrapper<>(map, keyChecker, valueChecker);
}
public MapWrapper<K, V> buildUnmodifiableMap() {
return new MapWrapper<>(Collections.unmodifiableMap(map), keyChecker, valueChecker);
}
}
@Override
protected MapWrapper<K, V> getSelf() {
return this;
}
}