001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.splitmap;
018
019import java.util.Collection;
020import java.util.Map;
021import java.util.Objects;
022import java.util.Set;
023
024import org.apache.commons.collections4.Get;
025import org.apache.commons.collections4.IterableGet;
026import org.apache.commons.collections4.MapIterator;
027import org.apache.commons.collections4.map.EntrySetToMapIteratorAdapter;
028
029/**
030 * {@link IterableGet} that uses a {@link Map}<K, V> for the
031 * {@link Get Get}<K, V> implementation.
032 *
033 * @param <K> The type of the keys in this map
034 * @param <V> The type of the values in this map
035 * @since 4.0
036 */
037public class AbstractIterableGetMapDecorator<K, V> implements IterableGet<K, V> {
038
039    /** The map to decorate */
040    transient Map<K, V> map;
041
042    /**
043     * Constructor only used in deserialization, do not use otherwise.
044     */
045    protected AbstractIterableGetMapDecorator() {
046    }
047
048    /**
049     * Create a new AbstractSplitMapDecorator.
050     *
051     * @param map The map to decorate, must not be null
052     * @throws NullPointerException if map is null
053     */
054    public AbstractIterableGetMapDecorator(final Map<K, V> map) {
055        this.map = Objects.requireNonNull(map, "map");
056    }
057
058    @Override
059    public boolean containsKey(final Object key) {
060        return decorated().containsKey(key);
061    }
062
063    @Override
064    public boolean containsValue(final Object value) {
065        return decorated().containsValue(value);
066    }
067
068    /**
069     * Gets the map being decorated.
070     *
071     * @return The decorated map
072     */
073    protected Map<K, V> decorated() {
074        return map;
075    }
076
077    @Override
078    public Set<Map.Entry<K, V>> entrySet() {
079        return decorated().entrySet();
080    }
081
082    @Override
083    public boolean equals(final Object object) {
084        if (object == this) {
085            return true;
086        }
087        return decorated().equals(object);
088    }
089
090    @Override
091    public V get(final Object key) {
092        return decorated().get(key);
093    }
094
095    @Override
096    public int hashCode() {
097        return decorated().hashCode();
098    }
099
100    @Override
101    public boolean isEmpty() {
102        return decorated().isEmpty();
103    }
104
105    @Override
106    public Set<K> keySet() {
107        return decorated().keySet();
108    }
109
110    /**
111     * Gets a MapIterator over this Get.
112     *
113     * @return MapIterator&lt;K, V&gt;
114     */
115    @Override
116    public MapIterator<K, V> mapIterator() {
117        return new EntrySetToMapIteratorAdapter<>(entrySet());
118    }
119
120    @Override
121    public V remove(final Object key) {
122        return decorated().remove(key);
123    }
124
125    @Override
126    public int size() {
127        return decorated().size();
128    }
129
130    @Override
131    public String toString() {
132        return decorated().toString();
133    }
134
135    @Override
136    public Collection<V> values() {
137        return decorated().values();
138    }
139
140}