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 *
034 * @param <K> the type of the keys in this map
035 * @param <V> the type of the values in this map
036 * @since 3.0
037 */
038public final class UnmodifiableBidiMap<K, V>
039        extends AbstractBidiMapDecorator<K, V> implements Unmodifiable {
040
041    /** The inverse unmodifiable map */
042    private UnmodifiableBidiMap<V, K> inverse;
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 BidiMap
053     * @throws NullPointerException if map is null
054     * @since 4.0
055     */
056    public static <K, V> BidiMap<K, V> unmodifiableBidiMap(final BidiMap<? extends K, ? extends V> map) {
057        if (map instanceof Unmodifiable) {
058            @SuppressWarnings("unchecked") // safe to upcast
059            final BidiMap<K, V> tmpMap = (BidiMap<K, V>) map;
060            return tmpMap;
061        }
062        return new UnmodifiableBidiMap<>(map);
063    }
064
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    @Override
079    public void clear() {
080        throw new UnsupportedOperationException();
081    }
082
083    @Override
084    public V put(final K key, final V value) {
085        throw new UnsupportedOperationException();
086    }
087
088    @Override
089    public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
090        throw new UnsupportedOperationException();
091    }
092
093    @Override
094    public V remove(final Object key) {
095        throw new UnsupportedOperationException();
096    }
097
098    @Override
099    public Set<Map.Entry<K, V>> entrySet() {
100        final Set<Map.Entry<K, V>> set = super.entrySet();
101        return UnmodifiableEntrySet.unmodifiableEntrySet(set);
102    }
103
104    @Override
105    public Set<K> keySet() {
106        final Set<K> set = super.keySet();
107        return UnmodifiableSet.unmodifiableSet(set);
108    }
109
110    @Override
111    public Set<V> values() {
112        final Set<V> set = super.values();
113        return UnmodifiableSet.unmodifiableSet(set);
114    }
115
116    //-----------------------------------------------------------------------
117    @Override
118    public K removeValue(final Object value) {
119        throw new UnsupportedOperationException();
120    }
121
122    @Override
123    public MapIterator<K, V> mapIterator() {
124        final MapIterator<K, V> it = decorated().mapIterator();
125        return UnmodifiableMapIterator.unmodifiableMapIterator(it);
126    }
127
128    @Override
129    public synchronized BidiMap<V, K> inverseBidiMap() {
130        if (inverse == null) {
131            inverse = new UnmodifiableBidiMap<>(decorated().inverseBidiMap());
132            inverse.inverse = this;
133        }
134        return inverse;
135    }
136
137}