KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: LazyIterator.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 /** An ExtendedIterator that is created lazily.
10  * This is useful when constructing an iterator is expensive and
11  * you'd prefer to delay doing it until certain it's actually needed.
12  * For example, if you have <code>iterator1.andThen(iterator2)</code>
13  * you could implement iterator2 as a LazyIterator.
14  * The sequence to be defined is defined by the subclass's definition
15  * of create(). That is called exactly once on the first attempt
16  * to interact with the LazyIterator.
17  * @author jjc, modified to use ExtendedIterators by csayers
18  * @version $Revision: 1.4 $
19  */

20 abstract public class LazyIterator implements ExtendedIterator {
21
22     private ExtendedIterator it=null;
23
24     /** An ExtendedIterator that is created lazily.
25      * This constructor has very low overhead - the real work is
26      * delayed until the first attempt to use the iterator.
27      */

28     public LazyIterator() {
29     }
30
31     public boolean hasNext() {
32         lazy();
33         return it.hasNext();
34     }
35
36     public Object JavaDoc next() {
37         lazy();
38         return it.next();
39     }
40
41     public void remove() {
42         lazy();
43         it.remove();
44     }
45
46     public ExtendedIterator andThen(ClosableIterator other) {
47         lazy();
48         return it.andThen(other);
49     }
50
51     public ExtendedIterator filterKeep(Filter f) {
52         lazy();
53         return it.filterKeep(f);
54     }
55
56     public ExtendedIterator filterDrop(Filter f) {
57         lazy();
58         return it.filterDrop(f);
59     }
60
61     public ExtendedIterator mapWith(Map1 map1) {
62         lazy();
63         return it.mapWith(map1);
64     }
65
66     public void close() {
67         lazy();
68         it.close();
69             
70     }
71      
72     private void lazy() {
73         if (it == null)
74             it = create();
75     }
76
77     /** The subclass must define this to return
78      * the ExtendedIterator to invoke. This method will be
79      * called at most once, on the first attempt to
80      * use the iterator.
81      * From then on, all calls to this will be passed
82      * through to the returned Iterator.
83      * @return The parent iterator defining the sequence.
84      */

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

118
Popular Tags