UnmodifiableListIterator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.iterators;

  18. import java.util.ListIterator;
  19. import java.util.Objects;

  20. import org.apache.commons.collections4.Unmodifiable;

  21. /**
  22.  * Decorates a list iterator such that it cannot be modified.
  23.  * <p>
  24.  * Attempts to modify it will result in an UnsupportedOperationException.
  25.  * </p>
  26.  *
  27.  * @param <E> the type of elements returned by this iterator.
  28.  * @since 3.0
  29.  */
  30. public final class UnmodifiableListIterator<E> implements ListIterator<E>, Unmodifiable {

  31.     /**
  32.      * Decorates the specified iterator such that it cannot be modified.
  33.      *
  34.      * @param <E>  the element type
  35.      * @param iterator  the iterator to decorate
  36.      * @return a new unmodifiable list iterator
  37.      * @throws NullPointerException if the iterator is null
  38.      * @deprecated method name has typo in it. Use {@link org.apache.commons.collections4.iterators.UnmodifiableListIterator#unmodifiableListIterator(ListIterator)} instead.
  39.      */
  40.     @Deprecated
  41.     public static <E> ListIterator<E> umodifiableListIterator(final ListIterator<? extends E> iterator) {
  42.         return unmodifiableListIterator(iterator);
  43.     }

  44.     /**
  45.      * Decorates the specified iterator such that it cannot be modified.
  46.      *
  47.      * @param <E>  the element type
  48.      * @param iterator  the iterator to decorate
  49.      * @return a new unmodifiable list iterator
  50.      * @throws NullPointerException if the iterator is null
  51.      */
  52.     public static <E> ListIterator<E> unmodifiableListIterator(final ListIterator<? extends E> iterator) {
  53.         Objects.requireNonNull(iterator, "iterator");
  54.         if (iterator instanceof Unmodifiable) {
  55.             @SuppressWarnings("unchecked") // safe to upcast
  56.             final ListIterator<E> tmpIterator = (ListIterator<E>) iterator;
  57.             return tmpIterator;
  58.         }
  59.         return new UnmodifiableListIterator<>(iterator);
  60.     }

  61.     /** The iterator being decorated */
  62.     private final ListIterator<? extends E> iterator;

  63.     /**
  64.      * Constructs a new instance.
  65.      *
  66.      * @param iterator  the iterator to decorate
  67.      */
  68.     private UnmodifiableListIterator(final ListIterator<? extends E> iterator) {
  69.         this.iterator = iterator;
  70.     }

  71.     @Override
  72.     public void add(final E obj) {
  73.         throw new UnsupportedOperationException("add() is not supported");
  74.     }

  75.     @Override
  76.     public boolean hasNext() {
  77.         return iterator.hasNext();
  78.     }

  79.     @Override
  80.     public boolean hasPrevious() {
  81.         return iterator.hasPrevious();
  82.     }

  83.     @Override
  84.     public E next() {
  85.         return iterator.next();
  86.     }

  87.     @Override
  88.     public int nextIndex() {
  89.         return iterator.nextIndex();
  90.     }

  91.     @Override
  92.     public E previous() {
  93.         return iterator.previous();
  94.     }

  95.     @Override
  96.     public int previousIndex() {
  97.         return iterator.previousIndex();
  98.     }

  99.     @Override
  100.     public void remove() {
  101.         throw new UnsupportedOperationException("remove() is not supported");
  102.     }

  103.     @Override
  104.     public void set(final E ignored) {
  105.         throw new UnsupportedOperationException("set() is not supported");
  106.     }

  107. }