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.ListIterator;
20  import java.util.Objects;
21  
22  import org.apache.commons.collections4.Unmodifiable;
23  
24  /**
25   * Decorates a list 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 UnmodifiableListIterator<E> implements ListIterator<E>, Unmodifiable {
34  
35      /**
36       * Decorates the specified iterator such that it cannot be modified.
37       *
38       * @param <E>  the element type
39       * @param iterator  The iterator to decorate
40       * @return A new unmodifiable list iterator
41       * @throws NullPointerException if the iterator is null
42       * @deprecated method name has typo in it. Use {@link org.apache.commons.collections4.iterators.UnmodifiableListIterator#unmodifiableListIterator(ListIterator)} instead.
43       */
44      @Deprecated
45      public static <E> ListIterator<E> umodifiableListIterator(final ListIterator<? extends E> iterator) {
46          return unmodifiableListIterator(iterator);
47      }
48  
49      /**
50       * Decorates the specified iterator such that it cannot be modified.
51       *
52       * @param <E>  the element type
53       * @param iterator  The iterator to decorate
54       * @return A new unmodifiable list iterator
55       * @throws NullPointerException if the iterator is null
56       */
57      public static <E> ListIterator<E> unmodifiableListIterator(final ListIterator<? extends E> iterator) {
58          Objects.requireNonNull(iterator, "iterator");
59          if (iterator instanceof Unmodifiable) {
60              @SuppressWarnings("unchecked") // safe to upcast
61              final ListIterator<E> tmpIterator = (ListIterator<E>) iterator;
62              return tmpIterator;
63          }
64          return new UnmodifiableListIterator<>(iterator);
65      }
66  
67      /** The iterator being decorated */
68      private final ListIterator<? extends E> iterator;
69  
70      /**
71       * Constructs a new instance.
72       *
73       * @param iterator  The iterator to decorate
74       */
75      private UnmodifiableListIterator(final ListIterator<? extends E> iterator) {
76          this.iterator = iterator;
77      }
78  
79      /**
80       * Always throws {@link UnsupportedOperationException}.
81       *
82       * @param obj Ignored.
83       * @throws UnsupportedOperationException Always thrown.
84       */
85      @Override
86      public void add(final E obj) {
87          throw new UnsupportedOperationException("add() is not supported");
88      }
89  
90      @Override
91      public boolean hasNext() {
92          return iterator.hasNext();
93      }
94  
95      @Override
96      public boolean hasPrevious() {
97          return iterator.hasPrevious();
98      }
99  
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 }