001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: FragmentTripleIterator.java,v 1.12 2008/01/02 12:05:16 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.impl;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.util.iterator.*;
011:
012: import java.util.*;
013:
014: /**
015: Iterator which delivers all the triples from a Fragment's map.
016: */
017: public abstract class FragmentTripleIterator extends NiceIterator {
018: private final GraphAddList pending;
019: private final Iterator it;
020:
021: /**
022: An iterator over all the reification triples buried in <code>it</code> that match
023: <code>match</code>. The elements of the iterator are either Triples
024: or Fragmentss.
025: */
026: public FragmentTripleIterator(Triple match, Iterator it) {
027: super ();
028: this .it = it;
029: this .pending = new GraphAddList(match);
030: }
031:
032: /**
033: Answer true iff there are any triples left, ie, there are some triples in the pending
034: list once we've refilled.
035: @return true iff there are more triples to come
036: */
037: public boolean hasNext() {
038: refill();
039: return pending.size() > 0;
040: }
041:
042: /**
043: Answer the next triple in the iteration, if one exists. The triples are stored in the
044: pending list and removed from its end (which we hope is efficient).
045: @return the next triple
046: @throws NoSuchElementException if there isn't one
047: */
048: public Object next() {
049: ensureHasNext();
050: return pending.remove(pending.size() - 1);
051: }
052:
053: /**
054: Add all the [implied, matching] triples from the Object into the GraphAdd
055: entity (which will be our list). It would be nice if we could create an interface
056: for the fragmentObject's type.
057: */
058: protected abstract void fill(GraphAdd ga, Node n,
059: Object fragmentsObject);
060:
061: /**
062: Refill the pending list. Keep trying until either there are some elements
063: to be found in it, or we've run out of elements in the original iterator.
064: */
065: private void refill() {
066: while (pending.size() == 0 && it.hasNext())
067: refillFrom(pending, it.next());
068: }
069:
070: /**
071: Refill the buffer <code>pending</code> from the iterator element
072: <code>next</code>. The default behaviour is to assume that this object is
073: a Map.Entry; over-ride if it's something else.
074: */
075: protected void refillFrom(GraphAdd pending, Object next) {
076: Map.Entry e = (Map.Entry) next;
077: fill(pending, (Node) e.getKey(), e.getValue());
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: */
|