View Javadoc
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  
19  import java.io.Serializable;
20  import java.util.Collection;
21  import java.util.Map;
22  import java.util.Map.Entry;
23  import java.util.Objects;
24  import java.util.Set;
25  
26  import org.apache.commons.collections4.MapIterator;
27  import org.apache.commons.collections4.MultiSet;
28  import org.apache.commons.collections4.MultiValuedMap;
29  
30  /**
31   * Decorates another {@code MultiValuedMap} to provide additional behavior.
32   * <p>
33   * Each method call made on this {@code MultiValuedMap} is forwarded to the
34   * decorated {@code MultiValuedMap}. This class is used as a framework to build
35   * to extensions such as synchronized and unmodifiable behavior.
36   * </p>
37   *
38   * @param <K> the type of key elements
39   * @param <V> the type of value elements
40   *
41   * @since 4.1
42   */
43  public abstract class AbstractMultiValuedMapDecorator<K, V>
44          implements MultiValuedMap<K, V>, Serializable {
45  
46      /** Serialization version */
47      private static final long serialVersionUID = 20150612L;
48  
49      /** MultiValuedMap to decorate */
50      private final MultiValuedMap<K, V> map;
51  
52      /**
53       * Constructor that wraps (not copies).
54       *
55       * @param map  the map to decorate, must not be null
56       * @throws NullPointerException if the map is null
57       */
58      protected AbstractMultiValuedMapDecorator(final MultiValuedMap<K, V> map) {
59          this.map = Objects.requireNonNull(map, "map");
60      }
61  
62      @Override
63      public Map<K, Collection<V>> asMap() {
64          return decorated().asMap();
65      }
66  
67      @Override
68      public void clear() {
69          decorated().clear();
70      }
71  
72      @Override
73      public boolean containsKey(final Object key) {
74          return decorated().containsKey(key);
75      }
76  
77      @Override
78      public boolean containsMapping(final Object key, final Object value) {
79          return decorated().containsMapping(key, value);
80      }
81  
82      @Override
83      public boolean containsValue(final Object value) {
84          return decorated().containsValue(value);
85      }
86  
87      /**
88       * The decorated multivalued map.
89       *
90       * @return the map to decorate
91       */
92      protected MultiValuedMap<K, V> decorated() {
93          return map;
94      }
95  
96      @Override
97      public Collection<Entry<K, V>> entries() {
98          return decorated().entries();
99      }
100 
101     @Override
102     public boolean equals(final Object object) {
103         if (object == this) {
104             return true;
105         }
106         return decorated().equals(object);
107     }
108 
109     @Override
110     public Collection<V> get(final K key) {
111         return decorated().get(key);
112     }
113 
114     @Override
115     public int hashCode() {
116         return decorated().hashCode();
117     }
118 
119     @Override
120     public boolean isEmpty() {
121         return decorated().isEmpty();
122     }
123 
124     @Override
125     public MultiSet<K> keys() {
126         return decorated().keys();
127     }
128 
129     @Override
130     public Set<K> keySet() {
131         return decorated().keySet();
132     }
133 
134     @Override
135     public MapIterator<K, V> mapIterator() {
136         return decorated().mapIterator();
137     }
138 
139     @Override
140     public boolean put(final K key, final V value) {
141         return decorated().put(key, value);
142     }
143 
144     @Override
145     public boolean putAll(final K key, final Iterable<? extends V> values) {
146         return decorated().putAll(key, values);
147     }
148 
149     @Override
150     public boolean putAll(final Map<? extends K, ? extends V> map) {
151         return decorated().putAll(map);
152     }
153 
154     @Override
155     public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
156         return decorated().putAll(map);
157     }
158 
159     @Override
160     public Collection<V> remove(final Object key) {
161         return decorated().remove(key);
162     }
163 
164     @Override
165     public boolean removeMapping(final Object key, final Object item) {
166         return decorated().removeMapping(key, item);
167     }
168 
169     @Override
170     public int size() {
171         return decorated().size();
172     }
173 
174     @Override
175     public String toString() {
176         return decorated().toString();
177     }
178 
179     @Override
180     public Collection<V> values() {
181         return decorated().values();
182     }
183 
184 }