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.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      @Override
80      public void clear() {
81          throw new UnsupportedOperationException();
82      }
83  
84      @Override
85      public Set<Map.Entry<K, V>> entrySet() {
86          final Set<Map.Entry<K, V>> set = super.entrySet();
87          return UnmodifiableEntrySet.unmodifiableEntrySet(set);
88      }
89  
90      @Override
91      public SortedMap<K, V> headMap(final K toKey) {
92          final SortedMap<K, V> sm = decorated().headMap(toKey);
93          return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
94      }
95  
96      @Override
97      public SortedBidiMap<V, K> inverseBidiMap() {
98          if (inverse == null) {
99              inverse = new UnmodifiableSortedBidiMap<>(decorated().inverseBidiMap());
100             inverse.inverse = this;
101         }
102         return inverse;
103     }
104 
105     @Override
106     public Set<K> keySet() {
107         final Set<K> set = super.keySet();
108         return UnmodifiableSet.unmodifiableSet(set);
109     }
110 
111     @Override
112     public OrderedMapIterator<K, V> mapIterator() {
113         final OrderedMapIterator<K, V> it = decorated().mapIterator();
114         return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
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 K removeValue(final Object value) {
134         throw new UnsupportedOperationException();
135     }
136 
137     @Override
138     public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
139         final SortedMap<K, V> sm = decorated().subMap(fromKey, toKey);
140         return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
141     }
142 
143     @Override
144     public SortedMap<K, V> tailMap(final K fromKey) {
145         final SortedMap<K, V> sm = decorated().tailMap(fromKey);
146         return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
147     }
148 
149     @Override
150     public Set<V> values() {
151         final Set<V> set = super.values();
152         return UnmodifiableSet.unmodifiableSet(set);
153     }
154 
155 }