1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.collections15.iterators;
17
18 import java.util.ListIterator;
19
20 import net.sf.collections15.Unmodifiable;
21
22 /***
23 * Decorates a list iterator such that it cannot be modified.
24 *
25 * @todo Write tests here
26 *
27 * @since Commons Collections 3.0
28 * @version $Revision: 1.1 $ $Date: 2005/02/27 10:30:36 $
29 *
30 * @author Stephen Colebourne
31 * @author Mauro Franceschini
32 */
33 public final class UnmodifiableListIterator<E>
34 implements ListIterator<E>, Unmodifiable {
35
36
37
38
39 /*** The iterator being decorated */
40 private ListIterator<E> iterator;
41
42
43
44
45 /***
46 * Decorates the specified iterator such that it cannot be modified.
47 *
48 * @param iterator the iterator to decorate
49 * @throws IllegalArgumentException if the iterator is <code>null</code>
50 */
51 public static <E> ListIterator<E> decorate(final ListIterator<E> iterator) {
52 if (iterator == null) {
53 throw new IllegalArgumentException("ListIterator must not be null");
54 }
55 if (iterator instanceof Unmodifiable) {
56 return iterator;
57 }
58 return new UnmodifiableListIterator<E>(iterator);
59 }
60
61
62
63
64 /***
65 * Create a new <code>UnmodifiableListIterator</code> that wraps the
66 * provided list iterator.
67 *
68 * @param iterator the iterator to decorate
69 */
70 private UnmodifiableListIterator(final ListIterator<E> iterator) {
71 super();
72 this.iterator = iterator;
73 }
74
75
76
77
78 public boolean hasNext() {
79 return iterator.hasNext();
80 }
81
82 public E next() {
83 return iterator.next();
84 }
85
86 public int nextIndex() {
87 return iterator.nextIndex();
88 }
89
90 public boolean hasPrevious() {
91 return iterator.hasPrevious();
92 }
93
94 public E previous() {
95 return iterator.previous();
96 }
97
98 public int previousIndex() {
99 return iterator.previousIndex();
100 }
101
102 public void remove() {
103 throw new UnsupportedOperationException("remove() is not supported");
104 }
105
106 public void set(final E obj) {
107 throw new UnsupportedOperationException("set() is not supported");
108 }
109
110 public void add(final E obj) {
111 throw new UnsupportedOperationException("add() is not supported");
112 }
113
114 }