001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the License.
004 *
005 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
006 * specific language governing permission and limitations under the License.
007 *
008 * When distributing Covered Software, include this CDDL Header Notice in each file and include
009 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
010 * Header, with the fields enclosed by brackets [] replaced by your own identifying
011 * information: "Portions copyright [year] [name of copyright owner]".
012 *
013 * Copyright 2015 ForgeRock AS.
014 */
015
016package org.forgerock.util;
017
018// Java SE
019import java.util.Collection;
020import java.util.Map;
021import java.util.Set;
022
023/**
024 * Wraps another map. All methods simply call through to the wrapped map.
025 * Subclasses are expected to override one or more of these methods.
026 *
027 * @param <K>
028 *            The type of key.
029 * @param <V>
030 *            The type of value.
031 */
032public class MapDecorator<K, V> implements Map<K, V> {
033
034    /** The map wrapped by this decorator. */
035    protected final Map<K, V> map;
036
037    /**
038     * Constructs a new map decorator, wrapping the specified map.
039     *
040     * @param map
041     *            the map to wrap with the decorator.
042     * @throws NullPointerException
043     *             if {@code map} is {@code null}.
044     */
045    public MapDecorator(Map<K, V> map) {
046        if (map == null) {
047            throw new NullPointerException();
048        }
049        this.map = map;
050    }
051
052    /**
053     * Returns the number of key-value mappings in this map.
054     */
055    @Override
056    public int size() {
057        return map.size();
058    }
059
060    /**
061     * Returns {@code true} if the map contains no key-value mappings.
062     */
063    @Override
064    public boolean isEmpty() {
065        return map.isEmpty();
066    }
067
068    /**
069     * Returns {@code true} if this map contains a mapping for the specified
070     * key.
071     *
072     * @param key
073     *            the key whose presence in this map is to be tested.
074     * @return {@code true} if this map contains a mapping for the specified
075     *         key.
076     */
077    @Override
078    public boolean containsKey(Object key) {
079        return map.containsKey(key);
080    }
081
082    /**
083     * Returns {@code true} if the map maps one or more keys to the specified
084     * value.
085     *
086     * @param value
087     *            the value whose presence in the map is to be tested.
088     * @return {@code true} if the map maps one or more keys to the specified
089     *         value.
090     */
091    @Override
092    public boolean containsValue(Object value) {
093        return map.containsValue(value);
094    }
095
096    /**
097     * Returns the value to which the specified key is mapped, or {@code null}
098     * if the map contains no mapping for the key.
099     *
100     * @param key
101     *            the key whose associated value is to be returned.
102     * @return the value to which the specified key is mapped, or {@code null}
103     *         if no mapping.
104     */
105    @Override
106    public V get(Object key) {
107        return map.get(key);
108    }
109
110    /**
111     * Associates the specified value with the specified key in the map.
112     *
113     * @param key
114     *            key with which the specified value is to be associated.
115     * @param value
116     *            value to be associated with the specified key.
117     * @return the previous value associated with key, or {@code null} if no
118     *         mapping.
119     */
120    @Override
121    public V put(K key, V value) {
122        return map.put(key, value);
123    }
124
125    /**
126     * Removes the mapping for a key from the map if it is present.
127     *
128     * @param key
129     *            key whose mapping is to be removed from the map.
130     * @return the previous value associated with key, or {@code null} if no
131     *         mapping.
132     */
133    @Override
134    public V remove(Object key) {
135        return map.remove(key);
136    }
137
138    /**
139     * Copies all of the mappings from the specified map to the map.
140     *
141     * @param m
142     *            mappings to be stored in the map.
143     */
144    @Override
145    public void putAll(Map<? extends K, ? extends V> m) {
146        map.putAll(m);
147    }
148
149    /**
150     * Removes all of the mappings from the map.
151     */
152    @Override
153    public void clear() {
154        map.clear();
155    }
156
157    /**
158     * Returns a {@link Set} view of the keys contained in the map.
159     */
160    @Override
161    public Set<K> keySet() {
162        return map.keySet();
163    }
164
165    /**
166     * Returns a {@link Collection} view of the values contained in the map.
167     */
168    @Override
169    public Collection<V> values() {
170        return map.values();
171    }
172
173    /**
174     * Returns a {@link Set} view of the mappings contained in the map.
175     */
176    @Override
177    public Set<Map.Entry<K, V>> entrySet() {
178        return map.entrySet();
179    }
180
181    /**
182     * Returns the hash code value for the map.
183     */
184    @Override
185    public int hashCode() {
186        return map.hashCode();
187    }
188
189    /**
190     * Compares the specified object with the map for equality.
191     *
192     * @param o
193     *            object to be compared for equality with the map.
194     * @return {@code true} if the specified object is equal to the map.
195     */
196    @Override
197    public boolean equals(Object o) {
198        return map.equals(o);
199    }
200}