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.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  	// Instance fields
37  	// -------------------------------------------------------------------------
38  	
39      /*** The iterator being decorated */
40      private Iterator<E> iterator;
41  
42      // Class fields
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      // Constructors
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      // Iterator interface methods.
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  }