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