UnmodifiableOrderedBidiMap.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.OrderedBidiMap;
  21. import org.apache.commons.collections4.OrderedMapIterator;
  22. import org.apache.commons.collections4.Unmodifiable;
  23. import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator;
  24. import org.apache.commons.collections4.map.UnmodifiableEntrySet;
  25. import org.apache.commons.collections4.set.UnmodifiableSet;

  26. /**
  27.  * Decorates another {@link OrderedBidiMap} 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 UnmodifiableOrderedBidiMap<K, V>
  37.         extends AbstractOrderedBidiMapDecorator<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 OrderedBidiMap
  47.      * @throws NullPointerException if map is null
  48.      * @since 4.0
  49.      */
  50.     public static <K, V> OrderedBidiMap<K, V> unmodifiableOrderedBidiMap(
  51.             final OrderedBidiMap<? extends K, ? extends V> map) {
  52.         if (map instanceof Unmodifiable) {
  53.             @SuppressWarnings("unchecked") // safe to upcast
  54.             final OrderedBidiMap<K, V> tmpMap = (OrderedBidiMap<K, V>) map;
  55.             return tmpMap;
  56.         }
  57.         return new UnmodifiableOrderedBidiMap<>(map);
  58.     }

  59.     /** The inverse unmodifiable map */
  60.     private UnmodifiableOrderedBidiMap<V, K> inverse;

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

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

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

  80.     @Override
  81.     public OrderedBidiMap<V, K> inverseBidiMap() {
  82.         return inverseOrderedBidiMap();
  83.     }

  84.     /**
  85.      * Gets an unmodifiable view of this map where the keys and values are reversed.
  86.      *
  87.      * @return an inverted unmodifiable bidirectional map
  88.      */
  89.     public OrderedBidiMap<V, K> inverseOrderedBidiMap() {
  90.         if (inverse == null) {
  91.             inverse = new UnmodifiableOrderedBidiMap<>(decorated().inverseBidiMap());
  92.             inverse.inverse = this;
  93.         }
  94.         return inverse;
  95.     }

  96.     @Override
  97.     public Set<K> keySet() {
  98.         final Set<K> set = super.keySet();
  99.         return UnmodifiableSet.unmodifiableSet(set);
  100.     }

  101.     @Override
  102.     public OrderedMapIterator<K, V> mapIterator() {
  103.         final OrderedMapIterator<K, V> it = decorated().mapIterator();
  104.         return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
  105.     }

  106.     @Override
  107.     public V put(final K key, final V value) {
  108.         throw new UnsupportedOperationException();
  109.     }

  110.     @Override
  111.     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
  112.         throw new UnsupportedOperationException();
  113.     }

  114.     @Override
  115.     public V remove(final Object key) {
  116.         throw new UnsupportedOperationException();
  117.     }

  118.     @Override
  119.     public K removeValue(final Object value) {
  120.         throw new UnsupportedOperationException();
  121.     }

  122.     @Override
  123.     public Set<V> values() {
  124.         final Set<V> set = super.values();
  125.         return UnmodifiableSet.unmodifiableSet(set);
  126.     }

  127. }