01: /*
02: * Copyright 1999-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
27: *
28: * @author Stephen Colebourne
29: */
30: public final class UnmodifiableIterator implements Iterator,
31: Unmodifiable {
32:
33: /** The iterator being decorated */
34: private Iterator iterator;
35:
36: //-----------------------------------------------------------------------
37: /**
38: * Decorates the specified iterator such that it cannot be modified.
39: * <p>
40: * If the iterator is already unmodifiable it is returned directly.
41: *
42: * @param iterator the iterator to decorate
43: * @throws IllegalArgumentException if the iterator is null
44: */
45: public static Iterator decorate(Iterator iterator) {
46: if (iterator == null) {
47: throw new IllegalArgumentException(
48: "Iterator must not be null");
49: }
50: if (iterator instanceof Unmodifiable) {
51: return iterator;
52: }
53: return new UnmodifiableIterator(iterator);
54: }
55:
56: //-----------------------------------------------------------------------
57: /**
58: * Constructor.
59: *
60: * @param iterator the iterator to decorate
61: */
62: private UnmodifiableIterator(Iterator iterator) {
63: super ();
64: this .iterator = iterator;
65: }
66:
67: //-----------------------------------------------------------------------
68: public boolean hasNext() {
69: return iterator.hasNext();
70: }
71:
72: public Object next() {
73: return iterator.next();
74: }
75:
76: public void remove() {
77: throw new UnsupportedOperationException(
78: "remove() is not supported");
79: }
80:
81: }
|