KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-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 import java.util.Map JavaDoc;
20
21 import org.apache.commons.collections.MapIterator;
22 import org.apache.commons.collections.ResettableIterator;
23
24 /**
25  * Implements a <code>MapIterator</code> using a Map entrySet.
26  * Reverse iteration is not supported.
27  * <pre>
28  * MapIterator it = map.mapIterator();
29  * while (it.hasNext()) {
30  * Object key = it.next();
31  * Object value = it.getValue();
32  * it.setValue(newValue);
33  * }
34  * </pre>
35  *
36  * @since Commons Collections 3.0
37  * @version $Revision: 1.7 $ $Date: 2004/02/18 00:59:50 $
38  *
39  * @author Stephen Colebourne
40  */

41 public class EntrySetMapIterator implements MapIterator, ResettableIterator {
42     
43     private final Map JavaDoc map;
44     private Iterator JavaDoc iterator;
45     private Map.Entry JavaDoc last;
46     private boolean canRemove = false;
47     
48     /**
49      * Constructor.
50      *
51      * @param map the map to iterate over
52      */

53     public EntrySetMapIterator(Map JavaDoc map) {
54         super();
55         this.map = map;
56         this.iterator = map.entrySet().iterator();
57     }
58
59     //-----------------------------------------------------------------------
60
/**
61      * Checks to see if there are more entries still to be iterated.
62      *
63      * @return <code>true</code> if the iterator has more elements
64      */

65     public boolean hasNext() {
66         return iterator.hasNext();
67     }
68
69     /**
70      * Gets the next <em>key</em> from the <code>Map</code>.
71      *
72      * @return the next key in the iteration
73      * @throws java.util.NoSuchElementException if the iteration is finished
74      */

75     public Object JavaDoc next() {
76         last = (Map.Entry JavaDoc) iterator.next();
77         canRemove = true;
78         return last.getKey();
79     }
80
81     //-----------------------------------------------------------------------
82
/**
83      * Removes the last returned key from the underlying <code>Map</code>.
84      * <p>
85      * This method can be called once per call to <code>next()</code>.
86      *
87      * @throws UnsupportedOperationException if remove is not supported by the map
88      * @throws IllegalStateException if <code>next()</code> has not yet been called
89      * @throws IllegalStateException if <code>remove()</code> has already been called
90      * since the last call to <code>next()</code>
91      */

92     public void remove() {
93         if (canRemove == false) {
94             throw new IllegalStateException JavaDoc("Iterator remove() can only be called once after next()");
95         }
96         iterator.remove();
97         last = null;
98         canRemove = false;
99     }
100     
101     //-----------------------------------------------------------------------
102
/**
103      * Gets the current key, which is the key returned by the last call
104      * to <code>next()</code>.
105      *
106      * @return the current key
107      * @throws IllegalStateException if <code>next()</code> has not yet been called
108      */

109     public Object JavaDoc getKey() {
110         if (last == null) {
111             throw new IllegalStateException JavaDoc("Iterator getKey() can only be called after next() and before remove()");
112         }
113         return last.getKey();
114     }
115
116     /**
117      * Gets the current value, which is the value associated with the last key
118      * returned by <code>next()</code>.
119      *
120      * @return the current value
121      * @throws IllegalStateException if <code>next()</code> has not yet been called
122      */

123     public Object JavaDoc getValue() {
124         if (last == null) {
125             throw new IllegalStateException JavaDoc("Iterator getValue() can only be called after next() and before remove()");
126         }
127         return last.getValue();
128     }
129
130     /**
131      * Sets the value associated with the current key.
132      *
133      * @param value the new value
134      * @return the previous value
135      * @throws UnsupportedOperationException if setValue is not supported by the map
136      * @throws IllegalStateException if <code>next()</code> has not yet been called
137      * @throws IllegalStateException if <code>remove()</code> has been called since the
138      * last call to <code>next()</code>
139      */

140     public Object JavaDoc setValue(Object JavaDoc value) {
141         if (last == null) {
142             throw new IllegalStateException JavaDoc("Iterator setValue() can only be called after next() and before remove()");
143         }
144         return last.setValue(value);
145     }
146
147     //-----------------------------------------------------------------------
148
/**
149      * Resets the state of the iterator.
150      */

151     public void reset() {
152         iterator = map.entrySet().iterator();
153         last = null;
154         canRemove = false;
155     }
156     
157     /**
158      * Gets the iterator as a String.
159      *
160      * @return a string version of the iterator
161      */

162     public String JavaDoc toString() {
163         if (last != null) {
164             return "MapIterator[" + getKey() + "=" + getValue() + "]";
165         } else {
166             return "MapIterator[]";
167         }
168     }
169     
170 }
171
Popular Tags