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} 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       * 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 UnmodifiableSortedMap(final SortedMap<K, ? extends V> map) {
80          super((SortedMap<K, V>) map);
81      }
82  
83      @Override
84      public void clear() {
85          throw new UnsupportedOperationException();
86      }
87  
88      @Override
89      public Comparator<? super K> comparator() {
90          return decorated().comparator();
91      }
92  
93      @Override
94      public Set<Map.Entry<K, V>> entrySet() {
95          return UnmodifiableEntrySet.unmodifiableEntrySet(super.entrySet());
96      }
97  
98      @Override
99      public K firstKey() {
100         return decorated().firstKey();
101     }
102 
103     @Override
104     public SortedMap<K, V> headMap(final K toKey) {
105         return new UnmodifiableSortedMap<>(decorated().headMap(toKey));
106     }
107 
108     @Override
109     public Set<K> keySet() {
110         return UnmodifiableSet.unmodifiableSet(super.keySet());
111     }
112 
113     @Override
114     public K lastKey() {
115         return decorated().lastKey();
116     }
117 
118     @Override
119     public V put(final K key, final V value) {
120         throw new UnsupportedOperationException();
121     }
122 
123     @Override
124     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
125         throw new UnsupportedOperationException();
126     }
127 
128     /**
129      * Read the map in using a custom routine.
130      *
131      * @param in  the input stream
132      * @throws IOException if an error occurs while reading from the stream
133      * @throws ClassNotFoundException if an object read from the stream can not be loaded
134      * @since 3.1
135      */
136     @SuppressWarnings("unchecked")
137     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
138         in.defaultReadObject();
139         map = (Map<K, V>) in.readObject();
140     }
141 
142     @Override
143     public V remove(final Object key) {
144         throw new UnsupportedOperationException();
145     }
146 
147     @Override
148     public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
149         return new UnmodifiableSortedMap<>(decorated().subMap(fromKey, toKey));
150     }
151 
152     @Override
153     public SortedMap<K, V> tailMap(final K fromKey) {
154         return new UnmodifiableSortedMap<>(decorated().tailMap(fromKey));
155     }
156 
157     @Override
158     public Collection<V> values() {
159         return UnmodifiableCollection.unmodifiableCollection(super.values());
160     }
161 
162     /**
163      * Write the map out using a custom routine.
164      *
165      * @param out  the output stream
166      * @throws IOException if an error occurs while writing to the stream
167      * @since 3.1
168      */
169     private void writeObject(final ObjectOutputStream out) throws IOException {
170         out.defaultWriteObject();
171         out.writeObject(map);
172     }
173 
174 }