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