    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.multimap;
    018
    019import java.io.Serializable;
    020import java.util.Collection;
    021import java.util.Map;
    022import java.util.Map.Entry;
    023import java.util.Objects;
    024import java.util.Set;
    025
    026import org.apache.commons.collections4.MapIterator;
    027import org.apache.commons.collections4.MultiSet;
    028import org.apache.commons.collections4.MultiValuedMap;
    029
    030/**
    031 * Decorates another {@code MultiValuedMap} to provide additional behavior.
    032 * <p>
    033 * Each method call made on this {@code MultiValuedMap} is forwarded to the
    034 * decorated {@code MultiValuedMap}. This class is used as a framework to build
    035 * to extensions such as synchronized and unmodifiable behavior.
    036 * </p>
    037 *
    038 * @param <K> the type of key elements
    039 * @param <V> the type of value elements
    040 * @since 4.1
    041 */
    042public abstract class AbstractMultiValuedMapDecorator<K, V>
    043        implements MultiValuedMap<K, V>, Serializable {
    044
    045    /** Serialization version */
    046    private static final long serialVersionUID = 20150612L;
    047
    048    /** MultiValuedMap to decorate */
    049    private final MultiValuedMap<K, V> map;
    050
    051    /**
    052     * Constructor that wraps (not copies).
    053     *
    054     * @param map  the map to decorate, must not be null
    055     * @throws NullPointerException if the map is null
    056     */
    057    protected AbstractMultiValuedMapDecorator(final MultiValuedMap<K, V> map) {
    058        this.map = Objects.requireNonNull(map, "map");
    059    }
    060
    061    @Override
    062    public Map<K, Collection<V>> asMap() {
    063        return decorated().asMap();
    064    }
    065
    066    @Override
    067    public void clear() {
    068        decorated().clear();
    069    }
    070
    071    @Override
    072    public boolean containsKey(final Object key) {
    073        return decorated().containsKey(key);
    074    }
    075
    076    @Override
    077    public boolean containsMapping(final Object key, final Object value) {
    078        return decorated().containsMapping(key, value);
    079    }
    080
    081    @Override
    082    public boolean containsValue(final Object value) {
    083        return decorated().containsValue(value);
    084    }
    085
    086    /**
    087     * The decorated multivalued map.
    088     *
    089     * @return the map to decorate
    090     */
    091    protected MultiValuedMap<K, V> decorated() {
    092        return map;
    093    }
    094
    095    @Override
    096    public Collection<Entry<K, V>> entries() {
    097        return decorated().entries();
    098    }
    099
    100    @Override
    101    public boolean equals(final Object object) {
    102        if (object == this) {
    103            return true;
    104        }
    105        return decorated().equals(object);
    106    }
    107
    108    @Override
    109    public Collection<V> get(final K key) {
    110        return decorated().get(key);
    111    }
    112
    113    @Override
    114    public int hashCode() {
    115        return decorated().hashCode();
    116    }
    117
    118    @Override
    119    public boolean isEmpty() {
    120        return decorated().isEmpty();
    121    }
    122
    123    @Override
    124    public MultiSet<K> keys() {
    125        return decorated().keys();
    126    }
    127
    128    @Override
    129    public Set<K> keySet() {
    130        return decorated().keySet();
    131    }
    132
    133    @Override
    134    public MapIterator<K, V> mapIterator() {
    135        return decorated().mapIterator();
    136    }
    137
    138    @Override
    139    public boolean put(final K key, final V value) {
    140        return decorated().put(key, value);
    141    }
    142
    143    @Override
    144    public boolean putAll(final K key, final Iterable<? extends V> values) {
    145        return decorated().putAll(key, values);
    146    }
    147
    148    @Override
    149    public boolean putAll(final Map<? extends K, ? extends V> map) {
    150        return decorated().putAll(map);
    151    }
    152
    153    @Override
    154    public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
    155        return decorated().putAll(map);
    156    }
    157
    158    @Override
    159    public Collection<V> remove(final Object key) {
    160        return decorated().remove(key);
    161    }
    162
    163    @Override
    164    public boolean removeMapping(final Object key, final Object item) {
    165        return decorated().removeMapping(key, item);
    166    }
    167
    168    @Override
    169    public int size() {
    170        return decorated().size();
    171    }
    172
    173    @Override
    174    public String toString() {
    175        return decorated().toString();
    176    }
    177
    178    @Override
    179    public Collection<V> values() {
    180        return decorated().values();
    181    }
    182
    183}