UnmodifiableBidiMap.java

  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. import java.util.Map;
  19. import java.util.Set;

  20. import org.apache.commons.collections4.BidiMap;
  21. import org.apache.commons.collections4.MapIterator;
  22. import org.apache.commons.collections4.Unmodifiable;
  23. import org.apache.commons.collections4.iterators.UnmodifiableMapIterator;
  24. import org.apache.commons.collections4.map.UnmodifiableEntrySet;
  25. import org.apache.commons.collections4.set.UnmodifiableSet;

  26. /**
  27.  * Decorates another {@link BidiMap} to ensure it can't be altered.
  28.  * <p>
  29.  * Attempts to modify it will result in an UnsupportedOperationException.
  30.  * </p>
  31.  *
  32.  * @param <K> the type of the keys in this map
  33.  * @param <V> the type of the values in this map
  34.  * @since 3.0
  35.  */
  36. public final class UnmodifiableBidiMap<K, V>
  37.         extends AbstractBidiMapDecorator<K, V> implements Unmodifiable {

  38.     /**
  39.      * Factory method to create an unmodifiable map.
  40.      * <p>
  41.      * If the map passed in is already unmodifiable, it is returned.
  42.      *
  43.      * @param <K> the key type
  44.      * @param <V> the value type
  45.      * @param map  the map to decorate, must not be null
  46.      * @return an unmodifiable BidiMap
  47.      * @throws NullPointerException if map is null
  48.      * @since 4.0
  49.      */
  50.     public static <K, V> BidiMap<K, V> unmodifiableBidiMap(final BidiMap<? extends K, ? extends V> map) {
  51.         if (map instanceof Unmodifiable) {
  52.             @SuppressWarnings("unchecked") // safe to upcast
  53.             final BidiMap<K, V> tmpMap = (BidiMap<K, V>) map;
  54.             return tmpMap;
  55.         }
  56.         return new UnmodifiableBidiMap<>(map);
  57.     }

  58.     /** The inverse unmodifiable map */
  59.     private UnmodifiableBidiMap<V, K> inverse;

  60.     /**
  61.      * Constructor that wraps (not copies).
  62.      *
  63.      * @param map  the map to decorate, must not be null
  64.      * @throws NullPointerException if map is null
  65.      */
  66.     @SuppressWarnings("unchecked") // safe to upcast
  67.     private UnmodifiableBidiMap(final BidiMap<? extends K, ? extends V> map) {
  68.         super((BidiMap<K, V>) map);
  69.     }

  70.     @Override
  71.     public void clear() {
  72.         throw new UnsupportedOperationException();
  73.     }

  74.     @Override
  75.     public Set<Map.Entry<K, V>> entrySet() {
  76.         final Set<Map.Entry<K, V>> set = super.entrySet();
  77.         return UnmodifiableEntrySet.unmodifiableEntrySet(set);
  78.     }

  79.     @Override
  80.     public synchronized BidiMap<V, K> inverseBidiMap() {
  81.         if (inverse == null) {
  82.             inverse = new UnmodifiableBidiMap<>(decorated().inverseBidiMap());
  83.             inverse.inverse = this;
  84.         }
  85.         return inverse;
  86.     }

  87.     @Override
  88.     public Set<K> keySet() {
  89.         final Set<K> set = super.keySet();
  90.         return UnmodifiableSet.unmodifiableSet(set);
  91.     }

  92.     @Override
  93.     public MapIterator<K, V> mapIterator() {
  94.         final MapIterator<K, V> it = decorated().mapIterator();
  95.         return UnmodifiableMapIterator.unmodifiableMapIterator(it);
  96.     }

  97.     @Override
  98.     public V put(final K key, final V value) {
  99.         throw new UnsupportedOperationException();
  100.     }

  101.     @Override
  102.     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
  103.         throw new UnsupportedOperationException();
  104.     }

  105.     @Override
  106.     public V remove(final Object key) {
  107.         throw new UnsupportedOperationException();
  108.     }

  109.     @Override
  110.     public K removeValue(final Object value) {
  111.         throw new UnsupportedOperationException();
  112.     }

  113.     @Override
  114.     public Set<V> values() {
  115.         final Set<V> set = super.values();
  116.         return UnmodifiableSet.unmodifiableSet(set);
  117.     }

  118. }