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