KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > iterators > UnmodifiableIterator


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 org.apache.commons.collections.iterators;
17
18 import java.util.Iterator JavaDoc;
19
20 import org.apache.commons.collections.Unmodifiable;
21
22 /**
23  * Decorates an iterator such that it cannot be modified.
24  *
25  * @since Commons Collections 3.0
26  * @version $Revision: 1.5 $ $Date: 2004/02/18 00:59:50 $
27  *
28  * @author Stephen Colebourne
29  */

30 public final class UnmodifiableIterator implements Iterator JavaDoc, Unmodifiable {
31
32     /** The iterator being decorated */
33     private Iterator JavaDoc iterator;
34
35     //-----------------------------------------------------------------------
36
/**
37      * Decorates the specified iterator such that it cannot be modified.
38      * <p>
39      * If the iterator is already unmodifiable it is returned directly.
40      *
41      * @param iterator the iterator to decorate
42      * @throws IllegalArgumentException if the iterator is null
43      */

44     public static Iterator JavaDoc decorate(Iterator JavaDoc iterator) {
45         if (iterator == null) {
46             throw new IllegalArgumentException JavaDoc("Iterator must not be null");
47         }
48         if (iterator instanceof Unmodifiable) {
49             return iterator;
50         }
51         return new UnmodifiableIterator(iterator);
52     }
53     
54     //-----------------------------------------------------------------------
55
/**
56      * Constructor.
57      *
58      * @param iterator the iterator to decorate
59      */

60     private UnmodifiableIterator(Iterator JavaDoc iterator) {
61         super();
62         this.iterator = iterator;
63     }
64
65     //-----------------------------------------------------------------------
66
public boolean hasNext() {
67         return iterator.hasNext();
68     }
69
70     public Object JavaDoc next() {
71         return iterator.next();
72     }
73
74     public void remove() {
75         throw new UnsupportedOperationException JavaDoc("remove() is not supported");
76     }
77
78 }
79
Popular Tags