KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > iterator > FilterIterator


1 /*
2  * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $Id: FilterIterator.java,v 1.6 2005/02/21 12:19:15 andy_seaborne Exp $
28  *
29  */

30 package com.hp.hpl.jena.util.iterator;
31
32 import java.util.Iterator JavaDoc;
33 import java.util.NoSuchElementException JavaDoc;
34
35 /** Creates a sub-Iterator by filtering.
36  * @author jjc
37  * @version Release='$Name: $' Revision='$Revision: 1.6 $' Date='$Date: 2005/02/21 12:19:15 $'
38  */

39 public class FilterIterator extends WrappedIterator
40 {
41     Filter f;
42     Object JavaDoc current;
43     boolean dead;
44
45     /** Creates a sub-Iterator.
46     * @param fl An object is included if it is accepted by this Filter.
47     * @param e The parent Iterator.
48     */

49     public FilterIterator( Filter fl, Iterator JavaDoc e) {
50         super(e);
51         f = fl;
52         current = null;
53         dead = false;
54     }
55
56     /** Are there any more acceptable objects.
57     * @return true if there is another acceptable object.
58     */

59     synchronized public boolean hasNext() {
60         if (current!=null)
61             return true;
62         while ( super.hasNext() ) {
63             current = super.next();
64             if (f.accept(current))
65                 return true;
66         }
67         current = null;
68         dead = true;
69         return false;
70     }
71     
72     /** remove's the member from the underlying <CODE>Iterator</CODE>;
73     <CODE>hasNext()</CODE> may not be called between calls to
74     <CODE>next()</CODE> and <CODE>remove()</CODE>.
75     */

76     synchronized public void remove() {
77         if ( current != null || dead )
78           throw new IllegalStateException JavaDoc(
79           "FilterIterator does not permit calls to hasNext between calls to next and remove.");
80         super.remove();
81         }
82         
83     /** The next acceptable object in the iterator.
84     * @return The next acceptable object.
85     */

86     synchronized public Object JavaDoc next() {
87         if (hasNext()) {
88             Object JavaDoc r = current;
89             current = null;
90             return r;
91         }
92         throw new NoSuchElementException JavaDoc();
93     }
94 }
95
Popular Tags