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   * @since 4.1
41   */
42  public abstract class AbstractMultiValuedMapDecorator<K, V>
43          implements MultiValuedMap<K, V>, Serializable {
44  
45      /** Serialization version */
46      private static final long serialVersionUID = 20150612L;
47  
48      /** MultiValuedMap to decorate */
49      private final MultiValuedMap<K, V> map;
50  
51      /**
52       * Constructor that wraps (not copies).
53       *
54       * @param map  the map to decorate, must not be null
55       * @throws NullPointerException if the map is null
56       */
57      protected AbstractMultiValuedMapDecorator(final MultiValuedMap<K, V> map) {
58          this.map = Objects.requireNonNull(map, "map");
59      }
60  
61      @Override
62      public Map<K, Collection<V>> asMap() {
63          return decorated().asMap();
64      }
65  
66      @Override
67      public void clear() {
68          decorated().clear();
69      }
70  
71      @Override
72      public boolean containsKey(final Object key) {
73          return decorated().containsKey(key);
74      }
75  
76      @Override
77      public boolean containsMapping(final Object key, final Object value) {
78          return decorated().containsMapping(key, value);
79      }
80  
81      @Override
82      public boolean containsValue(final Object value) {
83          return decorated().containsValue(value);
84      }
85  
86      /**
87       * The decorated multivalued map.
88       *
89       * @return the map to decorate
90       */
91      protected MultiValuedMap<K, V> decorated() {
92          return map;
93      }
94  
95      @Override
96      public Collection<Entry<K, V>> entries() {
97          return decorated().entries();
98      }
99  
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 }