001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: SetBunch.java,v 1.8 2008/01/02 12:09:51 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.mem;
007:
008: import java.util.HashSet;
009: import java.util.Iterator;
010: import java.util.Set;
011:
012: import com.hp.hpl.jena.graph.*;
013: import com.hp.hpl.jena.graph.query.Domain;
014: import com.hp.hpl.jena.graph.query.StageElement;
015: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
016: import com.hp.hpl.jena.util.iterator.WrappedIterator;
017:
018: public class SetBunch implements TripleBunch {
019: protected Set elements = new HashSet(20);
020:
021: public SetBunch(TripleBunch b) {
022: for (Iterator it = b.iterator(); it.hasNext();)
023: elements.add(it.next());
024: }
025:
026: protected static boolean equalsObjectOK(Triple t) {
027: Node o = t.getObject();
028: return o.isLiteral() ? o.getLiteralDatatype() == null : true;
029: }
030:
031: public boolean contains(Triple t) {
032: return elements.contains(t);
033: }
034:
035: public boolean containsBySameValueAs(Triple t) {
036: return equalsObjectOK(t) ? elements.contains(t)
037: : slowContains(t);
038: }
039:
040: protected boolean slowContains(Triple t) {
041: Iterator it = elements.iterator();
042: while (it.hasNext())
043: if (t.matches((Triple) it.next()))
044: return true;
045: return false;
046: }
047:
048: public int size() {
049: return elements.size();
050: }
051:
052: public void add(Triple t) {
053: elements.add(t);
054: }
055:
056: public void remove(Triple t) {
057: elements.remove(t);
058: }
059:
060: public ExtendedIterator iterator(HashCommon.NotifyEmpty container) {
061: return iterator();
062: }
063:
064: public ExtendedIterator iterator() {
065: return WrappedIterator.create(elements.iterator());
066: }
067:
068: public void app(Domain d, StageElement next, MatchOrBind s) {
069: Iterator it = iterator();
070: while (it.hasNext())
071: if (s.matches((Triple) it.next()))
072: next.run(d);
073: }
074: }
075:
076: /*
077: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
078: * All rights reserved.
079: *
080: * Redistribution and use in source and binary forms, with or without
081: * modification, are permitted provided that the following conditions
082: * are met:
083: * 1. Redistributions of source code must retain the above copyright
084: * notice, this list of conditions and the following disclaimer.
085: * 2. Redistributions in binary form must reproduce the above copyright
086: * notice, this list of conditions and the following disclaimer in the
087: * documentation and/or other materials provided with the distribution.
088: * 3. The name of the author may not be used to endorse or promote products
089: * derived from this software without specific prior written permission.
090: *
091: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
092: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
093: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
094: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
095: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
096: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
097: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
098: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
099: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
100: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
101: */
|