    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.trie;
    018
    019import java.io.Serializable;
    020import java.util.Collection;
    021import java.util.Collections;
    022import java.util.Comparator;
    023import java.util.Map;
    024import java.util.Objects;
    025import java.util.Set;
    026import java.util.SortedMap;
    027
    028import org.apache.commons.collections4.OrderedMapIterator;
    029import org.apache.commons.collections4.Trie;
    030import org.apache.commons.collections4.Unmodifiable;
    031import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator;
    032
    033/**
    034 * An unmodifiable {@link Trie}.
    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 4.0
    039 */
    040public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodifiable {
    041
    042    /** Serialization version */
    043    private static final long serialVersionUID = -7156426030315945159L;
    044
    045    /**
    046     * Factory method to create an unmodifiable trie.
    047     *
    048     * @param <K>  the key type
    049     * @param <V>  the value type
    050     * @param trie  the trie to decorate, must not be null
    051     * @return a new unmodifiable trie
    052     * @throws NullPointerException if trie is null
    053     */
    054    public static <K, V> Trie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie) {
    055        if (trie instanceof Unmodifiable) {
    056            @SuppressWarnings("unchecked") // safe to upcast
    057            final Trie<K, V> tmpTrie = (Trie<K, V>) trie;
    058            return tmpTrie;
    059        }
    060        return new UnmodifiableTrie<>(trie);
    061    }
    062
    063    private final Trie<K, V> delegate;
    064
    065    /**
    066     * Constructor that wraps (not copies).
    067     *
    068     * @param trie  the trie to decorate, must not be null
    069     * @throws NullPointerException if trie is null
    070     */
    071    public UnmodifiableTrie(final Trie<K, ? extends V> trie) {
    072        @SuppressWarnings("unchecked") // safe to upcast
    073        final Trie<K, V> tmpTrie = (Trie<K, V>) Objects.requireNonNull(trie, "trie");
    074        this.delegate = tmpTrie;
    075    }
    076
    077    @Override
    078    public void clear() {
    079        throw new UnsupportedOperationException();
    080    }
    081
    082    @Override
    083    public Comparator<? super K> comparator() {
    084        return delegate.comparator();
    085    }
    086
    087    @Override
    088    public boolean containsKey(final Object key) {
    089        return delegate.containsKey(key);
    090    }
    091
    092    @Override
    093    public boolean containsValue(final Object value) {
    094        return delegate.containsValue(value);
    095    }
    096
    097    @Override
    098    public Set<Entry<K, V>> entrySet() {
    099        return Collections.unmodifiableSet(delegate.entrySet());
    100    }
    101
    102    @Override
    103    public boolean equals(final Object obj) {
    104        return delegate.equals(obj);
    105    }
    106
    107    @Override
    108    public K firstKey() {
    109        return delegate.firstKey();
    110    }
    111
    112    @Override
    113    public V get(final Object key) {
    114        return delegate.get(key);
    115    }
    116
    117    @Override
    118    public int hashCode() {
    119        return delegate.hashCode();
    120    }
    121
    122    @Override
    123    public SortedMap<K, V> headMap(final K toKey) {
    124        return Collections.unmodifiableSortedMap(delegate.headMap(toKey));
    125    }
    126
    127    @Override
    128    public boolean isEmpty() {
    129        return delegate.isEmpty();
    130    }
    131
    132    @Override
    133    public Set<K> keySet() {
    134        return Collections.unmodifiableSet(delegate.keySet());
    135    }
    136
    137    @Override
    138    public K lastKey() {
    139        return delegate.lastKey();
    140    }
    141
    142    @Override
    143    public OrderedMapIterator<K, V> mapIterator() {
    144        final OrderedMapIterator<K, V> it = delegate.mapIterator();
    145        return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
    146    }
    147
    148    @Override
    149    public K nextKey(final K key) {
    150        return delegate.nextKey(key);
    151    }
    152
    153    @Override
    154    public SortedMap<K, V> prefixMap(final K key) {
    155        return Collections.unmodifiableSortedMap(delegate.prefixMap(key));
    156    }
    157
    158    @Override
    159    public K previousKey(final K key) {
    160        return delegate.previousKey(key);
    161    }
    162
    163    @Override
    164    public V put(final K key, final V value) {
    165        throw new UnsupportedOperationException();
    166    }
    167
    168    @Override
    169    public void putAll(final Map<? extends K, ? extends V> m) {
    170        throw new UnsupportedOperationException();
    171    }
    172
    173    @Override
    174    public V remove(final Object key) {
    175        throw new UnsupportedOperationException();
    176    }
    177
    178    @Override
    179    public int size() {
    180        return delegate.size();
    181    }
    182
    183    @Override
    184    public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
    185        return Collections.unmodifiableSortedMap(delegate.subMap(fromKey, toKey));
    186    }
    187
    188    @Override
    189    public SortedMap<K, V> tailMap(final K fromKey) {
    190        return Collections.unmodifiableSortedMap(delegate.tailMap(fromKey));
    191    }
    192
    193    @Override
    194    public String toString() {
    195        return delegate.toString();
    196    }
    197
    198    @Override
    199    public Collection<V> values() {
    200        return Collections.unmodifiableCollection(delegate.values());
    201    }
    202
    203}