KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: MapFilterIterator.java,v 1.4 2005/02/21 12:19:16 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.util.iterator;
8
9 import java.util.*;
10
11 /**
12     A MapFilterIterator takes a MapFilter and an [Extended]Iterator and returns a new
13     ExtendedIterator which delivers the sequence of all non-null elements MapFilter(X)
14     for X from the base iterator.
15     @author kers
16 */

17
18 public class MapFilterIterator extends NiceIterator implements ExtendedIterator
19     {
20     MapFilter f;
21     Object JavaDoc current;
22     boolean dead;
23     ClosableIterator underlying;
24     
25 /** Creates a sub-Iterator.
26  * @param fl An object is included if it is accepted by this Filter.
27  * @param e The parent Iterator.
28  */

29     public MapFilterIterator( MapFilter fl, ExtendedIterator e) {
30         f = fl;
31         current = null;
32         dead = false;
33         underlying = e;
34     }
35     
36 /** Are there any more acceptable objects.
37  * @return true if there is another acceptable object.
38  */

39     synchronized public boolean hasNext() {
40         if (current!=null)
41             return true;
42         while ( underlying.hasNext() ) {
43             current = f.accept( underlying.next() );
44             if (current != null)
45                 return true;
46         }
47         current = null;
48         dead = true;
49         return false;
50     }
51     
52     public void close()
53         {
54         underlying.close();
55         }
56         
57 /** remove's the member from the underlying <CODE>Iterator</CODE>;
58    <CODE>hasNext()</CODE> may not be called between calls to
59     <CODE>next()</CODE> and <CODE>remove()</CODE>.
60  */

61         synchronized public void remove() {
62             if ( current != null || dead )
63               throw new IllegalStateException JavaDoc(
64               "FilterIterator does not permit calls to hasNext between calls to next and remove.");
65
66             underlying.remove();
67         }
68 /** The next acceptable object in the iterator.
69  * @return The next acceptable object.
70  */

71     synchronized public Object JavaDoc next() {
72         if (hasNext()) {
73             Object JavaDoc r = current;
74             current = null;
75             return r;
76         }
77         throw new NoSuchElementException();
78     }
79 }
80
81
82 /*
83     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
84     All rights reserved.
85
86     Redistribution and use in source and binary forms, with or without
87     modification, are permitted provided that the following conditions
88     are met:
89
90     1. Redistributions of source code must retain the above copyright
91        notice, this list of conditions and the following disclaimer.
92
93     2. Redistributions in binary form must reproduce the above copyright
94        notice, this list of conditions and the following disclaimer in the
95        documentation and/or other materials provided with the distribution.
96
97     3. The name of the author may not be used to endorse or promote products
98        derived from this software without specific prior written permission.
99
100     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
101     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
102     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
103     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
104     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
105     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
106     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
107     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
108     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
109     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
110 */

111
Popular Tags