View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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   */
17  package org.apache.commons.collections;
18  
19  import 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<String,Integer> it = map.mapIterator();
36   * while (it.hasNext()) {
37   *   String key = it.next();
38   *   Integer value = it.getValue();
39   *   it.setValue(value + 1);
40   * }
41   * </pre>
42   *
43   * @param <K> the type of the keys in the map
44   * @param <V> the type of the values in the map
45   * @since 3.0
46   * @version $Id: MapIterator.java 1361710 2012-07-15 15:00:21Z tn $
47   */
48  public interface MapIterator<K, V> extends Iterator<K> {
49  
50      /**
51       * Checks to see if there are more entries still to be iterated.
52       *
53       * @return <code>true</code> if the iterator has more elements
54       */
55      boolean hasNext();
56  
57      /**
58       * Gets the next <em>key</em> from the <code>Map</code>.
59       *
60       * @return the next key in the iteration
61       * @throws java.util.NoSuchElementException 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 }