001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: NodeToTriplesMapBase.java,v 1.18 2008/01/03 15:41:18 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.mem;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.graph.Triple.Field;
013: import com.hp.hpl.jena.util.iterator.*;
014:
015: /**
016: A base class for the "normal" and "faster" NodeToTriplesMaps.
017:
018: @author kers
019: */
020: public abstract class NodeToTriplesMapBase {
021: /**
022: The map from nodes to Bunch(Triple).
023: */
024: public BunchMap bunchMap = new HashedBunchMap();
025:
026: /**
027: The number of triples held in this NTM, maintained incrementally
028: (because it's a pain to compute from scratch).
029: */
030: protected int size = 0;
031:
032: protected final Field indexField;
033: protected final Field f2;
034: protected final Field f3;
035:
036: public NodeToTriplesMapBase(Field indexField, Field f2, Field f3) {
037: this .indexField = indexField;
038: this .f2 = f2;
039: this .f3 = f3;
040: }
041:
042: /**
043: Add <code>t</code> to this NTM; the node <code>o</code> <i>must</i>
044: be the index node of the triple. Answer <code>true</code> iff the triple
045: was not previously in the set, ie, it really truly has been added.
046: */
047: public abstract boolean add(Triple t);
048:
049: /**
050: Remove <code>t</code> from this NTM. Answer <code>true</code> iff the
051: triple was previously in the set, ie, it really truly has been removed.
052: */
053: public abstract boolean remove(Triple t);
054:
055: public abstract Iterator iterator(Object o,
056: HashCommon.NotifyEmpty container);
057:
058: /**
059: Answer true iff this NTM contains the concrete triple <code>t</code>.
060: */
061: public abstract boolean contains(Triple t);
062:
063: public abstract boolean containsBySameValueAs(Triple t);
064:
065: /**
066: The nodes which appear in the index position of the stored triples; useful
067: for eg listSubjects().
068: */
069: public final Iterator domain() {
070: return bunchMap.keyIterator();
071: }
072:
073: protected final Object getIndexField(Triple t) {
074: return indexField.getField(t).getIndexingValue();
075: }
076:
077: /**
078: Clear this NTM; it will contain no triples.
079: */
080: public void clear() {
081: bunchMap.clear();
082: size = 0;
083: }
084:
085: public int size() {
086: return size;
087: }
088:
089: public void removedOneViaIterator() {
090: size -= 1; /* System.err.println( ">> rOVI: size := " + size ); */
091: }
092:
093: public boolean isEmpty() {
094: return size == 0;
095: }
096:
097: public abstract ExtendedIterator iterator(Node index, Node n2,
098: Node n3);
099:
100: /**
101: Answer an iterator over all the triples that are indexed by the item <code>y</code>.
102: Note that <code>y</code> need not be a Node (because of indexing values).
103: */
104: public abstract Iterator iteratorForIndexed(Object y);
105:
106: /**
107: Answer an iterator over all the triples in this NTM.
108: */
109: public ExtendedIterator iterateAll() {
110: final Iterator nodes = domain();
111: // System.err.println( "*>> NTM:iterateAll: nodes = " + IteratorCollection.iteratorToList( domain() ) );
112: return new NiceIterator() {
113: private Iterator current = NullIterator.instance;
114: private NotifyMe emptier = new NotifyMe();
115:
116: // private Object cn = "(none)";
117:
118: public Object next() {
119: if (hasNext() == false)
120: noElements("NodeToTriples iterator");
121: return current.next();
122: }
123:
124: class NotifyMe implements HashCommon.NotifyEmpty {
125: public void emptied() {
126: // System.err.println( ">> exhausted iterator for " + cn );
127: nodes.remove();
128: }
129: }
130:
131: public boolean hasNext() {
132: while (true) {
133: if (current.hasNext())
134: return true;
135: if (nodes.hasNext() == false)
136: return false;
137: Object next = nodes.next();
138: // cn = next;
139: // System.err.println( ">----> NTM:iterateAll:hasNext: node " + next );
140: current = iterator(next, emptier);
141: }
142: }
143:
144: public void remove() {
145: current.remove();
146: }
147: };
148: }
149: }
150:
151: /*
152: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
153: * reserved.
154: *
155: * Redistribution and use in source and binary forms, with or without
156: * modification, are permitted provided that the following conditions are met:
157: * 1. Redistributions of source code must retain the above copyright notice,
158: * this list of conditions and the following disclaimer. 2. Redistributions in
159: * binary form must reproduce the above copyright notice, this list of
160: * conditions and the following disclaimer in the documentation and/or other
161: * materials provided with the distribution. 3. The name of the author may not
162: * be used to endorse or promote products derived from this software without
163: * specific prior written permission.
164: *
165: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
166: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
167: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
168: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
169: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
170: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
171: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
172: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
173: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
174: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
175: */
|