AbstractMultiValuedMapDecorator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.multimap;

  18. import java.io.Serializable;
  19. import java.util.Collection;
  20. import java.util.Map;
  21. import java.util.Map.Entry;
  22. import java.util.Objects;
  23. import java.util.Set;

  24. import org.apache.commons.collections4.MapIterator;
  25. import org.apache.commons.collections4.MultiSet;
  26. import org.apache.commons.collections4.MultiValuedMap;

  27. /**
  28.  * Decorates another {@code MultiValuedMap} to provide additional behavior.
  29.  * <p>
  30.  * Each method call made on this {@code MultiValuedMap} is forwarded to the
  31.  * decorated {@code MultiValuedMap}. This class is used as a framework to build
  32.  * to extensions such as synchronized and unmodifiable behavior.
  33.  * </p>
  34.  *
  35.  * @param <K> the type of key elements
  36.  * @param <V> the type of value elements
  37.  * @since 4.1
  38.  */
  39. public abstract class AbstractMultiValuedMapDecorator<K, V>
  40.         implements MultiValuedMap<K, V>, Serializable {

  41.     /** Serialization version */
  42.     private static final long serialVersionUID = 20150612L;

  43.     /** MultiValuedMap to decorate */
  44.     private final MultiValuedMap<K, V> map;

  45.     /**
  46.      * Constructor that wraps (not copies).
  47.      *
  48.      * @param map  the map to decorate, must not be null
  49.      * @throws NullPointerException if the map is null
  50.      */
  51.     protected AbstractMultiValuedMapDecorator(final MultiValuedMap<K, V> map) {
  52.         this.map = Objects.requireNonNull(map, "map");
  53.     }

  54.     @Override
  55.     public Map<K, Collection<V>> asMap() {
  56.         return decorated().asMap();
  57.     }

  58.     @Override
  59.     public void clear() {
  60.         decorated().clear();
  61.     }

  62.     @Override
  63.     public boolean containsKey(final Object key) {
  64.         return decorated().containsKey(key);
  65.     }

  66.     @Override
  67.     public boolean containsMapping(final Object key, final Object value) {
  68.         return decorated().containsMapping(key, value);
  69.     }

  70.     @Override
  71.     public boolean containsValue(final Object value) {
  72.         return decorated().containsValue(value);
  73.     }

  74.     /**
  75.      * The decorated multivalued map.
  76.      *
  77.      * @return the map to decorate
  78.      */
  79.     protected MultiValuedMap<K, V> decorated() {
  80.         return map;
  81.     }

  82.     @Override
  83.     public Collection<Entry<K, V>> entries() {
  84.         return decorated().entries();
  85.     }

  86.     @Override
  87.     public boolean equals(final Object object) {
  88.         if (object == this) {
  89.             return true;
  90.         }
  91.         return decorated().equals(object);
  92.     }

  93.     @Override
  94.     public Collection<V> get(final K key) {
  95.         return decorated().get(key);
  96.     }

  97.     @Override
  98.     public int hashCode() {
  99.         return decorated().hashCode();
  100.     }

  101.     @Override
  102.     public boolean isEmpty() {
  103.         return decorated().isEmpty();
  104.     }

  105.     @Override
  106.     public MultiSet<K> keys() {
  107.         return decorated().keys();
  108.     }

  109.     @Override
  110.     public Set<K> keySet() {
  111.         return decorated().keySet();
  112.     }

  113.     @Override
  114.     public MapIterator<K, V> mapIterator() {
  115.         return decorated().mapIterator();
  116.     }

  117.     @Override
  118.     public boolean put(final K key, final V value) {
  119.         return decorated().put(key, value);
  120.     }

  121.     @Override
  122.     public boolean putAll(final K key, final Iterable<? extends V> values) {
  123.         return decorated().putAll(key, values);
  124.     }

  125.     @Override
  126.     public boolean putAll(final Map<? extends K, ? extends V> map) {
  127.         return decorated().putAll(map);
  128.     }

  129.     @Override
  130.     public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
  131.         return decorated().putAll(map);
  132.     }

  133.     @Override
  134.     public Collection<V> remove(final Object key) {
  135.         return decorated().remove(key);
  136.     }

  137.     @Override
  138.     public boolean removeMapping(final Object key, final Object item) {
  139.         return decorated().removeMapping(key, item);
  140.     }

  141.     @Override
  142.     public int size() {
  143.         return decorated().size();
  144.     }

  145.     @Override
  146.     public String toString() {
  147.         return decorated().toString();
  148.     }

  149.     @Override
  150.     public Collection<V> values() {
  151.         return decorated().values();
  152.     }

  153. }