UnmodifiableSortedBidiMap.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 java.util.SortedMap;

  21. import org.apache.commons.collections4.OrderedMapIterator;
  22. import org.apache.commons.collections4.SortedBidiMap;
  23. import org.apache.commons.collections4.Unmodifiable;
  24. import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator;
  25. import org.apache.commons.collections4.map.UnmodifiableEntrySet;
  26. import org.apache.commons.collections4.map.UnmodifiableSortedMap;
  27. import org.apache.commons.collections4.set.UnmodifiableSet;

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

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

  60.     /** The inverse unmodifiable map */
  61.     private UnmodifiableSortedBidiMap<V, K> inverse;

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

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

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

  81.     @Override
  82.     public SortedMap<K, V> headMap(final K toKey) {
  83.         final SortedMap<K, V> sm = decorated().headMap(toKey);
  84.         return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
  85.     }

  86.     @Override
  87.     public SortedBidiMap<V, K> inverseBidiMap() {
  88.         if (inverse == null) {
  89.             inverse = new UnmodifiableSortedBidiMap<>(decorated().inverseBidiMap());
  90.             inverse.inverse = this;
  91.         }
  92.         return inverse;
  93.     }

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

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

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

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

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

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

  120.     @Override
  121.     public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
  122.         final SortedMap<K, V> sm = decorated().subMap(fromKey, toKey);
  123.         return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
  124.     }

  125.     @Override
  126.     public SortedMap<K, V> tailMap(final K fromKey) {
  127.         final SortedMap<K, V> sm = decorated().tailMap(fromKey);
  128.         return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
  129.     }

  130.     @Override
  131.     public Set<V> values() {
  132.         final Set<V> set = super.values();
  133.         return UnmodifiableSet.unmodifiableSet(set);
  134.     }

  135. }