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.Iterator;
  18. import java.util.NoSuchElementException;
  19. import org.apache.commons.collections.ResettableIterator;
  20. /**
  21. * <code>SingletonIterator</code> is an {@link Iterator} over a single
  22. * object instance.
  23. *
  24. * @since Commons Collections 2.0
  25. * @version $Revision: 1.14 $ $Date: 2004/04/09 22:52:48 $
  26. *
  27. * @author James Strachan
  28. * @author Stephen Colebourne
  29. * @author Rodney Waldhoff
  30. */
  31. public class SingletonIterator
  32. implements Iterator, ResettableIterator {
  33. /** Whether remove is allowed */
  34. private final boolean removeAllowed;
  35. /** Is the cursor before the first element */
  36. private boolean beforeFirst = true;
  37. /** Has the element been removed */
  38. private boolean removed = false;
  39. /** The object */
  40. private Object object;
  41. /**
  42. * Constructs a new <code>SingletonIterator</code> where <code>remove</code>
  43. * is a permitted operation.
  44. *
  45. * @param object the single object to return from the iterator
  46. */
  47. public SingletonIterator(Object object) {
  48. this(object, true);
  49. }
  50. /**
  51. * Constructs a new <code>SingletonIterator</code> optionally choosing if
  52. * <code>remove</code> is a permitted operation.
  53. *
  54. * @param object the single object to return from the iterator
  55. * @param removeAllowed true if remove is allowed
  56. * @since Commons Collections 3.1
  57. */
  58. public SingletonIterator(Object object, boolean removeAllowed) {
  59. super();
  60. this.object = object;
  61. this.removeAllowed = removeAllowed;
  62. }
  63. //-----------------------------------------------------------------------
  64. /**
  65. * Is another object available from the iterator?
  66. * <p>
  67. * This returns true if the single object hasn't been returned yet.
  68. *
  69. * @return true if the single object hasn't been returned yet
  70. */
  71. public boolean hasNext() {
  72. return (beforeFirst && !removed);
  73. }
  74. /**
  75. * Get the next object from the iterator.
  76. * <p>
  77. * This returns the single object if it hasn't been returned yet.
  78. *
  79. * @return the single object
  80. * @throws NoSuchElementException if the single object has already
  81. * been returned
  82. */
  83. public Object next() {
  84. if (!beforeFirst || removed) {
  85. throw new NoSuchElementException();
  86. }
  87. beforeFirst = false;
  88. return object;
  89. }
  90. /**
  91. * Remove the object from this iterator.
  92. *
  93. * @throws IllegalStateException if the <tt>next</tt> method has not
  94. * yet been called, or the <tt>remove</tt> method has already
  95. * been called after the last call to the <tt>next</tt>
  96. * method.
  97. * @throws UnsupportedOperationException if remove is not supported
  98. */
  99. public void remove() {
  100. if (removeAllowed) {
  101. if (removed || beforeFirst) {
  102. throw new IllegalStateException();
  103. } else {
  104. object = null;
  105. removed = true;
  106. }
  107. } else {
  108. throw new UnsupportedOperationException();
  109. }
  110. }
  111. /**
  112. * Reset the iterator to the start.
  113. */
  114. public void reset() {
  115. beforeFirst = true;
  116. }
  117. }