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    *      https://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.bidimap;
18  
19  import java.util.Map;
20  import java.util.Set;
21  import java.util.SortedMap;
22  
23  import org.apache.commons.collections4.OrderedMapIterator;
24  import org.apache.commons.collections4.SortedBidiMap;
25  import org.apache.commons.collections4.Unmodifiable;
26  import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator;
27  import org.apache.commons.collections4.map.UnmodifiableEntrySet;
28  import org.apache.commons.collections4.map.UnmodifiableSortedMap;
29  import org.apache.commons.collections4.set.UnmodifiableSet;
30  
31  /**
32   * Decorates another {@link SortedBidiMap} to ensure it can't be altered.
33   * <p>
34   * Attempts to modify it will result in an {@link UnsupportedOperationException}.
35   * </p>
36   *
37   * @param <K> The type of the keys in this map
38   * @param <V> The type of the values in this map
39   * @since 3.0
40   */
41  public final class UnmodifiableSortedBidiMap<K, V>
42          extends AbstractSortedBidiMapDecorator<K, V> implements Unmodifiable {
43  
44      /**
45       * Factory method to create an unmodifiable map.
46       * <p>
47       * If the map passed in is already unmodifiable, it is returned.
48       *
49       * @param <K> The key type
50       * @param <V> The value type
51       * @param map  The map to decorate, must not be null
52       * @return An unmodifiable SortedBidiMap
53       * @throws NullPointerException if map is null
54       * @since 4.0
55       */
56      public static <K, V> SortedBidiMap<K, V> unmodifiableSortedBidiMap(final SortedBidiMap<K, ? extends V> map) {
57          if (map instanceof Unmodifiable) {
58              @SuppressWarnings("unchecked") // safe to upcast
59              final SortedBidiMap<K, V> tmpMap = (SortedBidiMap<K, V>) map;
60              return tmpMap;
61          }
62          return new UnmodifiableSortedBidiMap<>(map);
63      }
64  
65      /** The inverse unmodifiable map */
66      private UnmodifiableSortedBidiMap<V, K> inverse;
67  
68      /**
69       * Constructor that wraps (not copies).
70       *
71       * @param map  The map to decorate, must not be null
72       * @throws NullPointerException if map is null
73       */
74      @SuppressWarnings("unchecked") // safe to upcast
75      private UnmodifiableSortedBidiMap(final SortedBidiMap<K, ? extends V> map) {
76          super((SortedBidiMap<K, V>) map);
77      }
78  
79      /**
80       * Always throws {@link UnsupportedOperationException}.
81       *
82       * @throws UnsupportedOperationException Always thrown.
83       */
84      @Override
85      public void clear() {
86          throw new UnsupportedOperationException();
87      }
88  
89      @Override
90      public Set<Map.Entry<K, V>> entrySet() {
91          final Set<Map.Entry<K, V>> set = super.entrySet();
92          return UnmodifiableEntrySet.unmodifiableEntrySet(set);
93      }
94  
95      @Override
96      public SortedMap<K, V> headMap(final K toKey) {
97          final SortedMap<K, V> sm = decorated().headMap(toKey);
98          return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
99      }
100 
101     @Override
102     public SortedBidiMap<V, K> inverseBidiMap() {
103         if (inverse == null) {
104             inverse = new UnmodifiableSortedBidiMap<>(decorated().inverseBidiMap());
105             inverse.inverse = this;
106         }
107         return inverse;
108     }
109 
110     @Override
111     public Set<K> keySet() {
112         final Set<K> set = super.keySet();
113         return UnmodifiableSet.unmodifiableSet(set);
114     }
115 
116     @Override
117     public OrderedMapIterator<K, V> mapIterator() {
118         final OrderedMapIterator<K, V> it = decorated().mapIterator();
119         return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
120     }
121 
122     /**
123      * Always throws {@link UnsupportedOperationException}.
124      *
125      * @param key Ignored.
126      * @param value Ignored.
127      * @throws UnsupportedOperationException Always thrown.
128      */
129     @Override
130     public V put(final K key, final V value) {
131         throw new UnsupportedOperationException();
132     }
133 
134     /**
135      * Always throws {@link UnsupportedOperationException}.
136      *
137      * @param mapToCopy Ignored.
138      * @throws UnsupportedOperationException Always thrown.
139      */
140     @Override
141     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
142         throw new UnsupportedOperationException();
143     }
144 
145     /**
146      * Always throws {@link UnsupportedOperationException}.
147      *
148      * @param key Ignored.
149      * @throws UnsupportedOperationException Always thrown.
150      */
151     @Override
152     public V remove(final Object key) {
153         throw new UnsupportedOperationException();
154     }
155 
156     /**
157      * Always throws {@link UnsupportedOperationException}.
158      *
159      * @param value Ignored.
160      * @throws UnsupportedOperationException Always thrown.
161      */
162     @Override
163     public K removeValue(final Object value) {
164         throw new UnsupportedOperationException();
165     }
166 
167     @Override
168     public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
169         final SortedMap<K, V> sm = decorated().subMap(fromKey, toKey);
170         return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
171     }
172 
173     @Override
174     public SortedMap<K, V> tailMap(final K fromKey) {
175         final SortedMap<K, V> sm = decorated().tailMap(fromKey);
176         return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
177     }
178 
179     @Override
180     public Set<V> values() {
181         final Set<V> set = super.values();
182         return UnmodifiableSet.unmodifiableSet(set);
183     }
184 
185 }