View Javadoc

1   /*
2    *  Copyright 1999-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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  	// Instance fields
37  	// -------------------------------------------------------------------------
38  	
39      /*** The iterator being decorated */
40      private ListIterator<E> iterator;
41  
42      // Class methods
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      // Constructors
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      // ListIterator methods
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 }