001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: MapFilterIterator.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: import java.util.*;
010:
011: /**
012: A MapFilterIterator takes a MapFilter and an [Extended]Iterator and returns a new
013: ExtendedIterator which delivers the sequence of all non-null elements MapFilter(X)
014: for X from the base iterator.
015: @author kers
016: */
017:
018: public class MapFilterIterator extends NiceIterator implements
019: ExtendedIterator {
020: MapFilter f;
021: Object current;
022: boolean dead;
023: ClosableIterator underlying;
024:
025: /** Creates a sub-Iterator.
026: * @param fl An object is included if it is accepted by this Filter.
027: * @param e The parent Iterator.
028: */
029: public MapFilterIterator(MapFilter fl, ExtendedIterator e) {
030: f = fl;
031: current = null;
032: dead = false;
033: underlying = e;
034: }
035:
036: /** Are there any more acceptable objects.
037: * @return true if there is another acceptable object.
038: */
039: synchronized public boolean hasNext() {
040: if (current != null)
041: return true;
042: while (underlying.hasNext()) {
043: current = f.accept(underlying.next());
044: if (current != null)
045: return true;
046: }
047: current = null;
048: dead = true;
049: return false;
050: }
051:
052: public void close() {
053: underlying.close();
054: }
055:
056: /** remove's the member from the underlying <CODE>Iterator</CODE>;
057: <CODE>hasNext()</CODE> may not be called between calls to
058: <CODE>next()</CODE> and <CODE>remove()</CODE>.
059: */
060: synchronized public void remove() {
061: if (current != null || dead)
062: throw new IllegalStateException(
063: "FilterIterator does not permit calls to hasNext between calls to next and remove.");
064:
065: underlying.remove();
066: }
067:
068: /** The next acceptable object in the iterator.
069: * @return The next acceptable object.
070: */
071: synchronized public Object next() {
072: if (hasNext()) {
073: Object r = current;
074: current = null;
075: return r;
076: }
077: throw new NoSuchElementException();
078: }
079: }
080:
081: /*
082: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
083: All rights reserved.
084:
085: Redistribution and use in source and binary forms, with or without
086: modification, are permitted provided that the following conditions
087: are met:
088:
089: 1. Redistributions of source code must retain the above copyright
090: notice, this list of conditions and the following disclaimer.
091:
092: 2. Redistributions in binary form must reproduce the above copyright
093: notice, this list of conditions and the following disclaimer in the
094: documentation and/or other materials provided with the distribution.
095:
096: 3. The name of the author may not be used to endorse or promote products
097: derived from this software without specific prior written permission.
098:
099: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
100: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
101: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
102: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
103: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
104: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
105: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
106: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
107: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
108: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
109: */
|