View Javadoc
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  
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.io.Serializable;
23  import java.util.Collection;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.apache.commons.collections4.OrderedMap;
28  import org.apache.commons.collections4.OrderedMapIterator;
29  import org.apache.commons.collections4.Unmodifiable;
30  import org.apache.commons.collections4.collection.UnmodifiableCollection;
31  import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator;
32  import org.apache.commons.collections4.set.UnmodifiableSet;
33  
34  /**
35   * Decorates another {@code OrderedMap} to ensure it can't be altered.
36   * <p>
37   * This class is Serializable from Commons Collections 3.1.
38   * </p>
39   * <p>
40   * Attempts to modify it will result in an UnsupportedOperationException.
41   * </p>
42   *
43   * @param <K> the type of the keys in this map
44   * @param <V> the type of the values in this map
45   * @since 3.0
46   */
47  public final class UnmodifiableOrderedMap<K, V> extends AbstractOrderedMapDecorator<K, V> implements
48          Unmodifiable, Serializable {
49  
50      /** Serialization version */
51      private static final long serialVersionUID = 8136428161720526266L;
52  
53      /**
54       * Factory method to create an unmodifiable sorted map.
55       *
56       * @param <K>  the key type
57       * @param <V>  the value type
58       * @param map  the map to decorate, must not be null
59       * @return a new ordered map
60       * @throws NullPointerException if map is null
61       * @since 4.0
62       */
63      public static <K, V> OrderedMap<K, V> unmodifiableOrderedMap(final OrderedMap<? extends K, ? extends V> map) {
64          if (map instanceof Unmodifiable) {
65              @SuppressWarnings("unchecked") // safe to upcast
66              final OrderedMap<K, V> tmpMap = (OrderedMap<K, V>) map;
67              return tmpMap;
68          }
69          return new UnmodifiableOrderedMap<>(map);
70      }
71  
72      /**
73       * Constructor that wraps (not copies).
74       *
75       * @param map  the map to decorate, must not be null
76       * @throws NullPointerException if map is null
77       */
78      @SuppressWarnings("unchecked") // safe to upcast
79      private UnmodifiableOrderedMap(final OrderedMap<? extends K, ? extends V> map) {
80          super((OrderedMap<K, V>) map);
81      }
82  
83      @Override
84      public void clear() {
85          throw new UnsupportedOperationException();
86      }
87  
88      @Override
89      public Set<Map.Entry<K, V>> entrySet() {
90          final Set<Map.Entry<K, V>> set = super.entrySet();
91          return UnmodifiableEntrySet.unmodifiableEntrySet(set);
92      }
93  
94      @Override
95      public Set<K> keySet() {
96          final Set<K> set = super.keySet();
97          return UnmodifiableSet.unmodifiableSet(set);
98      }
99  
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 }