UnmodifiableIterator.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.Iterator;
  19. import java.util.Objects;

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

  21. /**
  22.  * Decorates an 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 UnmodifiableIterator<E> implements Iterator<E>, Unmodifiable {

  31.     /**
  32.      * Decorates the specified iterator such that it cannot be modified.
  33.      * <p>
  34.      * If the iterator is already unmodifiable it is returned directly.
  35.      *
  36.      * @param <E>  the element type
  37.      * @param iterator  the iterator to decorate
  38.      * @return a new unmodifiable iterator
  39.      * @throws NullPointerException if the iterator is null
  40.      */
  41.     public static <E> Iterator<E> unmodifiableIterator(final Iterator<? extends E> iterator) {
  42.         Objects.requireNonNull(iterator, "iterator");
  43.         if (iterator instanceof Unmodifiable) {
  44.             @SuppressWarnings("unchecked") // safe to upcast
  45.             final Iterator<E> tmpIterator = (Iterator<E>) iterator;
  46.             return tmpIterator;
  47.         }
  48.         return new UnmodifiableIterator<>(iterator);
  49.     }

  50.     /** The iterator being decorated */
  51.     private final Iterator<? extends E> iterator;

  52.     /**
  53.      * Constructs a new instance.
  54.      *
  55.      * @param iterator  the iterator to decorate
  56.      */
  57.     private UnmodifiableIterator(final Iterator<? extends E> iterator) {
  58.         this.iterator = iterator;
  59.     }

  60.     @Override
  61.     public boolean hasNext() {
  62.         return iterator.hasNext();
  63.     }

  64.     @Override
  65.     public E next() {
  66.         return iterator.next();
  67.     }

  68.     @Override
  69.     public void remove() {
  70.         throw new UnsupportedOperationException("remove() is not supported");
  71.     }

  72. }