View Javadoc

1   /*
2    *  Copyright 2003-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 net.sf.collections15;
17  
18  import java.util.Iterator;
19  
20  /***
21   * Defines an iterator that operates over a <code>Map</code>.
22   * <p/>
23   * This iterator is a special version designed for maps. It can be more
24   * efficient to use this rather than an entry set iterator where the option is
25   * available, and it is certainly more convenient.
26   * <p/>
27   * A map that provides this interface may not hold the data internally using Map
28   * Entry objects, thus this interface can avoid lots of object creation.
29   * <p/>
30   * In use, this iterator iterates through the keys in the map. After each call
31   * to <code>next()</code>, the <code>getValue()</code> method provides direct
32   * access to the value. The value can also be set using
33   * <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 Stephen Colebourne
44   * @version $Revision: 1.2 $ $Date: 2004/10/17 01:02:42 $
45   * @since Commons Collections15 1.0
46   */
47  public interface MapIterator <K, V> extends Iterator<K>
48  {
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       *
62       * @throws java.util.NoSuchElementException
63       *          if the iteration is finished
64       */
65      K next();
66  
67      //-----------------------------------------------------------------------
68      /***
69       * Gets the current key, which is the key returned by the last call to
70       * <code>next()</code>.
71       *
72       * @return the current key
73       *
74       * @throws IllegalStateException if <code>next()</code> has not yet been
75       *                               called
76       */
77      K getKey();
78  
79      /***
80       * Gets the current value, which is the value associated with the last key
81       * returned by <code>next()</code>.
82       *
83       * @return the current value
84       *
85       * @throws IllegalStateException if <code>next()</code> has not yet been
86       *                               called
87       */
88      V getValue();
89  
90      //-----------------------------------------------------------------------
91      /***
92       * Removes the last returned key from the underlying <code>Map</code>
93       * (optional operation).
94       * <p/>
95       * This method can be called once per call to <code>next()</code>.
96       *
97       * @throws UnsupportedOperationException if remove is not supported by the
98       *                                       map
99       * @throws IllegalStateException         if <code>next()</code> has not yet
100      *                                       been called
101      * @throws IllegalStateException         if <code>remove()</code> has
102      *                                       already been called since the last
103      *                                       call to <code>next()</code>
104      */
105     void remove();
106 
107     /***
108      * Sets the value associated with the current key (optional operation).
109      *
110      * @param value the new value
111      *
112      * @return the previous value
113      *
114      * @throws UnsupportedOperationException if setValue is not supported by the
115      *                                       map
116      * @throws IllegalStateException         if <code>next()</code> has not yet
117      *                                       been called
118      * @throws IllegalStateException         if <code>remove()</code> has been
119      *                                       called since the last call to
120      *                                       <code>next()</code>
121      */
122     V setValue(V value);
123 
124 }