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.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    /**
080     * Always throws {@link UnsupportedOperationException}.
081     *
082     * @param obj Ignored.
083     * @throws UnsupportedOperationException Always thrown.
084     */
085    @Override
086    public void add(final E obj) {
087        throw new UnsupportedOperationException("add() is not supported");
088    }
089
090    @Override
091    public boolean hasNext() {
092        return iterator.hasNext();
093    }
094
095    @Override
096    public boolean hasPrevious() {
097        return iterator.hasPrevious();
098    }
099
100    @Override
101    public E next() {
102        return iterator.next();
103    }
104
105    @Override
106    public int nextIndex() {
107        return iterator.nextIndex();
108    }
109
110    @Override
111    public E previous() {
112        return iterator.previous();
113    }
114
115    @Override
116    public int previousIndex() {
117        return iterator.previousIndex();
118    }
119
120    /**
121     * Always throws {@link UnsupportedOperationException}.
122     *
123     * @throws UnsupportedOperationException Always thrown.
124     */
125    @Override
126    public void remove() {
127        throw new UnsupportedOperationException("remove() is not supported");
128    }
129
130    /**
131     * Always throws {@link UnsupportedOperationException}.
132     *
133     * @param ignored Ignored.
134     * @throws UnsupportedOperationException Always thrown.
135     */
136    @Override
137    public void set(final E ignored) {
138        throw new UnsupportedOperationException("set() is not supported");
139    }
140
141}