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    *      https://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.util.Collection;
20  import java.util.Map;
21  import java.util.Map.Entry;
22  import java.util.Set;
23  
24  import org.apache.commons.collections4.MapIterator;
25  import org.apache.commons.collections4.MultiSet;
26  import org.apache.commons.collections4.MultiValuedMap;
27  import org.apache.commons.collections4.Unmodifiable;
28  import org.apache.commons.collections4.collection.UnmodifiableCollection;
29  import org.apache.commons.collections4.iterators.UnmodifiableMapIterator;
30  import org.apache.commons.collections4.map.UnmodifiableMap;
31  import org.apache.commons.collections4.multiset.UnmodifiableMultiSet;
32  import org.apache.commons.collections4.set.UnmodifiableSet;
33  
34  /**
35   * Decorates another {@link MultiValuedMap} to ensure it can't be altered.
36   * <p>
37   * Attempts to modify it will result in an UnsupportedOperationException.
38   * </p>
39   *
40   * @param <K> The type of key elements
41   * @param <V> The type of value elements
42   * @since 4.1
43   */
44  public final class UnmodifiableMultiValuedMap<K, V>
45          extends AbstractMultiValuedMapDecorator<K, V> implements Unmodifiable {
46  
47      /** Serialization version */
48      private static final long serialVersionUID = 20150612L;
49  
50      /**
51       * Factory method to create an unmodifiable MultiValuedMap.
52       * <p>
53       * If the map passed in is already unmodifiable, it is returned.
54       * </p>
55       *
56       * @param <K> The type of key elements
57       * @param <V> The type of value elements
58       * @param map  The map to decorate, may not be null
59       * @return An unmodifiable MultiValuedMap
60       * @throws NullPointerException if map is null
61       */
62      @SuppressWarnings("unchecked")
63      public static <K, V> UnmodifiableMultiValuedMap<K, V> unmodifiableMultiValuedMap(
64              final MultiValuedMap<? extends K, ? extends V> map) {
65          if (map instanceof Unmodifiable) {
66              return (UnmodifiableMultiValuedMap<K, V>) map;
67          }
68          return new UnmodifiableMultiValuedMap<>(map);
69      }
70  
71      /**
72       * Constructor that wraps (not copies).
73       *
74       * @param map  The MultiValuedMap to decorate, may not be null
75       * @throws NullPointerException if the map is null
76       */
77      @SuppressWarnings("unchecked")
78      private UnmodifiableMultiValuedMap(final MultiValuedMap<? extends K, ? extends V> map) {
79          super((MultiValuedMap<K, V>) map);
80      }
81  
82      @Override
83      public Map<K, Collection<V>> asMap() {
84          return UnmodifiableMap.unmodifiableMap(decorated().asMap());
85      }
86  
87      /**
88       * Always throws {@link UnsupportedOperationException}.
89       *
90       * @throws UnsupportedOperationException Always thrown.
91       */
92      @Override
93      public void clear() {
94          throw new UnsupportedOperationException();
95      }
96  
97      @Override
98      public Collection<Entry<K, V>> entries() {
99          return UnmodifiableCollection.unmodifiableCollection(decorated().entries());
100     }
101 
102     @Override
103     public Collection<V> get(final K key) {
104         return UnmodifiableCollection.unmodifiableCollection(decorated().get(key));
105     }
106 
107     @Override
108     public MultiSet<K> keys() {
109         return UnmodifiableMultiSet.unmodifiableMultiSet(decorated().keys());
110     }
111 
112     @Override
113     public Set<K> keySet() {
114         return UnmodifiableSet.unmodifiableSet(decorated().keySet());
115     }
116 
117     /**
118      * {@inheritDoc}
119      * <p>
120      * The returned map iterator's {@link MapIterator#setValue(Object)} method is not supported
121      * and will throw an {@link UnsupportedOperationException}.
122      * </p>
123      */
124     @Override
125     public MapIterator<K, V> mapIterator() {
126         return UnmodifiableMapIterator.unmodifiableMapIterator(decorated().mapIterator());
127     }
128 
129     /**
130      * Always throws {@link UnsupportedOperationException}.
131      *
132      * @param key Ignored.
133      * @throws UnsupportedOperationException Always thrown.
134      */
135     @Override
136     public boolean put(final K key, final V value) {
137         throw new UnsupportedOperationException();
138     }
139 
140     /**
141      * Always throws {@link UnsupportedOperationException}.
142      *
143      * @param key Ignored.
144      * @param values Ignored.
145      * @throws UnsupportedOperationException Always thrown.
146      */
147     @Override
148     public boolean putAll(final K key, final Iterable<? extends V> values) {
149         throw new UnsupportedOperationException();
150     }
151 
152     /**
153      * Always throws {@link UnsupportedOperationException}.
154      *
155      * @param map Ignored.
156      * @throws UnsupportedOperationException Always thrown.
157      */
158     @Override
159     public boolean putAll(final Map<? extends K, ? extends V> map) {
160         throw new UnsupportedOperationException();
161     }
162 
163     /**
164      * Always throws {@link UnsupportedOperationException}.
165      *
166      * @param map Ignored.
167      * @throws UnsupportedOperationException Always thrown.
168      */
169     @Override
170     public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
171         throw new UnsupportedOperationException();
172     }
173 
174     /**
175      * Always throws {@link UnsupportedOperationException}.
176      *
177      * @param key Ignored.
178      * @throws UnsupportedOperationException Always thrown.
179      */
180     @Override
181     public Collection<V> remove(final Object key) {
182         throw new UnsupportedOperationException();
183     }
184 
185     /**
186      * Always throws {@link UnsupportedOperationException}.
187      *
188      * @param key Ignored.
189      * @param item Ignored.
190      * @throws UnsupportedOperationException Always thrown.
191      */
192     @Override
193     public boolean removeMapping(final Object key, final Object item) {
194         throw new UnsupportedOperationException();
195     }
196 
197     @Override
198     public Collection<V> values() {
199         return UnmodifiableCollection.unmodifiableCollection(decorated().values());
200     }
201 
202 }