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