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    *      https://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   * Calling {@link #remove()} throws {@link UnsupportedOperationException}.
28   * </p>
29   *
30   * @param <E> The type of elements returned by this iterator.
31   * @param <T> The wrapped Iterator type.
32   * @since 3.0
33   */
34  public final class UnmodifiableIterator<E, T extends Iterator<? extends E>> implements Iterator<E>, Unmodifiable {
35  
36      /**
37       * Decorates the specified iterator such that it cannot be modified.
38       * <p>
39       * If the iterator is already unmodifiable it is returned directly.
40       * </p>
41       *
42       * @param <E>  the element type
43       * @param iterator  The iterator to decorate
44       * @return A new unmodifiable iterator
45       * @throws NullPointerException if the iterator is null
46       */
47      public static <E> Iterator<E> unmodifiableIterator(final Iterator<? extends E> iterator) {
48          Objects.requireNonNull(iterator, "iterator");
49          if (iterator instanceof Unmodifiable) {
50              @SuppressWarnings("unchecked") // safe to upcast
51              final Iterator<E> tmpIterator = (Iterator<E>) iterator;
52              return tmpIterator;
53          }
54          return wrap(iterator);
55      }
56  
57      static <E, T extends Iterator<? extends E>> UnmodifiableIterator<E, T> wrap(final T iterator) {
58          return new UnmodifiableIterator<>(iterator);
59      }
60  
61      /**
62       * The decorated iterator.
63       */
64      private final T iterator;
65  
66      /**
67       * Constructs a new instance.
68       *
69       * @param iterator  The iterator to decorate.
70       */
71      private UnmodifiableIterator(final T iterator) {
72          this.iterator = iterator;
73      }
74  
75      @Override
76      public boolean hasNext() {
77          return iterator.hasNext();
78      }
79  
80      @Override
81      public E next() {
82          return iterator.next();
83      }
84  
85      /**
86       * Always throws {@link UnsupportedOperationException}.
87       *
88       * @throws UnsupportedOperationException Always thrown.
89       */
90      // TODO This method can be removed in 5.0 since it's implemented as a default method in Iterator.
91      @Override
92      public void remove() {
93          throw new UnsupportedOperationException("remove");
94      }
95  
96      T unwrap() {
97          return iterator;
98      }
99  }