UnmodifiableMap.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.map;

  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;
  22. import java.util.Collection;
  23. import java.util.Map;
  24. import java.util.Set;

  25. import org.apache.commons.collections4.IterableMap;
  26. import org.apache.commons.collections4.MapIterator;
  27. import org.apache.commons.collections4.Unmodifiable;
  28. import org.apache.commons.collections4.collection.UnmodifiableCollection;
  29. import org.apache.commons.collections4.iterators.EntrySetMapIterator;
  30. import org.apache.commons.collections4.iterators.UnmodifiableMapIterator;
  31. import org.apache.commons.collections4.set.UnmodifiableSet;

  32. /**
  33.  * Decorates another {@code Map} to ensure it can't be altered.
  34.  * <p>
  35.  * This class is Serializable from Commons Collections 3.1.
  36.  * </p>
  37.  * <p>
  38.  * Attempts to modify it will result in an UnsupportedOperationException.
  39.  * </p>
  40.  *
  41.  * @param <K> the type of the keys in this map
  42.  * @param <V> the type of the values in this map
  43.  * @since 3.0
  44.  */
  45. public final class UnmodifiableMap<K, V>
  46.         extends AbstractMapDecorator<K, V>
  47.         implements Unmodifiable, Serializable {

  48.     /** Serialization version */
  49.     private static final long serialVersionUID = 2737023427269031941L;

  50.     /**
  51.      * Factory method to create an unmodifiable map.
  52.      *
  53.      * @param <K>  the key type
  54.      * @param <V>  the value type
  55.      * @param map  the map to decorate, must not be null
  56.      * @return a new unmodifiable map
  57.      * @throws NullPointerException if map is null
  58.      * @since 4.0
  59.      */
  60.     public static <K, V> Map<K, V> unmodifiableMap(final Map<? extends K, ? extends V> map) {
  61.         if (map instanceof Unmodifiable) {
  62.             @SuppressWarnings("unchecked") // safe to upcast
  63.             final Map<K, V> tmpMap = (Map<K, V>) map;
  64.             return tmpMap;
  65.         }
  66.         return new UnmodifiableMap<>(map);
  67.     }

  68.     /**
  69.      * Constructor that wraps (not copies).
  70.      *
  71.      * @param map  the map to decorate, must not be null
  72.      * @throws NullPointerException if map is null
  73.      */
  74.     @SuppressWarnings("unchecked") // safe to upcast
  75.     private UnmodifiableMap(final Map<? extends K, ? extends V> map) {
  76.         super((Map<K, V>) map);
  77.     }

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

  82.     @Override
  83.     public Set<Map.Entry<K, V>> entrySet() {
  84.         final Set<Map.Entry<K, V>> set = super.entrySet();
  85.         return UnmodifiableEntrySet.unmodifiableEntrySet(set);
  86.     }

  87.     @Override
  88.     public Set<K> keySet() {
  89.         final Set<K> set = super.keySet();
  90.         return UnmodifiableSet.unmodifiableSet(set);
  91.     }

  92.     @Override
  93.     public MapIterator<K, V> mapIterator() {
  94.         if (map instanceof IterableMap) {
  95.             final MapIterator<K, V> it = ((IterableMap<K, V>) map).mapIterator();
  96.             return UnmodifiableMapIterator.unmodifiableMapIterator(it);
  97.         }
  98.         final MapIterator<K, V> it = new EntrySetMapIterator<>(map);
  99.         return UnmodifiableMapIterator.unmodifiableMapIterator(it);
  100.     }

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

  105.     @Override
  106.     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
  107.         throw new UnsupportedOperationException();
  108.     }

  109.     /**
  110.      * Deserializes the map in using a custom routine.
  111.      *
  112.      * @param in  the input stream
  113.      * @throws IOException if an error occurs while reading from the stream
  114.      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
  115.      * @since 3.1
  116.      */
  117.     @SuppressWarnings("unchecked")
  118.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  119.         in.defaultReadObject();
  120.         map = (Map<K, V>) in.readObject();
  121.     }

  122.     @Override
  123.     public V remove(final Object key) {
  124.         throw new UnsupportedOperationException();
  125.     }

  126.     @Override
  127.     public Collection<V> values() {
  128.         final Collection<V> coll = super.values();
  129.         return UnmodifiableCollection.unmodifiableCollection(coll);
  130.     }

  131.     /**
  132.      * Serializes this object to an ObjectOutputStream.
  133.      *
  134.      * @param out the target ObjectOutputStream.
  135.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  136.      * @since 3.1
  137.      */
  138.     private void writeObject(final ObjectOutputStream out) throws IOException {
  139.         out.defaultWriteObject();
  140.         out.writeObject(map);
  141.     }

  142. }