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