001: // ============================================================================
002: // $Id: FindAllIterator.java,v 1.6 2006/10/19 01:43:44 davidahall Exp $
003: // Copyright (c) 2004-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:
033: package net.sf.jga.util;
034:
035: import java.util.Iterator;
036: import java.util.NoSuchElementException;
037: import net.sf.jga.fn.UnaryFunctor;
038:
039: /**
040: * Iterator that applies one of the FindX functors as many times as possible.
041: * Formally, this iterator uses a functor that takes one iterator and returns
042: * an iterator, and repeatedly applies it to a given iterator until the result
043: * iterator's <code>hasNext()</code> method is false.
044: * <p>
045: * Copyright © 2004-2005 David A. Hall
046: *
047: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
048: **/
049:
050: public class FindAllIterator<T> implements
051: Iterable<Iterator<? extends T>>,
052: Iterator<Iterator<? extends T>> {
053:
054: // the base iterator
055: private Iterator<? extends T> _iter;
056:
057: // the functor
058: private UnaryFunctor<Iterator<? extends T>, ? extends Iterator<T>> _fn;
059:
060: // three state flag indicating that the test was done and its results
061: private Boolean _tested;
062:
063: /**
064: * Builds a FindAllIterator that will apply the given functor to the given
065: * iterator.
066: */
067: public FindAllIterator(
068: Iterator<? extends T> it,
069: UnaryFunctor<Iterator<? extends T>, ? extends Iterator<T>> fn) {
070: _iter = it;
071: _fn = fn;
072: }
073:
074: // - - - - - - - - - - -
075: // Iterable<T> interface
076: // - - - - - - - - - - -
077:
078: public Iterator<Iterator<? extends T>> iterator() {
079: return this ;
080: }
081:
082: // - - - - - - - - - - -
083: // Iterator<T> interface
084: // - - - - - - - - - - -
085:
086: public boolean hasNext() {
087: _iter = _fn.fn(_iter);
088: _tested = Boolean.valueOf(_iter.hasNext());
089: return _tested;
090: }
091:
092: public Iterator<? extends T> next() {
093: if (_tested == null)
094: hasNext();
095:
096: if (!_tested)
097: throw new NoSuchElementException();
098:
099: _tested = null;
100: return /*(Iterator<T>)*/_iter;
101: }
102:
103: public void remove() {
104: _iter.remove();
105: }
106: }
|