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; 021import java.util.SortedMap; 022 023import org.apache.commons.collections4.set.UnmodifiableSet; 024import org.apache.commons.collections4.OrderedMapIterator; 025import org.apache.commons.collections4.SortedBidiMap; 026import org.apache.commons.collections4.Unmodifiable; 027import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator; 028import org.apache.commons.collections4.map.UnmodifiableEntrySet; 029import org.apache.commons.collections4.map.UnmodifiableSortedMap; 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 * 036 * @since 3.0 037 * @version $Id: UnmodifiableSortedBidiMap.html 972421 2015-11-14 20:00:04Z tn $ 038 */ 039public final class UnmodifiableSortedBidiMap<K, V> 040 extends AbstractSortedBidiMapDecorator<K, V> implements Unmodifiable { 041 042 /** The inverse unmodifiable map */ 043 private UnmodifiableSortedBidiMap<V, K> inverse; 044 045 /** 046 * Factory method to create an unmodifiable map. 047 * <p> 048 * If the map passed in is already unmodifiable, it is returned. 049 * 050 * @param <K> the key type 051 * @param <V> the value type 052 * @param map the map to decorate, must not be null 053 * @return an unmodifiable SortedBidiMap 054 * @throws IllegalArgumentException if map is null 055 * @since 4.0 056 */ 057 public static <K, V> SortedBidiMap<K, V> unmodifiableSortedBidiMap(final SortedBidiMap<K, ? extends V> map) { 058 if (map instanceof Unmodifiable) { 059 @SuppressWarnings("unchecked") // safe to upcast 060 final SortedBidiMap<K, V> tmpMap = (SortedBidiMap<K, V>) map; 061 return tmpMap; 062 } 063 return new UnmodifiableSortedBidiMap<K, V>(map); 064 } 065 066 //----------------------------------------------------------------------- 067 /** 068 * Constructor that wraps (not copies). 069 * 070 * @param map the map to decorate, must not be null 071 * @throws IllegalArgumentException if map is null 072 */ 073 @SuppressWarnings("unchecked") // safe to upcast 074 private UnmodifiableSortedBidiMap(final SortedBidiMap<K, ? extends V> map) { 075 super((SortedBidiMap<K, V>) map); 076 } 077 078 //----------------------------------------------------------------------- 079 @Override 080 public void clear() { 081 throw new UnsupportedOperationException(); 082 } 083 084 @Override 085 public V put(final K key, final V value) { 086 throw new UnsupportedOperationException(); 087 } 088 089 @Override 090 public void putAll(final Map<? extends K, ? extends V> mapToCopy) { 091 throw new UnsupportedOperationException(); 092 } 093 094 @Override 095 public V remove(final Object key) { 096 throw new UnsupportedOperationException(); 097 } 098 099 @Override 100 public Set<Map.Entry<K, V>> entrySet() { 101 final Set<Map.Entry<K, V>> set = super.entrySet(); 102 return UnmodifiableEntrySet.unmodifiableEntrySet(set); 103 } 104 105 @Override 106 public Set<K> keySet() { 107 final Set<K> set = super.keySet(); 108 return UnmodifiableSet.unmodifiableSet(set); 109 } 110 111 @Override 112 public Set<V> values() { 113 final Set<V> set = super.values(); 114 return UnmodifiableSet.unmodifiableSet(set); 115 } 116 117 //----------------------------------------------------------------------- 118 @Override 119 public K removeValue(final Object value) { 120 throw new UnsupportedOperationException(); 121 } 122 123 //----------------------------------------------------------------------- 124 @Override 125 public OrderedMapIterator<K, V> mapIterator() { 126 final OrderedMapIterator<K, V> it = decorated().mapIterator(); 127 return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it); 128 } 129 130 //----------------------------------------------------------------------- 131 @Override 132 public SortedBidiMap<V, K> inverseBidiMap() { 133 if (inverse == null) { 134 inverse = new UnmodifiableSortedBidiMap<V, K>(decorated().inverseBidiMap()); 135 inverse.inverse = this; 136 } 137 return inverse; 138 } 139 140 @Override 141 public SortedMap<K, V> subMap(final K fromKey, final K toKey) { 142 final SortedMap<K, V> sm = decorated().subMap(fromKey, toKey); 143 return UnmodifiableSortedMap.unmodifiableSortedMap(sm); 144 } 145 146 @Override 147 public SortedMap<K, V> headMap(final K toKey) { 148 final SortedMap<K, V> sm = decorated().headMap(toKey); 149 return UnmodifiableSortedMap.unmodifiableSortedMap(sm); 150 } 151 152 @Override 153 public SortedMap<K, V> tailMap(final K fromKey) { 154 final SortedMap<K, V> sm = decorated().tailMap(fromKey); 155 return UnmodifiableSortedMap.unmodifiableSortedMap(sm); 156 } 157 158}