001: // ============================================================================
002: // $Id: SingletonIterator.java,v 1.9 2005/08/02 23:45:22 davidahall Exp $
003: // Copyright (c) 2003-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032: package net.sf.jga.util;
033:
034: import java.util.ListIterator;
035: import java.util.NoSuchElementException;
036: import java.util.Iterator;
037:
038: /**
039: * Iterates over a single item. The iterator is structured as a list iterator,
040: * but the list is a fixed size (1). The value may be changed after it has
041: * been retrieved at least one time.
042: *
043: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
044: */
045:
046: public class SingletonIterator<T> implements ListIterator<T>,
047: Iterable<T> {
048: // The value returned by the iterator
049: private T _value;
050:
051: // Flag indicating that the 'pointer' is set to the beginning of the
052: // imaginary list.
053: private boolean _beforeValue = true;
054:
055: // Flag indicating if the current value can be changed.
056: private boolean _canSetValue = false;
057:
058: /**
059: * Builds a SingletonIterator that will return the given value.
060: */
061: public SingletonIterator(T value) {
062: _value = value;
063: }
064:
065: // - - - - - - - - - - -
066: // Iterable<T> interface
067: // - - - - - - - - - - -
068:
069: public Iterator<T> iterator() {
070: return this ;
071: }
072:
073: // - - - - - - - - - - - - -
074: // ListIterator<T> interface
075: // - - - - - - - - - - - - -
076:
077: /**
078: * Returns true if the item has not yet been returned.
079: * @return true if the item has not yet been returned
080: */
081: public boolean hasNext() {
082: return _beforeValue;
083: }
084:
085: /**
086: * Returns the single item
087: * @return the single item
088: * @throws NoSuchElementException if the item has already been returned
089: */
090: public T next() {
091: if (!_beforeValue)
092: throw new NoSuchElementException();
093:
094: _beforeValue = false;
095: _canSetValue = true;
096: return _value;
097: }
098:
099: /**
100: * Returns true if the item has not yet been returned.
101: * @return true if the item has not yet been returned
102: */
103: public boolean hasPrevious() {
104: return !_beforeValue;
105: }
106:
107: /**
108: * Returns the single item
109: * @return the single item
110: * @throws NoSuchElementException if the item has already been returned
111: */
112: public T previous() {
113: if (_beforeValue)
114: throw new NoSuchElementException();
115:
116: _beforeValue = true;
117: _canSetValue = true;
118: return _value;
119: }
120:
121: public int nextIndex() {
122: return _beforeValue ? 0 : 1;
123: }
124:
125: public int previousIndex() {
126: return _beforeValue ? -1 : 0;
127: }
128:
129: public void add(T arg) {
130: throw new UnsupportedOperationException();
131: }
132:
133: public void set(T value) {
134: if (!_canSetValue)
135: throw new IllegalStateException();
136:
137: _value = value;
138: _canSetValue = false;
139: }
140:
141: public void remove() {
142: throw new UnsupportedOperationException();
143: }
144: }
|