    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 *      https://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 java.util.Objects;
    020
    021import org.apache.commons.collections4.OrderedMapIterator;
    022import org.apache.commons.collections4.Unmodifiable;
    023
    024/**
    025 * Decorates an ordered map iterator such that it cannot be modified.
    026 * <p>
    027 * Attempts to modify it will result in an UnsupportedOperationException.
    028 * </p>
    029 *
    030 * @param <K> The type of keys
    031 * @param <V> The type of mapped values
    032 * @since 3.0
    033 */
    034public final class UnmodifiableOrderedMapIterator<K, V> implements OrderedMapIterator<K, V>,
    035        Unmodifiable {
    036
    037    /**
    038     * Decorates the specified iterator such that it cannot be modified.
    039     *
    040     * @param <K>  the key type
    041     * @param <V>  the value type
    042     * @param iterator  The iterator to decorate
    043     * @return A new unmodifiable ordered map iterator
    044     * @throws NullPointerException if the iterator is null
    045     */
    046    public static <K, V> OrderedMapIterator<K, V> unmodifiableOrderedMapIterator(
    047            final OrderedMapIterator<K, ? extends V> iterator) {
    048        Objects.requireNonNull(iterator, "iterator");
    049        if (iterator instanceof Unmodifiable) {
    050            @SuppressWarnings("unchecked") // safe to upcast
    051            final OrderedMapIterator<K, V> tmpIterator = (OrderedMapIterator<K, V>) iterator;
    052            return tmpIterator;
    053        }
    054        return new UnmodifiableOrderedMapIterator<>(iterator);
    055    }
    056
    057    /** The iterator being decorated */
    058    private final OrderedMapIterator<? extends K, ? extends V> iterator;
    059
    060    /**
    061     * Constructs a new instance.
    062     *
    063     * @param iterator  The iterator to decorate
    064     */
    065    private UnmodifiableOrderedMapIterator(final OrderedMapIterator<K, ? extends V> iterator) {
    066        this.iterator = iterator;
    067    }
    068
    069    @Override
    070    public K getKey() {
    071        return iterator.getKey();
    072    }
    073
    074    @Override
    075    public V getValue() {
    076        return iterator.getValue();
    077    }
    078
    079    @Override
    080    public boolean hasNext() {
    081        return iterator.hasNext();
    082    }
    083
    084    @Override
    085    public boolean hasPrevious() {
    086        return iterator.hasPrevious();
    087    }
    088
    089    @Override
    090    public K next() {
    091        return iterator.next();
    092    }
    093
    094    @Override
    095    public K previous() {
    096        return iterator.previous();
    097    }
    098
    099    /**
    100     * Always throws {@link UnsupportedOperationException}.
    101     *
    102     * @throws UnsupportedOperationException Always thrown.
    103     */
    104    @Override
    105    public void remove() {
    106        throw new UnsupportedOperationException("remove() is not supported");
    107    }
    108
    109    /**
    110     * Always throws {@link UnsupportedOperationException}.
    111     *
    112     * @param value Ignored.
    113     * @throws UnsupportedOperationException Always thrown.
    114     */
    115    @Override
    116    public V setValue(final V value) {
    117        throw new UnsupportedOperationException("setValue() is not supported");
    118    }
    119
    120}