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.BidiMap;
024import org.apache.commons.collections4.MapIterator;
025import org.apache.commons.collections4.Unmodifiable;
026import org.apache.commons.collections4.iterators.UnmodifiableMapIterator;
027import org.apache.commons.collections4.map.UnmodifiableEntrySet;
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 *
034 * @since 3.0
035 * @version $Id: UnmodifiableBidiMap.html 972421 2015-11-14 20:00:04Z tn $
036 */
037public final class UnmodifiableBidiMap<K, V>
038        extends AbstractBidiMapDecorator<K, V> implements Unmodifiable {
039
040    /** The inverse unmodifiable map */
041    private UnmodifiableBidiMap<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 BidiMap
052     * @throws IllegalArgumentException if map is null
053     * @since 4.0
054     */
055    public static <K, V> BidiMap<K, V> unmodifiableBidiMap(final BidiMap<? extends K, ? extends V> map) {
056        if (map instanceof Unmodifiable) {
057            @SuppressWarnings("unchecked") // safe to upcast
058            final BidiMap<K, V> tmpMap = (BidiMap<K, V>) map;
059            return tmpMap;
060        }
061        return new UnmodifiableBidiMap<K, V>(map);
062    }
063
064    //-----------------------------------------------------------------------
065    /**
066     * Constructor that wraps (not copies).
067     *
068     * @param map  the map to decorate, must not be null
069     * @throws IllegalArgumentException if map is null
070     */
071    @SuppressWarnings("unchecked") // safe to upcast
072    private UnmodifiableBidiMap(final BidiMap<? extends K, ? extends V> map) {
073        super((BidiMap<K, V>) map);
074    }
075
076    //-----------------------------------------------------------------------
077    @Override
078    public void clear() {
079        throw new UnsupportedOperationException();
080    }
081
082    @Override
083    public V put(final K key, final V value) {
084        throw new UnsupportedOperationException();
085    }
086
087    @Override
088    public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
089        throw new UnsupportedOperationException();
090    }
091
092    @Override
093    public V remove(final Object key) {
094        throw new UnsupportedOperationException();
095    }
096
097    @Override
098    public Set<Map.Entry<K, V>> entrySet() {
099        final Set<Map.Entry<K, V>> set = super.entrySet();
100        return UnmodifiableEntrySet.unmodifiableEntrySet(set);
101    }
102
103    @Override
104    public Set<K> keySet() {
105        final Set<K> set = super.keySet();
106        return UnmodifiableSet.unmodifiableSet(set);
107    }
108
109    @Override
110    public Set<V> values() {
111        final Set<V> set = super.values();
112        return UnmodifiableSet.unmodifiableSet(set);
113    }
114
115    //-----------------------------------------------------------------------
116    @Override
117    public K removeValue(final Object value) {
118        throw new UnsupportedOperationException();
119    }
120
121    @Override
122    public MapIterator<K, V> mapIterator() {
123        final MapIterator<K, V> it = decorated().mapIterator();
124        return UnmodifiableMapIterator.unmodifiableMapIterator(it);
125    }
126
127    @Override
128    public synchronized BidiMap<V, K> inverseBidiMap() {
129        if (inverse == null) {
130            inverse = new UnmodifiableBidiMap<V, K>(decorated().inverseBidiMap());
131            inverse.inverse = this;
132        }
133        return inverse;
134    }
135
136}