1. /*
  2. * Copyright 1999-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. import java.util.Enumeration;
  18. import java.util.Iterator;
  19. /**
  20. * Adapter to make an {@link Iterator Iterator} instance appear to be
  21. * an {@link Enumeration Enumeration} instance.
  22. *
  23. * @since Commons Collections 1.0
  24. * @version $Revision: 1.9 $ $Date: 2004/05/03 10:23:38 $
  25. *
  26. * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  27. */
  28. public class IteratorEnumeration implements Enumeration {
  29. /** The iterator being decorated. */
  30. private Iterator iterator;
  31. /**
  32. * Constructs a new <code>IteratorEnumeration</code> that will not
  33. * function until {@link #setIterator(Iterator) setIterator} is
  34. * invoked.
  35. */
  36. public IteratorEnumeration() {
  37. super();
  38. }
  39. /**
  40. * Constructs a new <code>IteratorEnumeration</code> that will use
  41. * the given iterator.
  42. *
  43. * @param iterator the iterator to use
  44. */
  45. public IteratorEnumeration( Iterator iterator ) {
  46. super();
  47. this.iterator = iterator;
  48. }
  49. // Iterator interface
  50. //-------------------------------------------------------------------------
  51. /**
  52. * Returns true if the underlying iterator has more elements.
  53. *
  54. * @return true if the underlying iterator has more elements
  55. */
  56. public boolean hasMoreElements() {
  57. return iterator.hasNext();
  58. }
  59. /**
  60. * Returns the next element from the underlying iterator.
  61. *
  62. * @return the next element from the underlying iterator.
  63. * @throws java.util.NoSuchElementException if the underlying iterator has no
  64. * more elements
  65. */
  66. public Object nextElement() {
  67. return iterator.next();
  68. }
  69. // Properties
  70. //-------------------------------------------------------------------------
  71. /**
  72. * Returns the underlying iterator.
  73. *
  74. * @return the underlying iterator
  75. */
  76. public Iterator getIterator() {
  77. return iterator;
  78. }
  79. /**
  80. * Sets the underlying iterator.
  81. *
  82. * @param iterator the new underlying iterator
  83. */
  84. public void setIterator( Iterator iterator ) {
  85. this.iterator = iterator;
  86. }
  87. }