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.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    @Override
080    public void clear() {
081        throw new UnsupportedOperationException();
082    }
083
084    @Override
085    public Set<Map.Entry<K, V>> entrySet() {
086        final Set<Map.Entry<K, V>> set = super.entrySet();
087        return UnmodifiableEntrySet.unmodifiableEntrySet(set);
088    }
089
090    @Override
091    public SortedMap<K, V> headMap(final K toKey) {
092        final SortedMap<K, V> sm = decorated().headMap(toKey);
093        return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
094    }
095
096    @Override
097    public SortedBidiMap<V, K> inverseBidiMap() {
098        if (inverse == null) {
099            inverse = new UnmodifiableSortedBidiMap<>(decorated().inverseBidiMap());
100            inverse.inverse = this;
101        }
102        return inverse;
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 OrderedMapIterator<K, V> mapIterator() {
113        final OrderedMapIterator<K, V> it = decorated().mapIterator();
114        return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
115    }
116
117    @Override
118    public V put(final K key, final V value) {
119        throw new UnsupportedOperationException();
120    }
121
122    @Override
123    public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
124        throw new UnsupportedOperationException();
125    }
126
127    @Override
128    public V remove(final Object key) {
129        throw new UnsupportedOperationException();
130    }
131
132    @Override
133    public K removeValue(final Object value) {
134        throw new UnsupportedOperationException();
135    }
136
137    @Override
138    public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
139        final SortedMap<K, V> sm = decorated().subMap(fromKey, toKey);
140        return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
141    }
142
143    @Override
144    public SortedMap<K, V> tailMap(final K fromKey) {
145        final SortedMap<K, V> sm = decorated().tailMap(fromKey);
146        return UnmodifiableSortedMap.unmodifiableSortedMap(sm);
147    }
148
149    @Override
150    public Set<V> values() {
151        final Set<V> set = super.values();
152        return UnmodifiableSet.unmodifiableSet(set);
153    }
154
155}