/* * Copyright 2022-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; @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; } }