1// GenericsNote: Converted. 2/* 3 * Copyright 2003-2004 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package org.jivesoftware.smack.util.collections; 18 19import java.util.Iterator; 20 21/** 22 * Defines an iterator that operates over a <code>Map</code>. 23 * <p/> 24 * This iterator is a special version designed for maps. It can be more 25 * efficient to use this rather than an entry set iterator where the option 26 * is available, and it is certainly more convenient. 27 * <p/> 28 * A map that provides this interface may not hold the data internally using 29 * Map Entry objects, thus this interface can avoid lots of object creation. 30 * <p/> 31 * In use, this iterator iterates through the keys in the map. After each call 32 * to <code>next()</code>, the <code>getValue()</code> method provides direct 33 * access to the value. The value can also be set using <code>setValue()</code>. 34 * <pre> 35 * MapIterator it = map.mapIterator(); 36 * while (it.hasNext()) { 37 * Object key = it.next(); 38 * Object value = it.getValue(); 39 * it.setValue(newValue); 40 * } 41 * </pre> 42 * 43 * @author Matt Hall, John Watkinson, Stephen Colebourne 44 * @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:19 $ 45 * @since Commons Collections 3.0 46 */ 47public interface MapIterator <K,V> extends Iterator<K> { 48 49 /** 50 * Checks to see if there are more entries still to be iterated. 51 * 52 * @return <code>true</code> if the iterator has more elements 53 */ 54 boolean hasNext(); 55 56 /** 57 * Gets the next <em>key</em> from the <code>Map</code>. 58 * 59 * @return the next key in the iteration 60 * @throws java.util.NoSuchElementException 61 * if the iteration is finished 62 */ 63 K next(); 64 65 //----------------------------------------------------------------------- 66 /** 67 * Gets the current key, which is the key returned by the last call 68 * to <code>next()</code>. 69 * 70 * @return the current key 71 * @throws IllegalStateException if <code>next()</code> has not yet been called 72 */ 73 K getKey(); 74 75 /** 76 * Gets the current value, which is the value associated with the last key 77 * returned by <code>next()</code>. 78 * 79 * @return the current value 80 * @throws IllegalStateException if <code>next()</code> has not yet been called 81 */ 82 V getValue(); 83 84 //----------------------------------------------------------------------- 85 /** 86 * Removes the last returned key from the underlying <code>Map</code> (optional operation). 87 * <p/> 88 * This method can be called once per call to <code>next()</code>. 89 * 90 * @throws UnsupportedOperationException if remove is not supported by the map 91 * @throws IllegalStateException if <code>next()</code> has not yet been called 92 * @throws IllegalStateException if <code>remove()</code> has already been called 93 * since the last call to <code>next()</code> 94 */ 95 void remove(); 96 97 /** 98 * Sets the value associated with the current key (optional operation). 99 * 100 * @param value the new value 101 * @return the previous value 102 * @throws UnsupportedOperationException if setValue is not supported by the map 103 * @throws IllegalStateException if <code>next()</code> has not yet been called 104 * @throws IllegalStateException if <code>remove()</code> has been called since the 105 * last call to <code>next()</code> 106 */ 107 V setValue(V value); 108 109} 110