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 java.util.Iterator;
020
021import org.apache.commons.collections4.Unmodifiable;
022
023/**
024 * Decorates an iterator such that it cannot be modified.
025 * <p>
026 * Attempts to modify it will result in an UnsupportedOperationException.
027 *
028 * @since 3.0
029 */
030public final class UnmodifiableIterator<E> implements Iterator<E>, Unmodifiable {
031
032    /** The iterator being decorated */
033    private final Iterator<? extends E> iterator;
034
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     *
041     * @param <E>  the element type
042     * @param iterator  the iterator to decorate
043     * @return a new unmodifiable iterator
044     * @throws NullPointerException if the iterator is null
045     */
046    public static <E> Iterator<E> unmodifiableIterator(final Iterator<? extends E> iterator) {
047        if (iterator == null) {
048            throw new NullPointerException("Iterator must not be null");
049        }
050        if (iterator instanceof Unmodifiable) {
051            @SuppressWarnings("unchecked") // safe to upcast
052            final Iterator<E> tmpIterator = (Iterator<E>) iterator;
053            return tmpIterator;
054        }
055        return new UnmodifiableIterator<>(iterator);
056    }
057
058    //-----------------------------------------------------------------------
059    /**
060     * Constructor.
061     *
062     * @param iterator  the iterator to decorate
063     */
064    private UnmodifiableIterator(final Iterator<? extends E> iterator) {
065        super();
066        this.iterator = iterator;
067    }
068
069    //-----------------------------------------------------------------------
070    @Override
071    public boolean hasNext() {
072        return iterator.hasNext();
073    }
074
075    @Override
076    public E next() {
077        return iterator.next();
078    }
079
080    @Override
081    public void remove() {
082        throw new UnsupportedOperationException("remove() is not supported");
083    }
084
085}