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.collections.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.collections.OrderedMap;
28  import org.apache.commons.collections.OrderedMapIterator;
29  import org.apache.commons.collections.Unmodifiable;
30  import org.apache.commons.collections.collection.UnmodifiableCollection;
31  import org.apache.commons.collections.iterators.UnmodifiableOrderedMapIterator;
32  import org.apache.commons.collections.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   * Attempts to modify it will result in an UnsupportedOperationException. 
40   *
41   * @since 3.0
42   * @version $Id: UnmodifiableOrderedMap.java 1429905 2013-01-07 17:15:14Z ggregory $
43   */
44  public final class UnmodifiableOrderedMap<K, V> extends AbstractOrderedMapDecorator<K, V> implements
45          Unmodifiable, Serializable {
46  
47      /** Serialization version */
48      private static final long serialVersionUID = 8136428161720526266L;
49  
50      /**
51       * Factory method to create an unmodifiable sorted map.
52       * 
53       * @param <K>  the key type
54       * @param <V>  the value type
55       * @param map  the map to decorate, must not be null
56       * @return a new ordered map
57       * @throws IllegalArgumentException if map is null
58       */
59      public static <K, V> OrderedMap<K, V> unmodifiableOrderedMap(final OrderedMap<K, V> map) {
60          if (map instanceof Unmodifiable) {
61              return map;
62          }
63          return new UnmodifiableOrderedMap<K, V>(map);
64      }
65  
66      //-----------------------------------------------------------------------
67      /**
68       * Constructor that wraps (not copies).
69       * 
70       * @param map  the map to decorate, must not be null
71       * @throws IllegalArgumentException if map is null
72       */
73      private UnmodifiableOrderedMap(final OrderedMap<K, V> map) {
74          super(map);
75      }
76  
77      //-----------------------------------------------------------------------
78      /**
79       * Write the map out using a custom routine.
80       * 
81       * @param out  the output stream
82       * @throws IOException
83       * @since 3.1
84       */
85      private void writeObject(final ObjectOutputStream out) throws IOException {
86          out.defaultWriteObject();
87          out.writeObject(map);
88      }
89  
90      /**
91       * Read the map in using a custom routine.
92       * 
93       * @param in  the input stream
94       * @throws IOException
95       * @throws ClassNotFoundException
96       * @since 3.1
97       */
98      @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect 
99      private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
100         in.defaultReadObject();
101         map = (Map<K, V>) in.readObject(); // (1)
102     }
103 
104     //-----------------------------------------------------------------------
105     @Override
106     public OrderedMapIterator<K, V> mapIterator() {
107         final OrderedMapIterator<K, V> it = decorated().mapIterator();
108         return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
109     }
110 
111     @Override
112     public void clear() {
113         throw new UnsupportedOperationException();
114     }
115 
116     @Override
117     public V put(final K key, final V value) {
118         throw new UnsupportedOperationException();
119     }
120 
121     @Override
122     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
123         throw new UnsupportedOperationException();
124     }
125 
126     @Override
127     public V remove(final Object key) {
128         throw new UnsupportedOperationException();
129     }
130 
131     @Override
132     public Set<Map.Entry<K, V>> entrySet() {
133         final Set<Map.Entry<K, V>> set = super.entrySet();
134         return UnmodifiableEntrySet.unmodifiableEntrySet(set);
135     }
136 
137     @Override
138     public Set<K> keySet() {
139         final Set<K> set = super.keySet();
140         return UnmodifiableSet.unmodifiableSet(set);
141     }
142 
143     @Override
144     public Collection<V> values() {
145         final Collection<V> coll = super.values();
146         return UnmodifiableCollection.unmodifiableCollection(coll);
147     }
148 
149 }