    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.collections4.map;
    018
    019import java.io.IOException;
    020import java.io.ObjectInputStream;
    021import java.io.ObjectOutputStream;
    022import java.io.Serializable;
    023import java.util.Collection;
    024import java.util.Map;
    025import java.util.Set;
    026
    027import org.apache.commons.collections4.IterableMap;
    028import org.apache.commons.collections4.MapIterator;
    029import org.apache.commons.collections4.Unmodifiable;
    030import org.apache.commons.collections4.collection.UnmodifiableCollection;
    031import org.apache.commons.collections4.iterators.EntrySetMapIterator;
    032import org.apache.commons.collections4.iterators.UnmodifiableMapIterator;
    033import org.apache.commons.collections4.set.UnmodifiableSet;
    034
    035/**
    036 * Decorates another {@code Map} to ensure it can't be altered.
    037 * <p>
    038 * This class is Serializable from Commons Collections 3.1.
    039 * </p>
    040 * <p>
    041 * Attempts to modify it will result in an UnsupportedOperationException.
    042 * </p>
    043 *
    044 * @param <K> The type of the keys in this map
    045 * @param <V> The type of the values in this map
    046 * @since 3.0
    047 */
    048public final class UnmodifiableMap<K, V>
    049        extends AbstractMapDecorator<K, V>
    050        implements Unmodifiable, Serializable {
    051
    052    /** Serialization version */
    053    private static final long serialVersionUID = 2737023427269031941L;
    054
    055    /**
    056     * Factory method to create an unmodifiable map.
    057     *
    058     * @param <K>  the key type
    059     * @param <V>  the value type
    060     * @param map  The map to decorate, must not be null
    061     * @return A new unmodifiable map
    062     * @throws NullPointerException if map is null
    063     * @since 4.0
    064     */
    065    public static <K, V> Map<K, V> unmodifiableMap(final Map<? extends K, ? extends V> map) {
    066        if (map instanceof Unmodifiable) {
    067            @SuppressWarnings("unchecked") // safe to upcast
    068            final Map<K, V> tmpMap = (Map<K, V>) map;
    069            return tmpMap;
    070        }
    071        return new UnmodifiableMap<>(map);
    072    }
    073
    074    /**
    075     * Constructor that wraps (not copies).
    076     *
    077     * @param map  The map to decorate, must not be null
    078     * @throws NullPointerException if map is null
    079     */
    080    @SuppressWarnings("unchecked") // safe to upcast
    081    private UnmodifiableMap(final Map<? extends K, ? extends V> map) {
    082        super((Map<K, V>) map);
    083    }
    084
    085    /**
    086     * Always throws {@link UnsupportedOperationException}.
    087     *
    088     * @throws UnsupportedOperationException Always thrown.
    089     */
    090    @Override
    091    public void clear() {
    092        throw new UnsupportedOperationException();
    093    }
    094
    095    @Override
    096    public Set<Map.Entry<K, V>> entrySet() {
    097        return UnmodifiableEntrySet.unmodifiableEntrySet(super.entrySet());
    098    }
    099
    100    @Override
    101    public Set<K> keySet() {
    102        final Set<K> set = super.keySet();
    103        return UnmodifiableSet.unmodifiableSet(set);
    104    }
    105
    106    @Override
    107    public MapIterator<K, V> mapIterator() {
    108        if (map instanceof IterableMap) {
    109            final MapIterator<K, V> it = ((IterableMap<K, V>) map).mapIterator();
    110            return UnmodifiableMapIterator.unmodifiableMapIterator(it);
    111        }
    112        final MapIterator<K, V> it = new EntrySetMapIterator<>(map);
    113        return UnmodifiableMapIterator.unmodifiableMapIterator(it);
    114    }
    115
    116    /**
    117     * Always throws {@link UnsupportedOperationException}.
    118     *
    119     * @param key Ignored.
    120     * @param value Ignored.
    121     * @throws UnsupportedOperationException Always thrown.
    122     */
    123    @Override
    124    public V put(final K key, final V value) {
    125        throw new UnsupportedOperationException();
    126    }
    127
    128    /**
    129     * Always throws {@link UnsupportedOperationException}.
    130     *
    131     * @param mapToCopy Ignored.
    132     * @throws UnsupportedOperationException Always thrown.
    133     */
    134    @Override
    135    public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
    136        throw new UnsupportedOperationException();
    137    }
    138
    139    /**
    140     * Deserializes the map in using a custom routine.
    141     *
    142     * @param in  The input stream
    143     * @throws IOException Thrown if an error occurs while reading from the stream
    144     * @throws ClassNotFoundException if an object read from the stream cannot be loaded
    145     * @since 3.1
    146     */
    147    @SuppressWarnings("unchecked")
    148    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    149        in.defaultReadObject();
    150        map = (Map<K, V>) in.readObject();
    151    }
    152
    153    /**
    154     * Always throws {@link UnsupportedOperationException}.
    155     *
    156     * @param key Ignored.
    157     * @throws UnsupportedOperationException Always thrown.
    158     */
    159    @Override
    160    public V remove(final Object key) {
    161        throw new UnsupportedOperationException();
    162    }
    163
    164    @Override
    165    public Collection<V> values() {
    166        return UnmodifiableCollection.unmodifiableCollection(super.values());
    167    }
    168
    169    /**
    170     * Serializes this object to an ObjectOutputStream.
    171     *
    172     * @param out The target ObjectOutputStream.
    173     * @throws IOException thrown when an I/O errors occur writing to the target stream.
    174     * @since 3.1
    175     */
    176    private void writeObject(final ObjectOutputStream out) throws IOException {
    177        out.defaultWriteObject();
    178        out.writeObject(map);
    179    }
    180
    181}