KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 /**
23  * Adapter to make {@link Enumeration Enumeration} instances appear
24  * to be {@link Iterator Iterator} instances.
25  *
26  * @since Commons Collections 1.0
27  * @version $Revision: 1.8 $ $Date: 2004/04/21 20:34:12 $
28  *
29  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
30  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
31  */

32 public class EnumerationIterator implements Iterator JavaDoc {
33     
34     /** The collection to remove elements from */
35     private Collection JavaDoc collection;
36     /** The enumeration being converted */
37     private Enumeration JavaDoc enumeration;
38     /** The last object retrieved */
39     private Object JavaDoc last;
40     
41     // Constructors
42
//-----------------------------------------------------------------------
43
/**
44      * Constructs a new <code>EnumerationIterator</code> that will not
45      * function until {@link #setEnumeration(Enumeration)} is called.
46      */

47     public EnumerationIterator() {
48         this(null, null);
49     }
50
51     /**
52      * Constructs a new <code>EnumerationIterator</code> that provides
53      * an iterator view of the given enumeration.
54      *
55      * @param enumeration the enumeration to use
56      */

57     public EnumerationIterator(final Enumeration JavaDoc enumeration) {
58         this(enumeration, null);
59     }
60
61     /**
62      * Constructs a new <code>EnumerationIterator</code> that will remove
63      * elements from the specified collection.
64      *
65      * @param enumeration the enumeration to use
66      * @param collection the collection to remove elements form
67      */

68     public EnumerationIterator(final Enumeration JavaDoc enumeration, final Collection JavaDoc collection) {
69         super();
70         this.enumeration = enumeration;
71         this.collection = collection;
72         this.last = null;
73     }
74
75     // Iterator interface
76
//-----------------------------------------------------------------------
77
/**
78      * Returns true if the underlying enumeration has more elements.
79      *
80      * @return true if the underlying enumeration has more elements
81      * @throws NullPointerException if the underlying enumeration is null
82      */

83     public boolean hasNext() {
84         return enumeration.hasMoreElements();
85     }
86
87     /**
88      * Returns the next object from the enumeration.
89      *
90      * @return the next object from the enumeration
91      * @throws NullPointerException if the enumeration is null
92      */

93     public Object JavaDoc next() {
94         last = enumeration.nextElement();
95         return last;
96     }
97
98     /**
99      * Removes the last retrieved element if a collection is attached.
100      * <p>
101      * Functions if an associated <code>Collection</code> is known.
102      * If so, the first occurrence of the last returned object from this
103      * iterator will be removed from the collection.
104      *
105      * @exception IllegalStateException <code>next()</code> not called.
106      * @exception UnsupportedOperationException if no associated collection
107      */

108     public void remove() {
109         if (collection != null) {
110             if (last != null) {
111                 collection.remove(last);
112             } else {
113                 throw new IllegalStateException JavaDoc("next() must have been called for remove() to function");
114             }
115         } else {
116             throw new UnsupportedOperationException JavaDoc("No Collection associated with this Iterator");
117         }
118     }
119
120     // Properties
121
//-----------------------------------------------------------------------
122
/**
123      * Returns the underlying enumeration.
124      *
125      * @return the underlying enumeration
126      */

127     public Enumeration JavaDoc getEnumeration() {
128         return enumeration;
129     }
130
131     /**
132      * Sets the underlying enumeration.
133      *
134      * @param enumeration the new underlying enumeration
135      */

136     public void setEnumeration(final Enumeration JavaDoc enumeration) {
137         this.enumeration = enumeration;
138     }
139     
140 }
141
Popular Tags