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.Comparator;
25  import java.util.Map;
26  import java.util.Set;
27  import java.util.SortedMap;
28  
29  import org.apache.commons.collections4.Unmodifiable;
30  import org.apache.commons.collections4.collection.UnmodifiableCollection;
31  import org.apache.commons.collections4.set.UnmodifiableSet;
32  
33  /**
34   * Decorates another <code>SortedMap</code> to ensure it can't be altered.
35   * <p>
36   * This class is Serializable from Commons Collections 3.1.
37   * </p>
38   * <p>
39   * Attempts to modify it will result in an UnsupportedOperationException.
40   * </p>
41   *
42   * @param <K> the type of the keys in this map
43   * @param <V> the type of the values in this map
44   * @since 3.0
45   */
46  public final class UnmodifiableSortedMap<K, V>
47          extends AbstractSortedMapDecorator<K, V>
48          implements Unmodifiable, Serializable {
49  
50      /** Serialization version */
51      private static final long serialVersionUID = 5805344239827376360L;
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 unmodifiable sorted map
60       * @throws NullPointerException if map is null
61       * @since 4.0
62       */
63      public static <K, V> SortedMap<K, V> unmodifiableSortedMap(final SortedMap<K, ? extends V> map) {
64          if (map instanceof Unmodifiable) {
65              @SuppressWarnings("unchecked") // safe to upcast
66              final SortedMap<K, V> tmpMap = (SortedMap<K, V>) map;
67              return tmpMap;
68          }
69          return new UnmodifiableSortedMap<>(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 UnmodifiableSortedMap(final SortedMap<K, ? extends V> map) {
81          super((SortedMap<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")
106     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
107         in.defaultReadObject();
108         map = (Map<K, V>) in.readObject();
109     }
110 
111     //-----------------------------------------------------------------------
112     @Override
113     public void clear() {
114         throw new UnsupportedOperationException();
115     }
116 
117     @Override
118     public V put(final K key, final V value) {
119         throw new UnsupportedOperationException();
120     }
121 
122     @Override
123     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
124         throw new UnsupportedOperationException();
125     }
126 
127     @Override
128     public V remove(final Object key) {
129         throw new UnsupportedOperationException();
130     }
131 
132     @Override
133     public Set<Map.Entry<K, V>> entrySet() {
134         return UnmodifiableEntrySet.unmodifiableEntrySet(super.entrySet());
135     }
136 
137     @Override
138     public Set<K> keySet() {
139         return UnmodifiableSet.unmodifiableSet(super.keySet());
140     }
141 
142     @Override
143     public Collection<V> values() {
144         return UnmodifiableCollection.unmodifiableCollection(super.values());
145     }
146 
147     //-----------------------------------------------------------------------
148     @Override
149     public K firstKey() {
150         return decorated().firstKey();
151     }
152 
153     @Override
154     public K lastKey() {
155         return decorated().lastKey();
156     }
157 
158     @Override
159     public Comparator<? super K> comparator() {
160         return decorated().comparator();
161     }
162 
163     @Override
164     public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
165         return new UnmodifiableSortedMap<>(decorated().subMap(fromKey, toKey));
166     }
167 
168     @Override
169     public SortedMap<K, V> headMap(final K toKey) {
170         return new UnmodifiableSortedMap<>(decorated().headMap(toKey));
171     }
172 
173     @Override
174     public SortedMap<K, V> tailMap(final K fromKey) {
175         return new UnmodifiableSortedMap<>(decorated().tailMap(fromKey));
176     }
177 
178 }