View Javadoc
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  
19  import java.util.Iterator;
20  import java.util.Objects;
21  
22  import org.apache.commons.collections4.Unmodifiable;
23  
24  /**
25   * Decorates an iterator such that it cannot be modified.
26   * <p>
27   * Attempts to modify it will result in an UnsupportedOperationException.
28   * </p>
29   *
30   * @param <E> the type of elements returned by this iterator.
31   * @since 3.0
32   */
33  public final class UnmodifiableIterator<E> implements Iterator<E>, Unmodifiable {
34  
35      /**
36       * Decorates the specified iterator such that it cannot be modified.
37       * <p>
38       * If the iterator is already unmodifiable it is returned directly.
39       *
40       * @param <E>  the element type
41       * @param iterator  the iterator to decorate
42       * @return a new unmodifiable iterator
43       * @throws NullPointerException if the iterator is null
44       */
45      public static <E> Iterator<E> unmodifiableIterator(final Iterator<? extends E> iterator) {
46          Objects.requireNonNull(iterator, "iterator");
47          if (iterator instanceof Unmodifiable) {
48              @SuppressWarnings("unchecked") // safe to upcast
49              final Iterator<E> tmpIterator = (Iterator<E>) iterator;
50              return tmpIterator;
51          }
52          return new UnmodifiableIterator<>(iterator);
53      }
54  
55      /** The iterator being decorated */
56      private final Iterator<? extends E> iterator;
57  
58      /**
59       * Constructs a new instance.
60       *
61       * @param iterator  the iterator to decorate
62       */
63      private UnmodifiableIterator(final Iterator<? extends E> iterator) {
64          this.iterator = iterator;
65      }
66  
67      @Override
68      public boolean hasNext() {
69          return iterator.hasNext();
70      }
71  
72      @Override
73      public E next() {
74          return iterator.next();
75      }
76  
77      @Override
78      public void remove() {
79          throw new UnsupportedOperationException("remove() is not supported");
80      }
81  
82  }