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.Iterator;
020import java.util.Objects;
021
022import org.apache.commons.collections4.Unmodifiable;
023
024/**
025 * Decorates an iterator such that it cannot be modified.
026 * <p>
027 * Calling {@link #remove()} throws {@link UnsupportedOperationException}.
028 * </p>
029 *
030 * @param <E> The type of elements returned by this iterator.
031 * @param <T> The wrapped Iterator type.
032 * @since 3.0
033 */
034public final class UnmodifiableIterator<E, T extends Iterator<? extends E>> implements Iterator<E>, Unmodifiable {
035
036    /**
037     * Decorates the specified iterator such that it cannot be modified.
038     * <p>
039     * If the iterator is already unmodifiable it is returned directly.
040     * </p>
041     *
042     * @param <E>  the element type
043     * @param iterator  The iterator to decorate
044     * @return A new unmodifiable iterator
045     * @throws NullPointerException if the iterator is null
046     */
047    public static <E> Iterator<E> unmodifiableIterator(final Iterator<? extends E> iterator) {
048        Objects.requireNonNull(iterator, "iterator");
049        if (iterator instanceof Unmodifiable) {
050            @SuppressWarnings("unchecked") // safe to upcast
051            final Iterator<E> tmpIterator = (Iterator<E>) iterator;
052            return tmpIterator;
053        }
054        return wrap(iterator);
055    }
056
057    static <E, T extends Iterator<? extends E>> UnmodifiableIterator<E, T> wrap(final T iterator) {
058        return new UnmodifiableIterator<>(iterator);
059    }
060
061    /**
062     * The decorated iterator.
063     */
064    private final T iterator;
065
066    /**
067     * Constructs a new instance.
068     *
069     * @param iterator  The iterator to decorate.
070     */
071    private UnmodifiableIterator(final T iterator) {
072        this.iterator = iterator;
073    }
074
075    @Override
076    public boolean hasNext() {
077        return iterator.hasNext();
078    }
079
080    @Override
081    public E next() {
082        return iterator.next();
083    }
084
085    /**
086     * Always throws {@link UnsupportedOperationException}.
087     *
088     * @throws UnsupportedOperationException Always thrown.
089     */
090    // TODO This method can be removed in 5.0 since it's implemented as a default method in Iterator.
091    @Override
092    public void remove() {
093        throw new UnsupportedOperationException("remove");
094    }
095
096    T unwrap() {
097        return iterator;
098    }
099}