001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.bidimap; 018 019import java.util.Map; 020import java.util.Set; 021 022import org.apache.commons.collections4.set.UnmodifiableSet; 023import org.apache.commons.collections4.OrderedBidiMap; 024import org.apache.commons.collections4.OrderedMapIterator; 025import org.apache.commons.collections4.Unmodifiable; 026import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator; 027import org.apache.commons.collections4.map.UnmodifiableEntrySet; 028 029/** 030 * Decorates another {@link OrderedBidiMap} to ensure it can't be altered. 031 * <p> 032 * Attempts to modify it will result in an UnsupportedOperationException. 033 * 034 * @since 3.0 035 * @version $Id: UnmodifiableOrderedBidiMap.html 972421 2015-11-14 20:00:04Z tn $ 036 */ 037public final class UnmodifiableOrderedBidiMap<K, V> 038 extends AbstractOrderedBidiMapDecorator<K, V> implements Unmodifiable { 039 040 /** The inverse unmodifiable map */ 041 private UnmodifiableOrderedBidiMap<V, K> inverse; 042 043 /** 044 * Factory method to create an unmodifiable map. 045 * <p> 046 * If the map passed in is already unmodifiable, it is returned. 047 * 048 * @param <K> the key type 049 * @param <V> the value type 050 * @param map the map to decorate, must not be null 051 * @return an unmodifiable OrderedBidiMap 052 * @throws IllegalArgumentException if map is null 053 * @since 4.0 054 */ 055 public static <K, V> OrderedBidiMap<K, V> unmodifiableOrderedBidiMap( 056 final OrderedBidiMap<? extends K, ? extends V> map) { 057 if (map instanceof Unmodifiable) { 058 @SuppressWarnings("unchecked") // safe to upcast 059 final OrderedBidiMap<K, V> tmpMap = (OrderedBidiMap<K, V>) map; 060 return tmpMap; 061 } 062 return new UnmodifiableOrderedBidiMap<K, V>(map); 063 } 064 065 //----------------------------------------------------------------------- 066 /** 067 * Constructor that wraps (not copies). 068 * 069 * @param map the map to decorate, must not be null 070 * @throws IllegalArgumentException if map is null 071 */ 072 @SuppressWarnings("unchecked") // safe to upcast 073 private UnmodifiableOrderedBidiMap(final OrderedBidiMap<? extends K, ? extends V> map) { 074 super((OrderedBidiMap<K, V>) map); 075 } 076 077 //----------------------------------------------------------------------- 078 @Override 079 public void clear() { 080 throw new UnsupportedOperationException(); 081 } 082 083 @Override 084 public V put(final K key, final V value) { 085 throw new UnsupportedOperationException(); 086 } 087 088 @Override 089 public void putAll(final Map<? extends K, ? extends V> mapToCopy) { 090 throw new UnsupportedOperationException(); 091 } 092 093 @Override 094 public V remove(final Object key) { 095 throw new UnsupportedOperationException(); 096 } 097 098 @Override 099 public Set<Map.Entry<K, V>> entrySet() { 100 final Set<Map.Entry<K, V>> set = super.entrySet(); 101 return UnmodifiableEntrySet.unmodifiableEntrySet(set); 102 } 103 104 @Override 105 public Set<K> keySet() { 106 final Set<K> set = super.keySet(); 107 return UnmodifiableSet.unmodifiableSet(set); 108 } 109 110 @Override 111 public Set<V> values() { 112 final Set<V> set = super.values(); 113 return UnmodifiableSet.unmodifiableSet(set); 114 } 115 116 //----------------------------------------------------------------------- 117 @Override 118 public K removeValue(final Object value) { 119 throw new UnsupportedOperationException(); 120 } 121 122 @Override 123 public OrderedBidiMap<V, K> inverseBidiMap() { 124 return inverseOrderedBidiMap(); 125 } 126 127 //----------------------------------------------------------------------- 128 @Override 129 public OrderedMapIterator<K, V> mapIterator() { 130 final OrderedMapIterator<K, V> it = decorated().mapIterator(); 131 return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it); 132 } 133 134 /** 135 * Gets an unmodifiable view of this map where the keys and values are reversed. 136 * 137 * @return an inverted unmodifiable bidirectional map 138 */ 139 public OrderedBidiMap<V, K> inverseOrderedBidiMap() { 140 if (inverse == null) { 141 inverse = new UnmodifiableOrderedBidiMap<V, K>(decorated().inverseBidiMap()); 142 inverse.inverse = this; 143 } 144 return inverse; 145 } 146 147}