001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
003: [See end of file]
004: $Id: NodeToTriplesMap.java,v 1.46 2008/01/02 12:09:51 andy_seaborne 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.*;
013: import com.hp.hpl.jena.util.iterator.*;
014:
015: /**
016: NodeToTriplesMap: a map from nodes to sets of triples.
017: Subclasses must override at least one of useXXXInFilter methods.
018: @author kers
019: */
020: public class NodeToTriplesMap extends NodeToTriplesMapBase {
021: public NodeToTriplesMap(Field indexField, Field f2, Field f3) {
022: super (indexField, f2, f3);
023: }
024:
025: /**
026: @see com.hp.hpl.jena.mem.Temp#add(com.hp.hpl.jena.graph.Triple)
027: */
028: public boolean add(Triple t) {
029: Object o = getIndexField(t);
030: OpenSetBunch s = (OpenSetBunch) bunchMap.get(o);
031: if (s == null)
032: bunchMap.put(o, s = createSetBunch());
033: if (s.baseSet().add(t)) {
034: size += 1;
035: return true;
036: } else
037: return false;
038: }
039:
040: private static class OpenSetBunch extends SetBunch {
041: private static final TripleBunch empty = new ArrayBunch();
042:
043: public OpenSetBunch() {
044: super (empty);
045: }
046:
047: public Set baseSet() {
048: return elements;
049: }
050: }
051:
052: private OpenSetBunch createSetBunch() {
053: return new OpenSetBunch();
054: }
055:
056: /**
057: @see com.hp.hpl.jena.mem.Temp#remove(com.hp.hpl.jena.graph.Triple)
058: */
059: public boolean remove(Triple t) {
060: Object o = getIndexField(t);
061: OpenSetBunch s = (OpenSetBunch) bunchMap.get(o);
062: if (s == null)
063: return false;
064: else {
065: Set base = s.baseSet();
066: boolean result = base.remove(t);
067: if (result)
068: size -= 1;
069: if (base.isEmpty())
070: bunchMap.remove(o);
071: return result;
072: }
073: }
074:
075: public Iterator iterator(Object o, HashCommon.NotifyEmpty container) {
076: TripleBunch b = bunchMap.get(o);
077: return b == null ? NullIterator.instance : b.iterator();
078: }
079:
080: /**
081: @see com.hp.hpl.jena.mem.Temp#contains(com.hp.hpl.jena.graph.Triple)
082: */
083: public boolean contains(Triple t) {
084: TripleBunch s = bunchMap.get(getIndexField(t));
085: return s == null ? false : s.contains(t);
086: }
087:
088: protected static boolean equalsObjectOK(Triple t) {
089: Node o = t.getObject();
090: return o.isLiteral() ? o.getLiteralDatatype() == null : true;
091: }
092:
093: public boolean containsBySameValueAs(Triple t) {
094: return equalsObjectOK(t) ? contains(t) : slowContains(t);
095: }
096:
097: protected boolean slowContains(Triple t) {
098: TripleBunch s = bunchMap.get(getIndexField(t));
099: if (s == null)
100: return false;
101: else {
102: Iterator it = s.iterator();
103: while (it.hasNext())
104: if (t.matches((Triple) it.next()))
105: return true;
106: return false;
107: }
108: }
109:
110: /**
111: @see com.hp.hpl.jena.mem.Temp#iterateAll(com.hp.hpl.jena.graph.Triple)
112: */
113: public ExtendedIterator iterateAll(Triple pattern) {
114: return indexField.filterOn(pattern).and(f2.filterOn(pattern))
115: .and(f3.filterOn(pattern)).filterKeep(iterateAll());
116: }
117:
118: public ExtendedIterator iterator(Node index, Node n2, Node n3) {
119: TripleBunch s = bunchMap.get(index.getIndexingValue());
120: return s == null ? NullIterator.instance : f2.filterOn(n2).and(
121: f3.filterOn(n3)).filterKeep(s.iterator());
122: }
123:
124: /**
125: Answer an iterator over all the triples that are indexed by the item <code>y</code>.
126: Note that <code>y</code> need not be a Node (because of indexing values).
127: */
128: public Iterator iteratorForIndexed(Object y) {
129: return get(y).iterator();
130: }
131:
132: /**
133: @see com.hp.hpl.jena.mem.Temp#get(java.lang.Object)
134: */
135: private TripleBunch get(Object y) {
136: return bunchMap.get(y);
137: }
138: }
139:
140: /*
141: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
142: All rights reserved.
143:
144: Redistribution and use in source and binary forms, with or without
145: modification, are permitted provided that the following conditions
146: are met:
147:
148: 1. Redistributions of source code must retain the above copyright
149: notice, this list of conditions and the following disclaimer.
150:
151: 2. Redistributions in binary form must reproduce the above copyright
152: notice, this list of conditions and the following disclaimer in the
153: documentation and/or other materials provided with the distribution.
154:
155: 3. The name of the author may not be used to endorse or promote products
156: derived from this software without specific prior written permission.
157:
158: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
159: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
160: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
161: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
162: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
163: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
164: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
165: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
166: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
167: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168: */
|