UnmodifiableMultiValuedMap.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.util.Collection;
  19. import java.util.Map;
  20. import java.util.Map.Entry;
  21. import java.util.Set;

  22. import org.apache.commons.collections4.MapIterator;
  23. import org.apache.commons.collections4.MultiSet;
  24. import org.apache.commons.collections4.MultiValuedMap;
  25. import org.apache.commons.collections4.Unmodifiable;
  26. import org.apache.commons.collections4.collection.UnmodifiableCollection;
  27. import org.apache.commons.collections4.iterators.UnmodifiableMapIterator;
  28. import org.apache.commons.collections4.map.UnmodifiableMap;
  29. import org.apache.commons.collections4.multiset.UnmodifiableMultiSet;
  30. import org.apache.commons.collections4.set.UnmodifiableSet;

  31. /**
  32.  * Decorates another {@link MultiValuedMap} to ensure it can't be altered.
  33.  * <p>
  34.  * Attempts to modify it will result in an UnsupportedOperationException.
  35.  * </p>
  36.  *
  37.  * @param <K> the type of key elements
  38.  * @param <V> the type of value elements
  39.  * @since 4.1
  40.  */
  41. public final class UnmodifiableMultiValuedMap<K, V>
  42.         extends AbstractMultiValuedMapDecorator<K, V> implements Unmodifiable {

  43.     /** Serialization version */
  44.     private static final long serialVersionUID = 20150612L;

  45.     /**
  46.      * Factory method to create an unmodifiable MultiValuedMap.
  47.      * <p>
  48.      * If the map passed in is already unmodifiable, it is returned.
  49.      * </p>
  50.      *
  51.      * @param <K> the type of key elements
  52.      * @param <V> the type of value elements
  53.      * @param map  the map to decorate, may not be null
  54.      * @return an unmodifiable MultiValuedMap
  55.      * @throws NullPointerException if map is null
  56.      */
  57.     @SuppressWarnings("unchecked")
  58.     public static <K, V> UnmodifiableMultiValuedMap<K, V> unmodifiableMultiValuedMap(
  59.             final MultiValuedMap<? extends K, ? extends V> map) {
  60.         if (map instanceof Unmodifiable) {
  61.             return (UnmodifiableMultiValuedMap<K, V>) map;
  62.         }
  63.         return new UnmodifiableMultiValuedMap<>(map);
  64.     }

  65.     /**
  66.      * Constructor that wraps (not copies).
  67.      *
  68.      * @param map  the MultiValuedMap to decorate, may not be null
  69.      * @throws NullPointerException if the map is null
  70.      */
  71.     @SuppressWarnings("unchecked")
  72.     private UnmodifiableMultiValuedMap(final MultiValuedMap<? extends K, ? extends V> map) {
  73.         super((MultiValuedMap<K, V>) map);
  74.     }

  75.     @Override
  76.     public Map<K, Collection<V>> asMap() {
  77.         return UnmodifiableMap.unmodifiableMap(decorated().asMap());
  78.     }

  79.     @Override
  80.     public void clear() {
  81.         throw new UnsupportedOperationException();
  82.     }

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

  87.     @Override
  88.     public Collection<V> get(final K key) {
  89.         return UnmodifiableCollection.unmodifiableCollection(decorated().get(key));
  90.     }

  91.     @Override
  92.     public MultiSet<K> keys() {
  93.         return UnmodifiableMultiSet.unmodifiableMultiSet(decorated().keys());
  94.     }

  95.     @Override
  96.     public Set<K> keySet() {
  97.         return UnmodifiableSet.unmodifiableSet(decorated().keySet());
  98.     }

  99.     @Override
  100.     public MapIterator<K, V> mapIterator() {
  101.         return UnmodifiableMapIterator.unmodifiableMapIterator(decorated().mapIterator());
  102.     }

  103.     @Override
  104.     public boolean put(final K key, final V value) {
  105.         throw new UnsupportedOperationException();
  106.     }

  107.     @Override
  108.     public boolean putAll(final K key, final Iterable<? extends V> values) {
  109.         throw new UnsupportedOperationException();
  110.     }

  111.     @Override
  112.     public boolean putAll(final Map<? extends K, ? extends V> map) {
  113.         throw new UnsupportedOperationException();
  114.     }

  115.     @Override
  116.     public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
  117.         throw new UnsupportedOperationException();
  118.     }

  119.     @Override
  120.     public Collection<V> remove(final Object key) {
  121.         throw new UnsupportedOperationException();
  122.     }

  123.     @Override
  124.     public boolean removeMapping(final Object key, final Object item) {
  125.         throw new UnsupportedOperationException();
  126.     }

  127.     @Override
  128.     public Collection<V> values() {
  129.         return UnmodifiableCollection.unmodifiableCollection(decorated().values());
  130.     }

  131. }