01: /*
02: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
03: All rights reserved - see end of file.
04: $Id: ObjectIterator.java,v 1.4 2008/01/02 12:09:51 andy_seaborne Exp $
05: */
06:
07: package com.hp.hpl.jena.mem;
08:
09: import java.util.*;
10:
11: import com.hp.hpl.jena.graph.*;
12: import com.hp.hpl.jena.util.CollectionFactory;
13: import com.hp.hpl.jena.util.iterator.NiceIterator;
14:
15: /**
16: Helper class for listObjects. Because literal indexing means that the
17: domain of the object map is not a node, but an indexing value (shared by
18: a bunch of different literal nodes), getting the list of objects requires
19: mapping that indexing value to all the triples that use it, and then
20: filtering those triples for their objects, removing duplicates.
21:
22: @author kers
23: */
24: public abstract class ObjectIterator extends NiceIterator {
25: public ObjectIterator(Iterator domain) {
26: this .domain = domain;
27: }
28:
29: protected abstract Iterator iteratorFor(Object y);
30:
31: final Iterator domain;
32:
33: final Set seen = CollectionFactory.createHashedSet();
34:
35: final List pending = new ArrayList();
36:
37: public boolean hasNext() {
38: while (pending.isEmpty() && domain.hasNext())
39: refillPending();
40: return !pending.isEmpty();
41: }
42:
43: public Object next() {
44: if (!hasNext())
45: throw new NoSuchElementException(
46: "FasterTripleStore listObjects next()");
47: return pending.remove(pending.size() - 1);
48: }
49:
50: protected void refillPending() {
51: Object y = domain.next();
52: if (y instanceof Node)
53: pending.add(y);
54: else {
55: Iterator z = iteratorFor(y);
56: while (z.hasNext()) {
57: Node object = ((Triple) z.next()).getObject();
58: if (seen.add(object))
59: pending.add(object);
60: }
61: }
62: }
63:
64: public void remove() {
65: throw new UnsupportedOperationException("listObjects remove()");
66: }
67: }
68:
69: /*
70: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
71: * All rights reserved.
72: *
73: * Redistribution and use in source and binary forms, with or without
74: * modification, are permitted provided that the following conditions
75: * are met:
76: * 1. Redistributions of source code must retain the above copyright
77: * notice, this list of conditions and the following disclaimer.
78: * 2. Redistributions in binary form must reproduce the above copyright
79: * notice, this list of conditions and the following disclaimer in the
80: * documentation and/or other materials provided with the distribution.
81: * 3. The name of the author may not be used to endorse or promote products
82: * derived from this software without specific prior written permission.
83: *
84: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
85: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
86: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
87: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
88: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
89: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
90: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
91: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
92: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
93: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
94: */
|