    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.multimap;
    018
    019import java.util.Collection;
    020import java.util.Map;
    021import java.util.Map.Entry;
    022import java.util.Set;
    023
    024import org.apache.commons.collections4.MapIterator;
    025import org.apache.commons.collections4.MultiSet;
    026import org.apache.commons.collections4.MultiValuedMap;
    027import org.apache.commons.collections4.Unmodifiable;
    028import org.apache.commons.collections4.collection.UnmodifiableCollection;
    029import org.apache.commons.collections4.iterators.UnmodifiableMapIterator;
    030import org.apache.commons.collections4.map.UnmodifiableMap;
    031import org.apache.commons.collections4.multiset.UnmodifiableMultiSet;
    032import org.apache.commons.collections4.set.UnmodifiableSet;
    033
    034/**
    035 * Decorates another {@link MultiValuedMap} to ensure it can't be altered.
    036 * <p>
    037 * Attempts to modify it will result in an UnsupportedOperationException.
    038 *
    039 * @param <K> the type of key elements
    040 * @param <V> the type of value elements
    041 *
    042 * @since 4.1
    043 */
    044public final class UnmodifiableMultiValuedMap<K, V>
    045        extends AbstractMultiValuedMapDecorator<K, V> implements Unmodifiable {
    046
    047    /** Serialization version */
    048    private static final long serialVersionUID = 20150612L;
    049
    050    /**
    051     * Factory method to create an unmodifiable MultiValuedMap.
    052     * <p>
    053     * If the map passed in is already unmodifiable, it is returned.
    054     *
    055     * @param <K> the type of key elements
    056     * @param <V> the type of value elements
    057     * @param map  the map to decorate, may not be null
    058     * @return an unmodifiable MultiValuedMap
    059     * @throws NullPointerException if map is null
    060     */
    061    @SuppressWarnings("unchecked")
    062    public static <K, V> UnmodifiableMultiValuedMap<K, V> unmodifiableMultiValuedMap(
    063            final MultiValuedMap<? extends K, ? extends V> map) {
    064        if (map instanceof Unmodifiable) {
    065            return (UnmodifiableMultiValuedMap<K, V>) map;
    066        }
    067        return new UnmodifiableMultiValuedMap<>(map);
    068    }
    069
    070    /**
    071     * Constructor that wraps (not copies).
    072     *
    073     * @param map  the MultiValuedMap to decorate, may not be null
    074     * @throws NullPointerException if the map is null
    075     */
    076    @SuppressWarnings("unchecked")
    077    private UnmodifiableMultiValuedMap(final MultiValuedMap<? extends K, ? extends V> map) {
    078        super((MultiValuedMap<K, V>) map);
    079    }
    080
    081    @Override
    082    public Collection<V> remove(final Object key) {
    083        throw new UnsupportedOperationException();
    084    }
    085
    086    @Override
    087    public boolean removeMapping(final Object key, final Object item) {
    088        throw new UnsupportedOperationException();
    089    }
    090
    091    @Override
    092    public void clear() {
    093        throw new UnsupportedOperationException();
    094    }
    095
    096    @Override
    097    public Collection<V> get(final K key) {
    098        return UnmodifiableCollection.unmodifiableCollection(decorated().get(key));
    099    }
    100
    101    @Override
    102    public boolean put(final K key, final V value) {
    103        throw new UnsupportedOperationException();
    104    }
    105
    106    @Override
    107    public Set<K> keySet() {
    108        return UnmodifiableSet.unmodifiableSet(decorated().keySet());
    109    }
    110
    111    @Override
    112    public Collection<Entry<K, V>> entries() {
    113        return UnmodifiableCollection.unmodifiableCollection(decorated().entries());
    114    }
    115
    116    @Override
    117    public MultiSet<K> keys() {
    118        return UnmodifiableMultiSet.unmodifiableMultiSet(decorated().keys());
    119    }
    120
    121    @Override
    122    public Collection<V> values() {
    123        return UnmodifiableCollection.unmodifiableCollection(decorated().values());
    124    }
    125
    126    @Override
    127    public Map<K, Collection<V>> asMap() {
    128        return UnmodifiableMap.unmodifiableMap(decorated().asMap());
    129    }
    130
    131    @Override
    132    public MapIterator<K, V> mapIterator() {
    133        return UnmodifiableMapIterator.unmodifiableMapIterator(decorated().mapIterator());
    134    }
    135
    136    @Override
    137    public boolean putAll(final K key, final Iterable<? extends V> values) {
    138        throw new UnsupportedOperationException();
    139    }
    140
    141    @Override
    142    public boolean putAll(final Map<? extends K, ? extends V> map) {
    143        throw new UnsupportedOperationException();
    144    }
    145
    146    @Override
    147    public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
    148        throw new UnsupportedOperationException();
    149    }
    150
    151}