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.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 @Override
80 public void add(final E obj) {
81 throw new UnsupportedOperationException("add() is not supported");
82 }
83
84 @Override
85 public boolean hasNext() {
86 return iterator.hasNext();
87 }
88
89 @Override
90 public boolean hasPrevious() {
91 return iterator.hasPrevious();
92 }
93
94 @Override
95 public E next() {
96 return iterator.next();
97 }
98
99 @Override
100 public int nextIndex() {
101 return iterator.nextIndex();
102 }
103
104 @Override
105 public E previous() {
106 return iterator.previous();
107 }
108
109 @Override
110 public int previousIndex() {
111 return iterator.previousIndex();
112 }
113
114 @Override
115 public void remove() {
116 throw new UnsupportedOperationException("remove() is not supported");
117 }
118
119 @Override
120 public void set(final E ignored) {
121 throw new UnsupportedOperationException("set() is not supported");
122 }
123
124 }