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