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 * https://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.BidiMap; 023import org.apache.commons.collections4.MapIterator; 024import org.apache.commons.collections4.Unmodifiable; 025import org.apache.commons.collections4.iterators.UnmodifiableMapIterator; 026import org.apache.commons.collections4.map.UnmodifiableEntrySet; 027import org.apache.commons.collections4.set.UnmodifiableSet; 028 029/** 030 * Decorates another {@link BidiMap} to ensure it can't be altered. 031 * <p> 032 * Attempts to modify it will result in an UnsupportedOperationException. 033 * </p> 034 * 035 * @param <K> The type of the keys in this map 036 * @param <V> The type of the values in this map 037 * @since 3.0 038 */ 039public final class UnmodifiableBidiMap<K, V> 040 extends AbstractBidiMapDecorator<K, V> implements Unmodifiable { 041 042 /** 043 * Factory method to create an unmodifiable map. 044 * <p> 045 * If the map passed in is already unmodifiable, it is returned. 046 * 047 * @param <K> The key type 048 * @param <V> The value type 049 * @param map The map to decorate, must not be null 050 * @return An unmodifiable BidiMap 051 * @throws NullPointerException if map is null 052 * @since 4.0 053 */ 054 public static <K, V> BidiMap<K, V> unmodifiableBidiMap(final BidiMap<? extends K, ? extends V> map) { 055 if (map instanceof Unmodifiable) { 056 @SuppressWarnings("unchecked") // safe to upcast 057 final BidiMap<K, V> tmpMap = (BidiMap<K, V>) map; 058 return tmpMap; 059 } 060 return new UnmodifiableBidiMap<>(map); 061 } 062 063 /** The inverse unmodifiable map */ 064 private UnmodifiableBidiMap<V, K> inverse; 065 066 /** 067 * Constructor that wraps (not copies). 068 * 069 * @param map The map to decorate, must not be null 070 * @throws NullPointerException if map is null 071 */ 072 @SuppressWarnings("unchecked") // safe to upcast 073 private UnmodifiableBidiMap(final BidiMap<? extends K, ? extends V> map) { 074 super((BidiMap<K, V>) map); 075 } 076 077 /** 078 * Always throws {@link UnsupportedOperationException}. 079 * 080 * @throws UnsupportedOperationException Always thrown. 081 */ 082 @Override 083 public void clear() { 084 throw new UnsupportedOperationException(); 085 } 086 087 @Override 088 public Set<Map.Entry<K, V>> entrySet() { 089 final Set<Map.Entry<K, V>> set = super.entrySet(); 090 return UnmodifiableEntrySet.unmodifiableEntrySet(set); 091 } 092 093 @Override 094 public synchronized BidiMap<V, K> inverseBidiMap() { 095 if (inverse == null) { 096 inverse = new UnmodifiableBidiMap<>(decorated().inverseBidiMap()); 097 inverse.inverse = this; 098 } 099 return inverse; 100 } 101 102 @Override 103 public Set<K> keySet() { 104 final Set<K> set = super.keySet(); 105 return UnmodifiableSet.unmodifiableSet(set); 106 } 107 108 @Override 109 public MapIterator<K, V> mapIterator() { 110 final MapIterator<K, V> it = decorated().mapIterator(); 111 return UnmodifiableMapIterator.unmodifiableMapIterator(it); 112 } 113 114 /** 115 * Always throws {@link UnsupportedOperationException}. 116 * 117 * @param key Ignored. 118 * @param value Ignored. 119 * @throws UnsupportedOperationException Always thrown. 120 */ 121 @Override 122 public V put(final K key, final V value) { 123 throw new UnsupportedOperationException(); 124 } 125 126 /** 127 * Always throws {@link UnsupportedOperationException}. 128 * 129 * @param mapToCopy Ignored. 130 * @throws UnsupportedOperationException Always thrown. 131 */ 132 @Override 133 public void putAll(final Map<? extends K, ? extends V> mapToCopy) { 134 throw new UnsupportedOperationException(); 135 } 136 137 /** 138 * Always throws {@link UnsupportedOperationException}. 139 * 140 * @param key Ignored. 141 * @throws UnsupportedOperationException Always thrown. 142 */ 143 @Override 144 public V remove(final Object key) { 145 throw new UnsupportedOperationException(); 146 } 147 148 /** 149 * Always throws {@link UnsupportedOperationException}. 150 * 151 * @param value Ignored. 152 * @throws UnsupportedOperationException Always thrown. 153 */ 154 @Override 155 public K removeValue(final Object value) { 156 throw new UnsupportedOperationException(); 157 } 158 159 @Override 160 public Set<V> values() { 161 return UnmodifiableSet.unmodifiableSet(super.values()); 162 } 163 164}