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.ListIterator;
020
021import org.apache.commons.collections4.Unmodifiable;
022
023/**
024 * Decorates a list 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 UnmodifiableListIterator<E> implements ListIterator<E>, Unmodifiable {
031
032    /** The iterator being decorated */
033    private final ListIterator<? extends E> iterator;
034
035    //-----------------------------------------------------------------------
036    /**
037     * Decorates the specified iterator such that it cannot be modified.
038     *
039     * @param <E>  the element type
040     * @param iterator  the iterator to decorate
041     * @return a new unmodifiable list iterator
042     * @throws NullPointerException if the iterator is null
043     */
044    public static <E> ListIterator<E> umodifiableListIterator(final ListIterator<? extends E> iterator) {
045        if (iterator == null) {
046            throw new NullPointerException("ListIterator must not be null");
047        }
048        if (iterator instanceof Unmodifiable) {
049            @SuppressWarnings("unchecked") // safe to upcast
050            final ListIterator<E> tmpIterator = (ListIterator<E>) iterator;
051            return tmpIterator;
052        }
053        return new UnmodifiableListIterator<>(iterator);
054    }
055
056    //-----------------------------------------------------------------------
057    /**
058     * Constructor.
059     *
060     * @param iterator  the iterator to decorate
061     */
062    private UnmodifiableListIterator(final ListIterator<? extends E> iterator) {
063        super();
064        this.iterator = iterator;
065    }
066
067    //-----------------------------------------------------------------------
068    @Override
069    public boolean hasNext() {
070        return iterator.hasNext();
071    }
072
073    @Override
074    public E next() {
075        return iterator.next();
076    }
077
078    @Override
079    public int nextIndex() {
080        return iterator.nextIndex();
081    }
082
083    @Override
084    public boolean hasPrevious() {
085        return iterator.hasPrevious();
086    }
087
088    @Override
089    public E previous() {
090        return iterator.previous();
091    }
092
093    @Override
094    public int previousIndex() {
095        return iterator.previousIndex();
096    }
097
098    @Override
099    public void remove() {
100        throw new UnsupportedOperationException("remove() is not supported");
101    }
102
103    @Override
104    public void set(final E obj) {
105        throw new UnsupportedOperationException("set() is not supported");
106    }
107
108    @Override
109    public void add(final E obj) {
110        throw new UnsupportedOperationException("add() is not supported");
111    }
112
113}