001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.iterators;
017:
018: import java.util.Iterator;
019: import java.util.NoSuchElementException;
020:
021: import org.apache.commons.collections.ResettableIterator;
022:
023: /**
024: * <code>SingletonIterator</code> is an {@link Iterator} over a single
025: * object instance.
026: *
027: * @since Commons Collections 2.0
028: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
029: *
030: * @author James Strachan
031: * @author Stephen Colebourne
032: * @author Rodney Waldhoff
033: */
034: public class SingletonIterator implements Iterator, ResettableIterator {
035:
036: /** Whether remove is allowed */
037: private final boolean removeAllowed;
038: /** Is the cursor before the first element */
039: private boolean beforeFirst = true;
040: /** Has the element been removed */
041: private boolean removed = false;
042: /** The object */
043: private Object object;
044:
045: /**
046: * Constructs a new <code>SingletonIterator</code> where <code>remove</code>
047: * is a permitted operation.
048: *
049: * @param object the single object to return from the iterator
050: */
051: public SingletonIterator(Object object) {
052: this (object, true);
053: }
054:
055: /**
056: * Constructs a new <code>SingletonIterator</code> optionally choosing if
057: * <code>remove</code> is a permitted operation.
058: *
059: * @param object the single object to return from the iterator
060: * @param removeAllowed true if remove is allowed
061: * @since Commons Collections 3.1
062: */
063: public SingletonIterator(Object object, boolean removeAllowed) {
064: super ();
065: this .object = object;
066: this .removeAllowed = removeAllowed;
067: }
068:
069: //-----------------------------------------------------------------------
070: /**
071: * Is another object available from the iterator?
072: * <p>
073: * This returns true if the single object hasn't been returned yet.
074: *
075: * @return true if the single object hasn't been returned yet
076: */
077: public boolean hasNext() {
078: return (beforeFirst && !removed);
079: }
080:
081: /**
082: * Get the next object from the iterator.
083: * <p>
084: * This returns the single object if it hasn't been returned yet.
085: *
086: * @return the single object
087: * @throws NoSuchElementException if the single object has already
088: * been returned
089: */
090: public Object next() {
091: if (!beforeFirst || removed) {
092: throw new NoSuchElementException();
093: }
094: beforeFirst = false;
095: return object;
096: }
097:
098: /**
099: * Remove the object from this iterator.
100: *
101: * @throws IllegalStateException if the <tt>next</tt> method has not
102: * yet been called, or the <tt>remove</tt> method has already
103: * been called after the last call to the <tt>next</tt>
104: * method.
105: * @throws UnsupportedOperationException if remove is not supported
106: */
107: public void remove() {
108: if (removeAllowed) {
109: if (removed || beforeFirst) {
110: throw new IllegalStateException();
111: } else {
112: object = null;
113: removed = true;
114: }
115: } else {
116: throw new UnsupportedOperationException();
117: }
118: }
119:
120: /**
121: * Reset the iterator to the start.
122: */
123: public void reset() {
124: beforeFirst = true;
125: }
126:
127: }
|