2023-05-13 12:53:57 +08:00
|
|
|
|
/*
|
2024-10-12 00:57:35 +08:00
|
|
|
|
* Copyright 2023-2024 the original author or authors.
|
2023-05-13 12:53:57 +08:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-06-29 01:19:21 +08:00
|
|
|
|
package xyz.zhouxy.plusone.commons.collection;
|
2023-05-09 01:03:21 +08:00
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Map.Entry;
|
2024-11-02 11:47:41 +08:00
|
|
|
|
import java.util.Objects;
|
2023-05-09 01:03:21 +08:00
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
2024-12-04 19:37:26 +08:00
|
|
|
|
import javax.annotation.Nonnull;
|
2023-05-09 01:03:21 +08:00
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
|
|
import com.google.common.annotations.Beta;
|
|
|
|
|
|
2024-12-04 19:37:26 +08:00
|
|
|
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
2023-08-09 20:23:31 +08:00
|
|
|
|
|
2024-12-04 19:37:26 +08:00
|
|
|
|
/**
|
|
|
|
|
* AbstractMapWrapper
|
|
|
|
|
*
|
|
|
|
|
* <p>
|
|
|
|
|
* 包装了一个 {@link Map}。
|
|
|
|
|
* 主要在于获取值时,如果 Key 不存在,是抛出异常;如果 Key 存在,则将 value 包装在 {@link Optional} 中 返回。
|
|
|
|
|
* 此外可以定义 Key 和 Value 的检查规则。
|
|
|
|
|
* </p>
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
|
|
|
|
* @since 1.0
|
|
|
|
|
*/
|
2023-05-09 01:03:21 +08:00
|
|
|
|
@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;
|
|
|
|
|
|
2024-12-04 19:37:26 +08:00
|
|
|
|
protected AbstractMapWrapper(Map<K, V> map,
|
|
|
|
|
@Nullable Consumer<K> keyChecker,
|
|
|
|
|
@Nullable Consumer<V> valueChecker) {
|
2023-05-09 01:03:21 +08:00
|
|
|
|
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) {
|
2024-05-28 09:35:04 +08:00
|
|
|
|
m.forEach(this::put);
|
2023-05-09 01:03:21 +08:00
|
|
|
|
return getSelf();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 20:01:43 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取 {@code map} 中的值。如果 {@code key} 不存在,则抛出异常。
|
|
|
|
|
* 将 {@code value}(可为 {@code null})装进 {@link Optional} 中后返回。
|
|
|
|
|
* <i>为了这碟醋包的这盘饺子。</i>
|
|
|
|
|
*
|
|
|
|
|
* @param key 键
|
|
|
|
|
* @return 可缺失的值
|
|
|
|
|
* @throws IllegalArgumentException key 不存在时抛出。
|
|
|
|
|
*/
|
2024-12-04 19:37:26 +08:00
|
|
|
|
@Nonnull
|
2023-05-31 20:01:43 +08:00
|
|
|
|
public Optional<V> get(K key) {
|
2024-12-04 19:37:26 +08:00
|
|
|
|
AssertTools.checkArgument(this.map.containsKey(key), "Key does not exist");
|
2023-06-27 01:57:43 +08:00
|
|
|
|
return Optional.ofNullable(this.map.get(key));
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 01:03:21 +08:00
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 20:01:43 +08:00
|
|
|
|
public final V putIfAbsent(K key, V value) {
|
2024-09-03 15:34:52 +08:00
|
|
|
|
if (this.keyChecker != null) {
|
|
|
|
|
this.keyChecker.accept(key);
|
|
|
|
|
}
|
|
|
|
|
if (this.valueChecker != null) {
|
|
|
|
|
this.valueChecker.accept(value);
|
|
|
|
|
}
|
2023-05-31 20:01:43 +08:00
|
|
|
|
return this.map.putIfAbsent(key, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public final V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
2024-09-03 15:34:52 +08:00
|
|
|
|
if (this.keyChecker != null) {
|
|
|
|
|
this.keyChecker.accept(key);
|
|
|
|
|
}
|
|
|
|
|
Function<? super K, ? extends V> func = (K k) -> {
|
|
|
|
|
V value = mappingFunction.apply(k);
|
|
|
|
|
if (this.valueChecker != null) {
|
|
|
|
|
this.valueChecker.accept(value);
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
};
|
2024-12-27 15:47:31 +08:00
|
|
|
|
return this.map.computeIfAbsent(key, func);
|
2023-05-31 20:01:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 01:03:21 +08:00
|
|
|
|
public final Map<K, V> exportMap() {
|
|
|
|
|
return this.map;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 15:34:52 +08:00
|
|
|
|
public final Map<K, V> exportUnmodifiableMap() {
|
2023-05-09 01:03:21 +08:00
|
|
|
|
return Collections.unmodifiableMap(this.map);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract T getSelf();
|
2023-05-31 20:01:43 +08:00
|
|
|
|
|
2023-06-05 14:42:05 +08:00
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return this.map.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-02 11:47:41 +08:00
|
|
|
|
@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;
|
2024-12-04 19:37:26 +08:00
|
|
|
|
AbstractMapWrapper<?, ?, ?> other = (AbstractMapWrapper<?, ?, ?>) obj;
|
2024-11-02 11:47:41 +08:00
|
|
|
|
return Objects.equals(map, other.map);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-29 01:19:21 +08:00
|
|
|
|
public abstract static class Builder<K, V, T extends AbstractMapWrapper<K, V, T>> {
|
2023-05-31 20:01:43 +08:00
|
|
|
|
protected final Map<K, V> map;
|
|
|
|
|
protected Consumer<K> keyChecker;
|
|
|
|
|
protected Consumer<V> valueChecker;
|
|
|
|
|
|
|
|
|
|
protected Builder(Map<K, V> map) {
|
|
|
|
|
this.map = map;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 01:57:43 +08:00
|
|
|
|
public Builder<K, V, T> keyChecker(@Nullable Consumer<K> keyChecker) {
|
2023-05-31 20:01:43 +08:00
|
|
|
|
this.keyChecker = keyChecker;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 01:57:43 +08:00
|
|
|
|
public Builder<K, V, T> valueChecker(@Nullable Consumer<V> valueChecker) {
|
2023-05-31 20:01:43 +08:00
|
|
|
|
this.valueChecker = valueChecker;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 01:57:43 +08:00
|
|
|
|
public Builder<K, V, T> put(K key, V value) {
|
2023-05-31 20:01:43 +08:00
|
|
|
|
if (this.keyChecker != null) {
|
|
|
|
|
this.keyChecker.accept(key);
|
|
|
|
|
}
|
|
|
|
|
if (this.valueChecker != null) {
|
|
|
|
|
this.valueChecker.accept(value);
|
|
|
|
|
}
|
|
|
|
|
this.map.put(key, value);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 01:57:43 +08:00
|
|
|
|
public Builder<K, V, T> putAll(Map<? extends K, ? extends V> m) {
|
2024-05-28 09:35:04 +08:00
|
|
|
|
m.forEach(this::put);
|
2023-05-31 20:01:43 +08:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 01:57:43 +08:00
|
|
|
|
public abstract T build();
|
2023-05-31 20:01:43 +08:00
|
|
|
|
|
2023-06-27 01:57:43 +08:00
|
|
|
|
public abstract T buildUnmodifiableMap();
|
2023-05-31 20:01:43 +08:00
|
|
|
|
}
|
2023-05-09 01:03:21 +08:00
|
|
|
|
}
|