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 *      http://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.map;
018
019import java.util.Collection;
020import java.util.Map;
021import java.util.Objects;
022import java.util.Set;
023
024/**
025 * Provides a base decorator that enables additional functionality to be added
026 * to a Map via decoration.
027 * <p>
028 * Methods are forwarded directly to the decorated map.
029 * </p>
030 * <p>
031 * This implementation does not perform any special processing with
032 * {@link #entrySet()}, {@link #keySet()} or {@link #values()}. Instead
033 * it simply returns the set/collection from the wrapped map. This may be
034 * undesirable, for example if you are trying to write a validating
035 * implementation it would provide a loophole around the validation.
036 * But, you might want that loophole, so this class is kept simple.
037 * </p>
038 *
039 * @param <K> the type of the keys in the map
040 * @param <V> the type of the values in the map
041 * @since 3.0
042 */
043public abstract class AbstractMapDecorator<K, V> extends AbstractIterableMap<K, V> {
044
045    /** The map to decorate */
046    transient Map<K, V> map;
047
048    /**
049     * Constructor only used in deserialization, do not use otherwise.
050     * @since 3.1
051     */
052    protected AbstractMapDecorator() {
053    }
054
055    /**
056     * Constructor that wraps (not copies).
057     *
058     * @param map  the map to decorate, must not be null
059     * @throws NullPointerException if the map is null
060     */
061    protected AbstractMapDecorator(final Map<K, V> map) {
062        this.map = Objects.requireNonNull(map, "map");
063    }
064
065    @Override
066    public void clear() {
067        decorated().clear();
068    }
069
070    @Override
071    public boolean containsKey(final Object key) {
072        return decorated().containsKey(key);
073    }
074
075    @Override
076    public boolean containsValue(final Object value) {
077        return decorated().containsValue(value);
078    }
079
080    /**
081     * Gets the map being decorated.
082     *
083     * @return the decorated map
084     */
085    protected Map<K, V> decorated() {
086        return map;
087    }
088
089    @Override
090    public Set<Map.Entry<K, V>> entrySet() {
091        return decorated().entrySet();
092    }
093
094    @Override
095    public boolean equals(final Object object) {
096        if (object == this) {
097            return true;
098        }
099        return decorated().equals(object);
100    }
101
102    @Override
103    public V get(final Object key) {
104        return decorated().get(key);
105    }
106
107    @Override
108    public int hashCode() {
109        return decorated().hashCode();
110    }
111
112    @Override
113    public boolean isEmpty() {
114        return decorated().isEmpty();
115    }
116
117    @Override
118    public Set<K> keySet() {
119        return decorated().keySet();
120    }
121
122    @Override
123    public V put(final K key, final V value) {
124        return decorated().put(key, value);
125    }
126
127    @Override
128    public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
129        decorated().putAll(mapToCopy);
130    }
131
132    @Override
133    public V remove(final Object key) {
134        return decorated().remove(key);
135    }
136
137    @Override
138    public int size() {
139        return decorated().size();
140    }
141
142    @Override
143    public String toString() {
144        return decorated().toString();
145    }
146
147    @Override
148    public Collection<V> values() {
149        return decorated().values();
150    }
151
152}