    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.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    @Override
    078    public void clear() {
    079        throw new UnsupportedOperationException();
    080    }
    081
    082    @Override
    083    public Set<Map.Entry<K, V>> entrySet() {
    084        final Set<Map.Entry<K, V>> set = super.entrySet();
    085        return UnmodifiableEntrySet.unmodifiableEntrySet(set);
    086    }
    087
    088    @Override
    089    public synchronized BidiMap<V, K> inverseBidiMap() {
    090        if (inverse == null) {
    091            inverse = new UnmodifiableBidiMap<>(decorated().inverseBidiMap());
    092            inverse.inverse = this;
    093        }
    094        return inverse;
    095    }
    096
    097    @Override
    098    public Set<K> keySet() {
    099        final Set<K> set = super.keySet();
    100        return UnmodifiableSet.unmodifiableSet(set);
    101    }
    102
    103    @Override
    104    public MapIterator<K, V> mapIterator() {
    105        final MapIterator<K, V> it = decorated().mapIterator();
    106        return UnmodifiableMapIterator.unmodifiableMapIterator(it);
    107    }
    108
    109    @Override
    110    public V put(final K key, final V value) {
    111        throw new UnsupportedOperationException();
    112    }
    113
    114    @Override
    115    public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
    116        throw new UnsupportedOperationException();
    117    }
    118
    119    @Override
    120    public V remove(final Object key) {
    121        throw new UnsupportedOperationException();
    122    }
    123
    124    @Override
    125    public K removeValue(final Object value) {
    126        throw new UnsupportedOperationException();
    127    }
    128
    129    @Override
    130    public Set<V> values() {
    131        final Set<V> set = super.values();
    132        return UnmodifiableSet.unmodifiableSet(set);
    133    }
    134
    135}