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.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   *
43   * @since 4.1
44   */
45  public final class UnmodifiableMultiValuedMap<K, V>
46          extends AbstractMultiValuedMapDecorator<K, V> implements Unmodifiable {
47  
48      /** Serialization version */
49      private static final long serialVersionUID = 20150612L;
50  
51      /**
52       * Factory method to create an unmodifiable MultiValuedMap.
53       * <p>
54       * If the map passed in is already unmodifiable, it is returned.
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      @Override
88      public void clear() {
89          throw new UnsupportedOperationException();
90      }
91  
92      @Override
93      public Collection<Entry<K, V>> entries() {
94          return UnmodifiableCollection.unmodifiableCollection(decorated().entries());
95      }
96  
97      @Override
98      public Collection<V> get(final K key) {
99          return UnmodifiableCollection.unmodifiableCollection(decorated().get(key));
100     }
101 
102     @Override
103     public MultiSet<K> keys() {
104         return UnmodifiableMultiSet.unmodifiableMultiSet(decorated().keys());
105     }
106 
107     @Override
108     public Set<K> keySet() {
109         return UnmodifiableSet.unmodifiableSet(decorated().keySet());
110     }
111 
112     @Override
113     public MapIterator<K, V> mapIterator() {
114         return UnmodifiableMapIterator.unmodifiableMapIterator(decorated().mapIterator());
115     }
116 
117     @Override
118     public boolean put(final K key, final V value) {
119         throw new UnsupportedOperationException();
120     }
121 
122     @Override
123     public boolean putAll(final K key, final Iterable<? extends V> values) {
124         throw new UnsupportedOperationException();
125     }
126 
127     @Override
128     public boolean putAll(final Map<? extends K, ? extends V> map) {
129         throw new UnsupportedOperationException();
130     }
131 
132     @Override
133     public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
134         throw new UnsupportedOperationException();
135     }
136 
137     @Override
138     public Collection<V> remove(final Object key) {
139         throw new UnsupportedOperationException();
140     }
141 
142     @Override
143     public boolean removeMapping(final Object key, final Object item) {
144         throw new UnsupportedOperationException();
145     }
146 
147     @Override
148     public Collection<V> values() {
149         return UnmodifiableCollection.unmodifiableCollection(decorated().values());
150     }
151 
152 }