KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom4j > o3impl > FilterIterator


1 /*
2  * Copyright 2001 (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.2 2003/06/10 16:18:36 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
11
12 import java.util.Iterator JavaDoc;
13 import java.util.NoSuchElementException JavaDoc;
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.2 $
20  */

21 public abstract class FilterIterator implements Iterator JavaDoc {
22
23     protected Iterator JavaDoc proxy;
24     private Object JavaDoc next;
25     private boolean first = true;
26
27     public FilterIterator(Iterator JavaDoc proxy) {
28         this.proxy = proxy;
29     }
30
31
32     public boolean hasNext() {
33         if (first) {
34             next = findNext();
35             first = false;
36         }
37         return next != null;
38     }
39
40     public Object JavaDoc next() throws NoSuchElementException JavaDoc {
41         if (!hasNext()) {
42             throw new NoSuchElementException JavaDoc();
43         }
44         Object JavaDoc answer = this.next;
45         this.next = findNext();
46         return answer;
47     }
48
49     /**
50      * Always throws UnsupportedOperationException as this class
51      * does look-ahead with its internal iterator.
52      *
53      * @throws UnsupportedOperationException always
54      */

55     public void remove() {
56         throw new UnsupportedOperationException JavaDoc();
57     }
58
59     /** Filter method to perform some matching on the given element.
60      *
61      * @return true if the given element matches the filter
62      * and should be appear in the iteration
63      */

64     protected abstract boolean matches(Object JavaDoc element);
65
66
67     protected Object JavaDoc findNext() {
68         if (proxy != null) {
69             while (proxy.hasNext()) {
70                 Object JavaDoc next = proxy.next();
71                 if (next != null && matches(next)) {
72                     return next;
73                 }
74             }
75             proxy = null;
76         }
77         return null;
78     }
79 }
80
81
82 /*
83  * Redistribution and use of this software and associated documentation
84  * ("Software"), with or without modification, are permitted provided
85  * that the following conditions are met:
86  *
87  * 1. Redistributions of source code must retain copyright
88  * statements and notices. Redistributions must also contain a
89  * copy of this document.
90  *
91  * 2. Redistributions in binary form must reproduce the
92  * above copyright notice, this list of conditions and the
93  * following disclaimer in the documentation and/or other
94  * materials provided with the distribution.
95  *
96  * 3. The name "DOM4J" must not be used to endorse or promote
97  * products derived from this Software without prior written
98  * permission of MetaStuff, Ltd. For written permission,
99  * please contact dom4j-info@metastuff.com.
100  *
101  * 4. Products derived from this Software may not be called "DOM4J"
102  * nor may "DOM4J" appear in their names without prior written
103  * permission of MetaStuff, Ltd. DOM4J is a registered
104  * trademark of MetaStuff, Ltd.
105  *
106  * 5. Due credit should be given to the DOM4J Project
107  * (http://dom4j.org/).
108  *
109  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
110  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
111  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
112  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
113  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
114  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
115  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
116  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
117  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
118  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
119  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
120  * OF THE POSSIBILITY OF SUCH DAMAGE.
121  *
122  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
123  *
124  * $Id: FilterIterator.java,v 1.2 2003/06/10 16:18:36 per_nyfelt Exp $
125  */

126
Popular Tags