001/*
002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
003 * 
004 * This software is open source. 
005 * See the bottom of this file for the licence.
006 * 
007 * $Id: FilterIterator.java,v 1.3 2001/08/01 09:17:21 jstrachan Exp $
008 */
009
010package org.dom4j.tree;
011
012import java.util.Iterator;
013import java.util.NoSuchElementException;
014
015/** <p><code>FilterIterator</code> is an abstract base class which is useful
016  * for implementors of {@link Iterator} which filter an existing iterator.
017  *
018  * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
019  * @version $Revision: 1.3 $
020  */
021public abstract class FilterIterator implements Iterator {
022    
023    protected Iterator proxy;
024    private Object next;
025    private boolean first = true;
026    
027    public FilterIterator(Iterator proxy) {
028        this.proxy = proxy;
029    }
030
031
032    public boolean hasNext() {
033        if ( first ) {
034            next = findNext();
035            first = false;
036        }
037        return next != null;
038    }
039
040    public Object next() throws NoSuchElementException {
041        if ( ! hasNext() ) {
042            throw new NoSuchElementException();
043        }
044        Object answer = this.next;
045        this.next = findNext();
046        return answer;
047    }
048
049    public void remove() {
050        if (proxy != null) {
051            proxy.remove();
052        }
053    }
054    
055    /** Filter method to perform some matching on the given element.
056      * 
057      * @return true if the given element matches the filter
058      * and should be appear in the iteration
059      */
060    protected abstract boolean matches(Object element);
061    
062    
063    protected Object findNext() {
064        if ( proxy != null ) {
065            while (proxy.hasNext()) {
066                Object next = proxy.next();
067                if ( next != null &&  matches(next) ) {
068                    return next;
069                }
070            }
071            proxy = null;
072        }
073        return null;
074    }
075}
076
077
078
079
080/*
081 * Redistribution and use of this software and associated documentation
082 * ("Software"), with or without modification, are permitted provided
083 * that the following conditions are met:
084 *
085 * 1. Redistributions of source code must retain copyright
086 *    statements and notices.  Redistributions must also contain a
087 *    copy of this document.
088 *
089 * 2. Redistributions in binary form must reproduce the
090 *    above copyright notice, this list of conditions and the
091 *    following disclaimer in the documentation and/or other
092 *    materials provided with the distribution.
093 *
094 * 3. The name "DOM4J" must not be used to endorse or promote
095 *    products derived from this Software without prior written
096 *    permission of MetaStuff, Ltd.  For written permission,
097 *    please contact dom4j-info@metastuff.com.
098 *
099 * 4. Products derived from this Software may not be called "DOM4J"
100 *    nor may "DOM4J" appear in their names without prior written
101 *    permission of MetaStuff, Ltd. DOM4J is a registered
102 *    trademark of MetaStuff, Ltd.
103 *
104 * 5. Due credit should be given to the DOM4J Project
105 *    (http://dom4j.org/).
106 *
107 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
108 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
109 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
110 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
111 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
112 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
113 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
114 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
115 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
116 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
117 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
118 * OF THE POSSIBILITY OF SUCH DAMAGE.
119 *
120 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
121 *
122 * $Id: FilterIterator.java,v 1.3 2001/08/01 09:17:21 jstrachan Exp $
123 */