优化代码。

feature/net-util
ZhouXY108 2023-06-27 01:57:43 +08:00
parent f4a12f1695
commit 13fc8e40b5
2 changed files with 26 additions and 11 deletions

View File

@ -70,10 +70,25 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
* @throws IllegalArgumentException key * @throws IllegalArgumentException key
*/ */
public Optional<V> get(K key) { public Optional<V> get(K key) {
if (this.map.containsKey(key)) { if (!this.map.containsKey(key)) {
return Optional.ofNullable(this.map.get(key)); throw new IllegalArgumentException("Key does not exist");
} }
throw new IllegalArgumentException("Key does not exist"); return Optional.ofNullable(this.map.get(key));
}
/**
* {@code map} {@code key}
*
* @param key
* @return
* @throws IllegalArgumentException key
*/
@Nullable
public V getOrNull(K key) {
if (!this.map.containsKey(key)) {
throw new IllegalArgumentException("Key does not exist");
}
return this.map.get(key);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -149,7 +164,7 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
return this.map.toString(); return this.map.toString();
} }
protected abstract static class Builder<K, V> { protected abstract static class Builder<K, V, T extends AbstractMapWrapper<K, V, T>> {
protected final Map<K, V> map; protected final Map<K, V> map;
protected Consumer<K> keyChecker; protected Consumer<K> keyChecker;
protected Consumer<V> valueChecker; protected Consumer<V> valueChecker;
@ -158,17 +173,17 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
this.map = map; this.map = map;
} }
public Builder<K, V> keyChecker(@Nullable Consumer<K> keyChecker) { public Builder<K, V, T> keyChecker(@Nullable Consumer<K> keyChecker) {
this.keyChecker = keyChecker; this.keyChecker = keyChecker;
return this; return this;
} }
public Builder<K, V> valueChecker(@Nullable Consumer<V> valueChecker) { public Builder<K, V, T> valueChecker(@Nullable Consumer<V> valueChecker) {
this.valueChecker = valueChecker; this.valueChecker = valueChecker;
return this; return this;
} }
public Builder<K, V> put(K key, V value) { public Builder<K, V, T> put(K key, V value) {
if (this.keyChecker != null) { if (this.keyChecker != null) {
this.keyChecker.accept(key); this.keyChecker.accept(key);
} }
@ -179,15 +194,15 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
return this; return this;
} }
public Builder<K, V> putAll(Map<? extends K, ? extends V> m) { public Builder<K, V, T> putAll(Map<? extends K, ? extends V> m) {
for (Entry<? extends K, ? extends V> entry : m.entrySet()) { for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
put(entry.getKey(), entry.getValue()); put(entry.getKey(), entry.getValue());
} }
return this; return this;
} }
public abstract MapWrapper<K, V> build(); public abstract T build();
public abstract MapWrapper<K, V> buildUnmodifiableMap(); public abstract T buildUnmodifiableMap();
} }
} }

View File

@ -61,7 +61,7 @@ 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> extends AbstractMapWrapper.Builder<K, V> { public static final class Builder<K, V> extends AbstractMapWrapper.Builder<K, V, MapWrapper<K, V>> {
private Builder(Map<K, V> map) { private Builder(Map<K, V> map) {
super(map); super(map);