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;
020import java.util.Objects;
021
022import org.apache.commons.collections4.Unmodifiable;
023
024/**
025 * Decorates a list iterator such that it cannot be modified.
026 * <p>
027 * Attempts to modify it will result in an UnsupportedOperationException.
028 * </p>
029 *
030 * @param <E> the type of elements returned by this iterator.
031 * @since 3.0
032 */
033public final class UnmodifiableListIterator<E> implements ListIterator<E>, Unmodifiable {
034
035    /**
036     * Decorates the specified iterator such that it cannot be modified.
037     *
038     * @param <E>  the element type
039     * @param iterator  the iterator to decorate
040     * @return a new unmodifiable list iterator
041     * @throws NullPointerException if the iterator is null
042     * @deprecated method name has typo in it. Use {@link org.apache.commons.collections4.iterators.UnmodifiableListIterator#unmodifiableListIterator(ListIterator)} instead.
043     */
044    @Deprecated
045    public static <E> ListIterator<E> umodifiableListIterator(final ListIterator<? extends E> iterator) {
046        return unmodifiableListIterator(iterator);
047    }
048
049    /**
050     * Decorates the specified iterator such that it cannot be modified.
051     *
052     * @param <E>  the element type
053     * @param iterator  the iterator to decorate
054     * @return a new unmodifiable list iterator
055     * @throws NullPointerException if the iterator is null
056     */
057    public static <E> ListIterator<E> unmodifiableListIterator(final ListIterator<? extends E> iterator) {
058        Objects.requireNonNull(iterator, "iterator");
059        if (iterator instanceof Unmodifiable) {
060            @SuppressWarnings("unchecked") // safe to upcast
061            final ListIterator<E> tmpIterator = (ListIterator<E>) iterator;
062            return tmpIterator;
063        }
064        return new UnmodifiableListIterator<>(iterator);
065    }
066
067    /** The iterator being decorated */
068    private final ListIterator<? extends E> iterator;
069
070    /**
071     * Constructs a new instance.
072     *
073     * @param iterator  the iterator to decorate
074     */
075    private UnmodifiableListIterator(final ListIterator<? extends E> iterator) {
076        this.iterator = iterator;
077    }
078
079    @Override
080    public void add(final E obj) {
081        throw new UnsupportedOperationException("add() is not supported");
082    }
083
084    @Override
085    public boolean hasNext() {
086        return iterator.hasNext();
087    }
088
089    @Override
090    public boolean hasPrevious() {
091        return iterator.hasPrevious();
092    }
093
094    @Override
095    public E next() {
096        return iterator.next();
097    }
098
099    @Override
100    public int nextIndex() {
101        return iterator.nextIndex();
102    }
103
104    @Override
105    public E previous() {
106        return iterator.previous();
107    }
108
109    @Override
110    public int previousIndex() {
111        return iterator.previousIndex();
112    }
113
114    @Override
115    public void remove() {
116        throw new UnsupportedOperationException("remove() is not supported");
117    }
118
119    @Override
120    public void set(final E obj) {
121        throw new UnsupportedOperationException("set() is not supported");
122    }
123
124}