UnmodifiableOrderedMap.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.OrderedMap;
  26. import org.apache.commons.collections4.OrderedMapIterator;
  27. import org.apache.commons.collections4.Unmodifiable;
  28. import org.apache.commons.collections4.collection.UnmodifiableCollection;
  29. import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator;
  30. import org.apache.commons.collections4.set.UnmodifiableSet;

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

  46.     /** Serialization version */
  47.     private static final long serialVersionUID = 8136428161720526266L;

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

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

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

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

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

  90.     @Override
  91.     public OrderedMapIterator<K, V> mapIterator() {
  92.         final OrderedMapIterator<K, V> it = decorated().mapIterator();
  93.         return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
  94.     }

  95.     @Override
  96.     public V put(final K key, final V value) {
  97.         throw new UnsupportedOperationException();
  98.     }

  99.     @Override
  100.     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
  101.         throw new UnsupportedOperationException();
  102.     }

  103.     /**
  104.      * Deseializes the map in using a custom routine.
  105.      *
  106.      * @param in  the input stream
  107.      * @throws IOException if an error occurs while reading from the stream
  108.      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
  109.      * @since 3.1
  110.      */
  111.     @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect
  112.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  113.         in.defaultReadObject();
  114.         map = (Map<K, V>) in.readObject(); // (1)
  115.     }

  116.     @Override
  117.     public V remove(final Object key) {
  118.         throw new UnsupportedOperationException();
  119.     }

  120.     @Override
  121.     public Collection<V> values() {
  122.         final Collection<V> coll = super.values();
  123.         return UnmodifiableCollection.unmodifiableCollection(coll);
  124.     }

  125.     /**
  126.      * Serializes this object to an ObjectOutputStream.
  127.      *
  128.      * @param out the target ObjectOutputStream.
  129.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  130.      * @since 3.1
  131.      */
  132.     private void writeObject(final ObjectOutputStream out) throws IOException {
  133.         out.defaultWriteObject();
  134.         out.writeObject(map);
  135.     }

  136. }