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 *      http://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.OrderedMap;
028import org.apache.commons.collections4.OrderedMapIterator;
029import org.apache.commons.collections4.Unmodifiable;
030import org.apache.commons.collections4.collection.UnmodifiableCollection;
031import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator;
032import org.apache.commons.collections4.set.UnmodifiableSet;
033
034/**
035 * Decorates another {@code OrderedMap} to ensure it can't be altered.
036 * <p>
037 * This class is Serializable from Commons Collections 3.1.
038 * </p>
039 * <p>
040 * Attempts to modify it will result in an UnsupportedOperationException.
041 * </p>
042 *
043 * @param <K> the type of the keys in this map
044 * @param <V> the type of the values in this map
045 * @since 3.0
046 */
047public final class UnmodifiableOrderedMap<K, V> extends AbstractOrderedMapDecorator<K, V> implements
048        Unmodifiable, Serializable {
049
050    /** Serialization version */
051    private static final long serialVersionUID = 8136428161720526266L;
052
053    /**
054     * Factory method to create an unmodifiable sorted map.
055     *
056     * @param <K>  the key type
057     * @param <V>  the value type
058     * @param map  the map to decorate, must not be null
059     * @return a new ordered map
060     * @throws NullPointerException if map is null
061     * @since 4.0
062     */
063    public static <K, V> OrderedMap<K, V> unmodifiableOrderedMap(final OrderedMap<? extends K, ? extends V> map) {
064        if (map instanceof Unmodifiable) {
065            @SuppressWarnings("unchecked") // safe to upcast
066            final OrderedMap<K, V> tmpMap = (OrderedMap<K, V>) map;
067            return tmpMap;
068        }
069        return new UnmodifiableOrderedMap<>(map);
070    }
071
072    /**
073     * Constructor that wraps (not copies).
074     *
075     * @param map  the map to decorate, must not be null
076     * @throws NullPointerException if map is null
077     */
078    @SuppressWarnings("unchecked") // safe to upcast
079    private UnmodifiableOrderedMap(final OrderedMap<? extends K, ? extends V> map) {
080        super((OrderedMap<K, V>) map);
081    }
082
083    @Override
084    public void clear() {
085        throw new UnsupportedOperationException();
086    }
087
088    @Override
089    public Set<Map.Entry<K, V>> entrySet() {
090        final Set<Map.Entry<K, V>> set = super.entrySet();
091        return UnmodifiableEntrySet.unmodifiableEntrySet(set);
092    }
093
094    @Override
095    public Set<K> keySet() {
096        final Set<K> set = super.keySet();
097        return UnmodifiableSet.unmodifiableSet(set);
098    }
099
100    @Override
101    public OrderedMapIterator<K, V> mapIterator() {
102        final OrderedMapIterator<K, V> it = decorated().mapIterator();
103        return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
104    }
105
106    @Override
107    public V put(final K key, final V value) {
108        throw new UnsupportedOperationException();
109    }
110
111    @Override
112    public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
113        throw new UnsupportedOperationException();
114    }
115
116    /**
117     * Read the map in using a custom routine.
118     *
119     * @param in  the input stream
120     * @throws IOException if an error occurs while reading from the stream
121     * @throws ClassNotFoundException if an object read from the stream can not be loaded
122     * @since 3.1
123     */
124    @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect
125    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
126        in.defaultReadObject();
127        map = (Map<K, V>) in.readObject(); // (1)
128    }
129
130    @Override
131    public V remove(final Object key) {
132        throw new UnsupportedOperationException();
133    }
134
135    @Override
136    public Collection<V> values() {
137        final Collection<V> coll = super.values();
138        return UnmodifiableCollection.unmodifiableCollection(coll);
139    }
140
141    /**
142     * Write the map out using a custom routine.
143     *
144     * @param out  the output stream
145     * @throws IOException if an error occurs while writing to the stream
146     * @since 3.1
147     */
148    private void writeObject(final ObjectOutputStream out) throws IOException {
149        out.defaultWriteObject();
150        out.writeObject(map);
151    }
152
153}