diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java deleted file mode 100644 index 04a974b..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java +++ /dev/null @@ -1,234 +0,0 @@ -/* - * Copyright 2023-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package xyz.zhouxy.plusone.commons.collection; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.function.Consumer; -import java.util.function.Function; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import com.google.common.annotations.Beta; - -import xyz.zhouxy.plusone.commons.util.AssertTools; - -/** - * AbstractMapWrapper - * - *

- * 包装了一个 {@link Map}。 - * 主要在于获取值时,如果 Key 不存在,是抛出异常;如果 Key 存在,则将 value 包装在 {@link Optional} 中 返回。 - * 此外可以定义 Key 和 Value 的检查规则。 - *

- * - * @author ZhouXY - * @since 1.0 - */ -@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) { - m.forEach(this::put); - return getSelf(); - } - - /** - * 获取 {@code map} 中的值。如果 {@code key} 不存在,则抛出异常。 - * 将 {@code value}(可为 {@code null})装进 {@link Optional} 中后返回。 - * 为了这碟醋包的这盘饺子。 - * - * @param key 键 - * @return 可缺失的值 - * @throws IllegalArgumentException key 不存在时抛出。 - */ - @Nonnull - public Optional get(K key) { - AssertTools.checkArgument(this.map.containsKey(key), "Key does not exist"); - return Optional.ofNullable(this.map.get(key)); - } - - @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 V putIfAbsent(K key, V value) { - if (this.keyChecker != null) { - this.keyChecker.accept(key); - } - if (this.valueChecker != null) { - this.valueChecker.accept(value); - } - return this.map.putIfAbsent(key, value); - } - - public final V computeIfAbsent(K key, Function mappingFunction) { - if (this.keyChecker != null) { - this.keyChecker.accept(key); - } - Function func = (K k) -> { - V value = mappingFunction.apply(k); - if (this.valueChecker != null) { - this.valueChecker.accept(value); - } - return value; - }; - return this.map.computeIfAbsent(key, func); - } - - public final Map exportMap() { - return this.map; - } - - public final Map exportUnmodifiableMap() { - return Collections.unmodifiableMap(this.map); - } - - protected abstract T getSelf(); - - @Override - public String toString() { - return this.map.toString(); - } - - @Override - public int hashCode() { - return Objects.hash(map); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - AbstractMapWrapper other = (AbstractMapWrapper) obj; - return Objects.equals(map, other.map); - } - - public abstract static class Builder> { - protected final Map map; - protected Consumer keyChecker; - protected Consumer valueChecker; - - protected Builder(Map map) { - this.map = map; - } - - public Builder keyChecker(@Nullable Consumer keyChecker) { - this.keyChecker = keyChecker; - return this; - } - - public Builder valueChecker(@Nullable Consumer valueChecker) { - this.valueChecker = valueChecker; - return this; - } - - public Builder 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 putAll(Map m) { - m.forEach(this::put); - return this; - } - - public abstract T build(); - - public abstract T buildUnmodifiableMap(); - } -} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/MapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/MapWrapper.java deleted file mode 100644 index 026bd9c..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/MapWrapper.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2023-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package xyz.zhouxy.plusone.commons.collection; - -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Map; -import java.util.SortedMap; -import java.util.TreeMap; -import java.util.function.Consumer; - -import com.google.common.annotations.Beta; - -/** - * MapWrapper - * - *

- * Map 包装器的默认实现 - *

- * - * @author ZhouXY - * @since 0.1.0 - */ -@Beta -public final class MapWrapper extends AbstractMapWrapper> { - - private MapWrapper(Map map, Consumer keyChecker, Consumer valueChecker) { - super(map, keyChecker, valueChecker); - } - - public static Builder wrap(Map map) { - return new Builder<>(map); - } - - public static Builder wrapHashMap() { - return new Builder<>(new HashMap<>()); - } - - public static Builder wrapHashMap(int initialCapacity) { - return new Builder<>(new HashMap<>(initialCapacity)); - } - - public static Builder wrapHashMap(int initialCapacity, float loadFactor) { - return new Builder<>(new HashMap<>(initialCapacity, loadFactor)); - } - - public static , V> Builder wrapTreeMap() { - return new Builder<>(new TreeMap<>()); - } - - public static Builder wrapTreeMap(SortedMap m) { - return new Builder<>(new TreeMap<>(m)); - } - - public static Builder wrapTreeMap(Comparator comparator) { - return new Builder<>(new TreeMap<>(comparator)); - } - - public static final class Builder extends AbstractMapWrapper.Builder> { - - private Builder(Map map) { - super(map); - } - - @Override - public MapWrapper build() { - return new MapWrapper<>(map, keyChecker, valueChecker); - } - - @Override - public MapWrapper buildUnmodifiableMap() { - return new MapWrapper<>(Collections.unmodifiableMap(map), keyChecker, valueChecker); - } - } - - @Override - protected MapWrapper getSelf() { - return this; - } -}