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.collections.iterators;
18
19 import java.util.ListIterator;
20
21 import org.apache.commons.collections.Unmodifiable;
22
23 /**
24 * Decorates a list iterator such that it cannot be modified.
25 * <p>
26 * Attempts to modify it will result in an UnsupportedOperationException.
27 *
28 * @since 3.0
29 * @version $Id: UnmodifiableListIterator.java 1429905 2013-01-07 17:15:14Z ggregory $
30 */
31 public final class UnmodifiableListIterator<E> implements ListIterator<E>, Unmodifiable {
32
33 /** The iterator being decorated */
34 private final ListIterator<E> iterator;
35
36 //-----------------------------------------------------------------------
37 /**
38 * Decorates the specified iterator such that it cannot be modified.
39 *
40 * @param <E> the element type
41 * @param iterator the iterator to decorate
42 * @return a new unmodifiable list iterator
43 * @throws IllegalArgumentException if the iterator is null
44 */
45 public static <E> ListIterator<E> umodifiableListIterator(final ListIterator<E> iterator) {
46 if (iterator == null) {
47 throw new IllegalArgumentException("ListIterator must not be null");
48 }
49 if (iterator instanceof Unmodifiable) {
50 return iterator;
51 }
52 return new UnmodifiableListIterator<E>(iterator);
53 }
54
55 //-----------------------------------------------------------------------
56 /**
57 * Constructor.
58 *
59 * @param iterator the iterator to decorate
60 */
61 private UnmodifiableListIterator(final ListIterator<E> iterator) {
62 super();
63 this.iterator = iterator;
64 }
65
66 //-----------------------------------------------------------------------
67 public boolean hasNext() {
68 return iterator.hasNext();
69 }
70
71 public E next() {
72 return iterator.next();
73 }
74
75 public int nextIndex() {
76 return iterator.nextIndex();
77 }
78
79 public boolean hasPrevious() {
80 return iterator.hasPrevious();
81 }
82
83 public E previous() {
84 return iterator.previous();
85 }
86
87 public int previousIndex() {
88 return iterator.previousIndex();
89 }
90
91 public void remove() {
92 throw new UnsupportedOperationException("remove() is not supported");
93 }
94
95 public void set(final E obj) {
96 throw new UnsupportedOperationException("set() is not supported");
97 }
98
99 public void add(final E obj) {
100 throw new UnsupportedOperationException("add() is not supported");
101 }
102
103 }