forked from plusone/plusone-commons
重构。
parent
122fff1da6
commit
b732578e6d
|
@ -60,7 +60,16 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
|
|||
return getSelf();
|
||||
}
|
||||
|
||||
public final Optional<V> get(K key) {
|
||||
/**
|
||||
* 获取 {@code map} 中的值。如果 {@code key} 不存在,则抛出异常。
|
||||
* 将 {@code value}(可为 {@code null})装进 {@link Optional} 中后返回。
|
||||
* <i>为了这碟醋包的这盘饺子。</i>
|
||||
*
|
||||
* @param key 键
|
||||
* @return 可缺失的值
|
||||
* @throws IllegalArgumentException key 不存在时抛出。
|
||||
*/
|
||||
public Optional<V> get(K key) {
|
||||
if (this.map.containsKey(key)) {
|
||||
return Optional.ofNullable(this.map.get(key));
|
||||
}
|
||||
|
@ -112,6 +121,19 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
|
|||
return this.map.remove(key);
|
||||
}
|
||||
|
||||
public final V putIfAbsent(K key, V value) {
|
||||
return this.map.putIfAbsent(key, value);
|
||||
}
|
||||
|
||||
public final V computeIfAbsent(K key, Function<? super K, ? extends V> 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<K, V> exportMap() {
|
||||
return this.map;
|
||||
}
|
||||
|
@ -121,4 +143,46 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
|
|||
}
|
||||
|
||||
protected abstract T getSelf();
|
||||
|
||||
protected abstract static class Builder<K, V> {
|
||||
protected final Map<K, V> map;
|
||||
protected Consumer<K> keyChecker;
|
||||
protected Consumer<V> valueChecker;
|
||||
|
||||
protected 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 abstract MapWrapper<K, V> build();
|
||||
|
||||
public abstract MapWrapper<K, V> buildUnmodifiableMap();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<K, V> extends AbstractMapWrapper<K, V, MapWrapper<
|
|||
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;
|
||||
public static final class Builder<K, V> extends AbstractMapWrapper.Builder<K, V> {
|
||||
|
||||
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;
|
||||
super(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MapWrapper<K, V> build() {
|
||||
return new MapWrapper<>(map, keyChecker, valueChecker);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MapWrapper<K, V> buildUnmodifiableMap() {
|
||||
return new MapWrapper<>(Collections.unmodifiableMap(map), keyChecker, valueChecker);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue