001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: LazyIterator.java,v 1.7 2008/01/02 12:07:35 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.util.iterator;
008:
009: /** An ExtendedIterator that is created lazily.
010: * This is useful when constructing an iterator is expensive and
011: * you'd prefer to delay doing it until certain it's actually needed.
012: * For example, if you have <code>iterator1.andThen(iterator2)</code>
013: * you could implement iterator2 as a LazyIterator.
014: * The sequence to be defined is defined by the subclass's definition
015: * of create(). That is called exactly once on the first attempt
016: * to interact with the LazyIterator.
017: * @author jjc, modified to use ExtendedIterators by csayers
018: * @version $Revision: 1.7 $
019: */
020: abstract public class LazyIterator implements ExtendedIterator {
021:
022: private ExtendedIterator it = null;
023:
024: /** An ExtendedIterator that is created lazily.
025: * This constructor has very low overhead - the real work is
026: * delayed until the first attempt to use the iterator.
027: */
028: public LazyIterator() {
029: }
030:
031: public boolean hasNext() {
032: lazy();
033: return it.hasNext();
034: }
035:
036: public Object next() {
037: lazy();
038: return it.next();
039: }
040:
041: public void remove() {
042: lazy();
043: it.remove();
044: }
045:
046: public ExtendedIterator andThen(ClosableIterator other) {
047: lazy();
048: return it.andThen(other);
049: }
050:
051: public ExtendedIterator filterKeep(Filter f) {
052: lazy();
053: return it.filterKeep(f);
054: }
055:
056: public ExtendedIterator filterDrop(Filter f) {
057: lazy();
058: return it.filterDrop(f);
059: }
060:
061: public ExtendedIterator mapWith(Map1 map1) {
062: lazy();
063: return it.mapWith(map1);
064: }
065:
066: public void close() {
067: lazy();
068: it.close();
069:
070: }
071:
072: private void lazy() {
073: if (it == null)
074: it = create();
075: }
076:
077: /** The subclass must define this to return
078: * the ExtendedIterator to invoke. This method will be
079: * called at most once, on the first attempt to
080: * use the iterator.
081: * From then on, all calls to this will be passed
082: * through to the returned Iterator.
083: * @return The parent iterator defining the sequence.
084: */
085: public abstract ExtendedIterator create();
086:
087: }
088:
089: /*
090: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
091: All rights reserved.
092:
093: Redistribution and use in source and binary forms, with or without
094: modification, are permitted provided that the following conditions
095: are met:
096:
097: 1. Redistributions of source code must retain the above copyright
098: notice, this list of conditions and the following disclaimer.
099:
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: */
|