重构。

feature/net-util
ZhouXY108 2023-05-31 20:01:43 +08:00
parent 122fff1da6
commit b732578e6d
2 changed files with 69 additions and 37 deletions

View File

@ -60,7 +60,16 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
return getSelf(); 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)) { if (this.map.containsKey(key)) {
return Optional.ofNullable(this.map.get(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); 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() { public final Map<K, V> exportMap() {
return this.map; return this.map;
} }
@ -121,4 +143,46 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
} }
protected abstract T getSelf(); 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();
}
} }

View File

@ -20,13 +20,10 @@ import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.function.Consumer; import java.util.function.Consumer;
import javax.annotation.Nullable;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
@Beta @Beta
@ -64,47 +61,18 @@ public final class MapWrapper<K, V> extends AbstractMapWrapper<K, V, MapWrapper<
return new Builder<>(new TreeMap<>(comparator)); return new Builder<>(new TreeMap<>(comparator));
} }
public static final class Builder<K, V> { public static final class Builder<K, V> extends AbstractMapWrapper.Builder<K, V> {
private final Map<K, V> map;
private Consumer<K> keyChecker;
private Consumer<V> valueChecker;
private Builder(Map<K, V> map) { private Builder(Map<K, V> map) {
this.map = map; super(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;
} }
@Override
public MapWrapper<K, V> build() { public MapWrapper<K, V> build() {
return new MapWrapper<>(map, keyChecker, valueChecker); return new MapWrapper<>(map, keyChecker, valueChecker);
} }
@Override
public MapWrapper<K, V> buildUnmodifiableMap() { public MapWrapper<K, V> buildUnmodifiableMap() {
return new MapWrapper<>(Collections.unmodifiableMap(map), keyChecker, valueChecker); return new MapWrapper<>(Collections.unmodifiableMap(map), keyChecker, valueChecker);
} }