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</code> 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      /**
74       * Constructor that wraps (not copies).
75       *
76       * @param map  the map to decorate, must not be null
77       * @throws NullPointerException if map is null
78       */
79      @SuppressWarnings("unchecked") // safe to upcast
80      private UnmodifiableOrderedMap(final OrderedMap<? extends K, ? extends V> map) {
81          super((OrderedMap<K, V>) map);
82      }
83  
84      //-----------------------------------------------------------------------
85      /**
86       * Write the map out using a custom routine.
87       *
88       * @param out  the output stream
89       * @throws IOException if an error occurs while writing to the stream
90       * @since 3.1
91       */
92      private void writeObject(final ObjectOutputStream out) throws IOException {
93          out.defaultWriteObject();
94          out.writeObject(map);
95      }
96  
97      /**
98       * Read the map in using a custom routine.
99       *
100      * @param in  the input stream
101      * @throws IOException if an error occurs while reading from the stream
102      * @throws ClassNotFoundException if an object read from the stream can not be loaded
103      * @since 3.1
104      */
105     @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect
106     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
107         in.defaultReadObject();
108         map = (Map<K, V>) in.readObject(); // (1)
109     }
110 
111     //-----------------------------------------------------------------------
112     @Override
113     public OrderedMapIterator<K, V> mapIterator() {
114         final OrderedMapIterator<K, V> it = decorated().mapIterator();
115         return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
116     }
117 
118     @Override
119     public void clear() {
120         throw new UnsupportedOperationException();
121     }
122 
123     @Override
124     public V put(final K key, final V value) {
125         throw new UnsupportedOperationException();
126     }
127 
128     @Override
129     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
130         throw new UnsupportedOperationException();
131     }
132 
133     @Override
134     public V remove(final Object key) {
135         throw new UnsupportedOperationException();
136     }
137 
138     @Override
139     public Set<Map.Entry<K, V>> entrySet() {
140         final Set<Map.Entry<K, V>> set = super.entrySet();
141         return UnmodifiableEntrySet.unmodifiableEntrySet(set);
142     }
143 
144     @Override
145     public Set<K> keySet() {
146         final Set<K> set = super.keySet();
147         return UnmodifiableSet.unmodifiableSet(set);
148     }
149 
150     @Override
151     public Collection<V> values() {
152         final Collection<V> coll = super.values();
153         return UnmodifiableCollection.unmodifiableCollection(coll);
154     }
155 
156 }