1 /* 2 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. 3 * 4 * This software is open source. 5 * See the bottom of this file for the licence. 6 * 7 * $Id: FilterIterator.java,v 1.8 2004/06/25 08:03:41 maartenc Exp $ 8 */ 9 10 package org.dom4j.tree; 11 12 import java.util.Iterator; 13 import java.util.NoSuchElementException; 14 15 /*** <p><code>FilterIterator</code> is an abstract base class which is useful 16 * for implementors of {@link Iterator} which filter an existing iterator. 17 * 18 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 19 * @version $Revision: 1.8 $ 20 * @deprecated THIS CLASS WILL BE REMOVED IN dom4j-1.6 !! 21 */ 22 public abstract class FilterIterator implements Iterator { 23 24 protected Iterator proxy; 25 private Object next; 26 private boolean first = true; 27 28 public FilterIterator(Iterator proxy) { 29 this.proxy = proxy; 30 } 31 32 33 public boolean hasNext() { 34 if ( first ) { 35 next = findNext(); 36 first = false; 37 } 38 return next != null; 39 } 40 41 public Object next() throws NoSuchElementException { 42 if ( ! hasNext() ) { 43 throw new NoSuchElementException(); 44 } 45 Object answer = this.next; 46 this.next = findNext(); 47 return answer; 48 } 49 50 /*** 51 * Always throws UnsupportedOperationException as this class 52 * does look-ahead with its internal iterator. 53 * 54 * @throws UnsupportedOperationException always 55 */ 56 public void remove() { 57 throw new UnsupportedOperationException(); 58 } 59 60 /*** Filter method to perform some matching on the given element. 61 * 62 * @return true if the given element matches the filter 63 * and should be appear in the iteration 64 */ 65 protected abstract boolean matches(Object element); 66 67 68 protected Object findNext() { 69 if ( proxy != null ) { 70 while (proxy.hasNext()) { 71 Object next = proxy.next(); 72 if ( next != null && matches(next) ) { 73 return next; 74 } 75 } 76 proxy = null; 77 } 78 return null; 79 } 80 } 81 82 83 84 85 /* 86 * Redistribution and use of this software and associated documentation 87 * ("Software"), with or without modification, are permitted provided 88 * that the following conditions are met: 89 * 90 * 1. Redistributions of source code must retain copyright 91 * statements and notices. Redistributions must also contain a 92 * copy of this document. 93 * 94 * 2. Redistributions in binary form must reproduce the 95 * above copyright notice, this list of conditions and the 96 * following disclaimer in the documentation and/or other 97 * materials provided with the distribution. 98 * 99 * 3. The name "DOM4J" must not be used to endorse or promote 100 * products derived from this Software without prior written 101 * permission of MetaStuff, Ltd. For written permission, 102 * please contact dom4j-info@metastuff.com. 103 * 104 * 4. Products derived from this Software may not be called "DOM4J" 105 * nor may "DOM4J" appear in their names without prior written 106 * permission of MetaStuff, Ltd. DOM4J is a registered 107 * trademark of MetaStuff, Ltd. 108 * 109 * 5. Due credit should be given to the DOM4J Project - 110 * http://www.dom4j.org 111 * 112 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 113 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 114 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 115 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 116 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 117 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 118 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 119 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 120 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 121 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 122 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 123 * OF THE POSSIBILITY OF SUCH DAMAGE. 124 * 125 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. 126 * 127 * $Id: FilterIterator.java,v 1.8 2004/06/25 08:03:41 maartenc Exp $ 128 */