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 org.apache.commons.collections;
  17. import java.util.Iterator;
  18. /**
  19. * Defines an iterator that operates over a <code>Map</code>.
  20. * <p>
  21. * This iterator is a special version designed for maps. It can be more
  22. * efficient to use this rather than an entry set iterator where the option
  23. * is available, and it is certainly more convenient.
  24. * <p>
  25. * A map that provides this interface may not hold the data internally using
  26. * Map Entry objects, thus this interface can avoid lots of object creation.
  27. * <p>
  28. * In use, this iterator iterates through the keys in the map. After each call
  29. * to <code>next()</code>, the <code>getValue()</code> method provides direct
  30. * access to the value. The value can also be set using <code>setValue()</code>.
  31. * <pre>
  32. * MapIterator it = map.mapIterator();
  33. * while (it.hasNext()) {
  34. * Object key = it.next();
  35. * Object value = it.getValue();
  36. * it.setValue(newValue);
  37. * }
  38. * </pre>
  39. *
  40. * @since Commons Collections 3.0
  41. * @version $Revision: 1.7 $ $Date: 2004/02/18 01:15:42 $
  42. *
  43. * @author Stephen Colebourne
  44. */
  45. public interface MapIterator extends Iterator {
  46. /**
  47. * Checks to see if there are more entries still to be iterated.
  48. *
  49. * @return <code>true</code> if the iterator has more elements
  50. */
  51. boolean hasNext();
  52. /**
  53. * Gets the next <em>key</em> from the <code>Map</code>.
  54. *
  55. * @return the next key in the iteration
  56. * @throws java.util.NoSuchElementException if the iteration is finished
  57. */
  58. Object next();
  59. //-----------------------------------------------------------------------
  60. /**
  61. * Gets the current key, which is the key returned by the last call
  62. * to <code>next()</code>.
  63. *
  64. * @return the current key
  65. * @throws IllegalStateException if <code>next()</code> has not yet been called
  66. */
  67. Object getKey();
  68. /**
  69. * Gets the current value, which is the value associated with the last key
  70. * returned by <code>next()</code>.
  71. *
  72. * @return the current value
  73. * @throws IllegalStateException if <code>next()</code> has not yet been called
  74. */
  75. Object getValue();
  76. //-----------------------------------------------------------------------
  77. /**
  78. * Removes the last returned key from the underlying <code>Map</code> (optional operation).
  79. * <p>
  80. * This method can be called once per call to <code>next()</code>.
  81. *
  82. * @throws UnsupportedOperationException if remove is not supported by the map
  83. * @throws IllegalStateException if <code>next()</code> has not yet been called
  84. * @throws IllegalStateException if <code>remove()</code> has already been called
  85. * since the last call to <code>next()</code>
  86. */
  87. void remove();
  88. /**
  89. * Sets the value associated with the current key (optional operation).
  90. *
  91. * @param value the new value
  92. * @return the previous value
  93. * @throws UnsupportedOperationException if setValue is not supported by the map
  94. * @throws IllegalStateException if <code>next()</code> has not yet been called
  95. * @throws IllegalStateException if <code>remove()</code> has been called since the
  96. * last call to <code>next()</code>
  97. */
  98. Object setValue(Object value);
  99. }