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.Iterator;
19
20 import net.sf.collections15.Unmodifiable;
21
22 /***
23 * Decorates an 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 UnmodifiableIterator<E>
34 implements Iterator<E>, Unmodifiable {
35
36
37
38
39 /*** The iterator being decorated */
40 private Iterator<E> iterator;
41
42
43
44
45 /***
46 * Decorates the specified iterator such that it cannot be modified.
47 * <p>
48 * If the iterator is already unmodifiable it is returned directly.
49 *
50 * @param iterator the iterator to decorate
51 *
52 * @throws IllegalArgumentException if the iterator is <code>null</code>
53 */
54 public static <E> Iterator<E> decorate(final Iterator<E> iterator) {
55 if (iterator == null) {
56 throw new IllegalArgumentException("Iterator must not be null");
57 }
58 if (iterator instanceof Unmodifiable) {
59 return iterator;
60 }
61 return new UnmodifiableIterator<E>(iterator);
62 }
63
64
65
66
67 /***
68 * Create a new <code>UnmodifiableIterator</code> that decorates the
69 * provided iterator.
70 *
71 * @param iterator the iterator to decorate
72 */
73 private UnmodifiableIterator(final Iterator<E> iterator) {
74 super();
75 this.iterator = iterator;
76 }
77
78
79
80
81 public boolean hasNext() {
82 return iterator.hasNext();
83 }
84
85 public E next() {
86 return iterator.next();
87 }
88
89 public void remove() {
90 throw new UnsupportedOperationException("remove() is not supported");
91 }
92
93 }