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.iterators;
018
019import org.apache.commons.collections4.MapIterator;
020import org.apache.commons.collections4.Unmodifiable;
021
022/**
023 * Decorates a map iterator such that it cannot be modified.
024 * <p>
025 * Attempts to modify it will result in an UnsupportedOperationException.
026 *
027 * @since 3.0
028 */
029public final class UnmodifiableMapIterator<K, V> implements MapIterator<K, V>, Unmodifiable {
030
031    /** The iterator being decorated */
032    private final MapIterator<? extends K, ? extends V> iterator;
033
034    //-----------------------------------------------------------------------
035    /**
036     * Decorates the specified iterator such that it cannot be modified.
037     *
038     * @param <K>  the key type
039     * @param <V>  the value type
040     * @param iterator  the iterator to decorate
041     * @return a new unmodifiable map iterator
042     * @throws NullPointerException if the iterator is null
043     */
044    public static <K, V> MapIterator<K, V> unmodifiableMapIterator(
045            final MapIterator<? extends K, ? extends V> iterator) {
046        if (iterator == null) {
047            throw new NullPointerException("MapIterator must not be null");
048        }
049        if (iterator instanceof Unmodifiable) {
050            @SuppressWarnings("unchecked") // safe to upcast
051            final MapIterator<K, V> tmpIterator = (MapIterator<K, V>) iterator;
052            return tmpIterator;
053        }
054        return new UnmodifiableMapIterator<>(iterator);
055    }
056
057    //-----------------------------------------------------------------------
058    /**
059     * Constructor.
060     *
061     * @param iterator  the iterator to decorate
062     */
063    private UnmodifiableMapIterator(final MapIterator<? extends K, ? extends V> iterator) {
064        super();
065        this.iterator = iterator;
066    }
067
068    //-----------------------------------------------------------------------
069    @Override
070    public boolean hasNext() {
071        return iterator.hasNext();
072    }
073
074    @Override
075    public K next() {
076        return iterator.next();
077    }
078
079    @Override
080    public K getKey() {
081        return iterator.getKey();
082    }
083
084    @Override
085    public V getValue() {
086        return iterator.getValue();
087    }
088
089    @Override
090    public V setValue(final V value) {
091        throw new UnsupportedOperationException("setValue() is not supported");
092    }
093
094    @Override
095    public void remove() {
096        throw new UnsupportedOperationException("remove() is not supported");
097    }
098
099}