~ubuntu-branches/ubuntu/oneiric/libcommons-collections-java/oneiric

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/collections/iterators/EnumerationIterator.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2004-08-07 00:02:50 UTC
  • Revision ID: james.westby@ubuntu.com-20040807000250-hcnqvrdpxg95nmzr
Tags: upstream-2.1.1
ImportĀ upstreamĀ versionĀ 2.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
19
import java.util.Enumeration;
 
20
import java.util.Iterator;
 
21
 
 
22
/** Adapter to make {@link Enumeration Enumeration} instances appear
 
23
  * to be {@link Iterator Iterator} instances.
 
24
  *
 
25
  * @since 1.0
 
26
  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 
27
  * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
 
28
  */
 
29
public class EnumerationIterator implements Iterator {
 
30
    
 
31
    private Collection collection;
 
32
 
 
33
    private Enumeration enumeration;
 
34
 
 
35
    private Object last;
 
36
    
 
37
    /**
 
38
     *  Constructs a new <Code>EnumerationIterator</Code> that will not
 
39
     *  function until {@link #setEnumeration(Enumeration)} is called.
 
40
     */
 
41
    public EnumerationIterator() {
 
42
        this(null, null);
 
43
    }
 
44
 
 
45
    /**
 
46
     *  Constructs a new <Code>EnumerationIterator</Code> that provides
 
47
     *  an iterator view of the given enumeration.
 
48
     *
 
49
     *  @param enumeration  the enumeration to use
 
50
     */
 
51
    public EnumerationIterator( Enumeration enumeration ) {
 
52
        this(enumeration, null);
 
53
    }
 
54
 
 
55
    /**
 
56
     *  Constructs a new <Code>EnumerationIterator</Code> that will remove
 
57
     *  elements from the specified collection.
 
58
     *
 
59
     *  @param enumeration  the enumeration to use
 
60
     *  @param collection  the collection to remove elements from
 
61
     */
 
62
    public EnumerationIterator( Enumeration enumeration, Collection collection ) {
 
63
        this.enumeration = enumeration;
 
64
        this.collection = collection;
 
65
        this.last = null;
 
66
    }
 
67
 
 
68
    // Iterator interface
 
69
    //-------------------------------------------------------------------------
 
70
 
 
71
    /**
 
72
     *  Returns true if the underlying enumeration has more elements.
 
73
     *
 
74
     *  @return true if the underlying enumeration has more elements
 
75
     *  @throws NullPointerException  if the underlying enumeration is null
 
76
     */
 
77
    public boolean hasNext() {
 
78
        return enumeration.hasMoreElements();
 
79
    }
 
80
 
 
81
    /**
 
82
     *  Returns the next object from the enumeration.
 
83
     *
 
84
     *  @return the next object from the enumeration
 
85
     *  @throws NullPointerException if the enumeration is null
 
86
     */
 
87
    public Object next() {
 
88
        last = enumeration.nextElement();
 
89
        return last;
 
90
    }
 
91
 
 
92
    /**
 
93
     * Functions if an associated <code>Collection</code> is known.
 
94
     * If so, the first occurrence of the last returned object from this
 
95
     * iterator will be removed from the collection.
 
96
     *
 
97
     * @exception IllegalStateException <code>next()</code> not called.
 
98
     * @exception UnsupportedOperationException No associated
 
99
     * <code>Collection</code>.
 
100
     */
 
101
    public void remove() {
 
102
        if (collection != null) {
 
103
            if (last != null) {
 
104
                collection.remove(last);
 
105
            }
 
106
            else {
 
107
                throw new IllegalStateException
 
108
                    ("next() must have been called for remove() to function");
 
109
            }
 
110
        }
 
111
        else {
 
112
            throw new UnsupportedOperationException
 
113
                ("No Collection associated with this Iterator");
 
114
        }
 
115
    }
 
116
 
 
117
    // Properties
 
118
    //-------------------------------------------------------------------------
 
119
 
 
120
    /**
 
121
     *  Returns the underlying enumeration.
 
122
     *
 
123
     *  @return the underlying enumeration
 
124
     */
 
125
    public Enumeration getEnumeration() {
 
126
        return enumeration;
 
127
    }
 
128
 
 
129
    /**
 
130
     *  Sets the underlying enumeration.
 
131
     *
 
132
     *  @param enumeration  the new underlying enumeration
 
133
     */
 
134
    public void setEnumeration( Enumeration enumeration ) {
 
135
        this.enumeration = enumeration;
 
136
    }
 
137
}