001: /*
002: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: SimpleBulkUpdateHandler.java,v 1.28 2008/01/02 12:05:20 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.impl;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.util.IteratorCollection;
013: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
014:
015: /**
016: A simple-minded implementation of the bulk update interface. This only
017: operates on (subclasses of) GraphBase, since it needs access to the
018: performAdd/performDelete operations.
019: <p>
020: It handles update events, with a special eye to not copying iterators unless
021: there is at least one listener registered with the graph's event manager.
022:
023: @author kers
024: */
025:
026: public class SimpleBulkUpdateHandler implements BulkUpdateHandler {
027: protected GraphWithPerform graph;
028: protected GraphEventManager manager;
029:
030: public SimpleBulkUpdateHandler(GraphWithPerform graph) {
031: this .graph = graph;
032: this .manager = graph.getEventManager();
033: }
034:
035: public void add(Triple[] triples) {
036: for (int i = 0; i < triples.length; i += 1)
037: graph.performAdd(triples[i]);
038: manager.notifyAddArray(graph, triples);
039: }
040:
041: public void add(List triples) {
042: add(triples, true);
043: }
044:
045: protected void add(List triples, boolean notify) {
046: for (int i = 0; i < triples.size(); i += 1)
047: graph.performAdd((Triple) triples.get(i));
048: if (notify)
049: manager.notifyAddList(graph, triples);
050: }
051:
052: public void add(Iterator it) {
053: addIterator(it, true);
054: }
055:
056: public void addIterator(Iterator it, boolean notify) {
057: List s = IteratorCollection.iteratorToList(it);
058: add(s, false);
059: if (notify)
060: manager.notifyAddIterator(graph, s);
061: }
062:
063: public void add(Graph g) {
064: add(g, false);
065: }
066:
067: public void add(Graph g, boolean withReifications) {
068: addIterator(GraphUtil.findAll(g), false);
069: if (withReifications)
070: addReifications(graph, g);
071: manager.notifyAddGraph(graph, g);
072: }
073:
074: public static void addReifications(Graph ours, Graph g) {
075: Reifier r = g.getReifier();
076: Iterator it = r.allNodes();
077: while (it.hasNext()) {
078: Node node = (Node) it.next();
079: ours.getReifier().reifyAs(node, r.getTriple(node));
080: }
081: }
082:
083: public static void deleteReifications(Graph ours, Graph g) {
084: Reifier r = g.getReifier();
085: Iterator it = r.allNodes();
086: while (it.hasNext()) {
087: Node node = (Node) it.next();
088: ours.getReifier().remove(node, r.getTriple(node));
089: }
090: }
091:
092: public void delete(Triple[] triples) {
093: for (int i = 0; i < triples.length; i += 1)
094: graph.performDelete(triples[i]);
095: manager.notifyDeleteArray(graph, triples);
096: }
097:
098: public void delete(List triples) {
099: delete(triples, true);
100: }
101:
102: protected void delete(List triples, boolean notify) {
103: for (int i = 0; i < triples.size(); i += 1)
104: graph.performDelete((Triple) triples.get(i));
105: if (notify)
106: manager.notifyDeleteList(graph, triples);
107: }
108:
109: public void delete(Iterator it) {
110: deleteIterator(it, true);
111: }
112:
113: public void deleteIterator(Iterator it, boolean notify) {
114: List L = IteratorCollection.iteratorToList(it);
115: delete(L, false);
116: if (notify)
117: manager.notifyDeleteIterator(graph, L);
118: }
119:
120: private List triplesOf(Graph g) {
121: ArrayList L = new ArrayList();
122: Iterator it = g.find(Triple.ANY);
123: while (it.hasNext())
124: L.add(it.next());
125: return L;
126: }
127:
128: public void delete(Graph g) {
129: delete(g, false);
130: }
131:
132: public void delete(Graph g, boolean withReifications) {
133: if (g.dependsOn(graph))
134: delete(triplesOf(g));
135: else
136: deleteIterator(GraphUtil.findAll(g), false);
137: if (withReifications)
138: deleteReifications(graph, g);
139: manager.notifyDeleteGraph(graph, g);
140: }
141:
142: public void removeAll() {
143: removeAll(graph);
144: notifyRemoveAll();
145: }
146:
147: protected void notifyRemoveAll() {
148: manager.notifyEvent(graph, GraphEvents.removeAll);
149: }
150:
151: public void remove(Node s, Node p, Node o) {
152: removeAll(graph, s, p, o);
153: manager.notifyEvent(graph, GraphEvents.remove(s, p, o));
154: }
155:
156: public static void removeAll(Graph g, Node s, Node p, Node o) {
157: ExtendedIterator it = g.find(s, p, o);
158: try {
159: while (it.hasNext()) {
160: it.next();
161: it.remove();
162: }
163: } finally {
164: it.close();
165: }
166: }
167:
168: public static void removeAll(Graph g) {
169: ExtendedIterator it = GraphUtil.findAll(g);
170: try {
171: while (it.hasNext()) {
172: it.next();
173: it.remove();
174: }
175: } finally {
176: it.close();
177: }
178: }
179: }
180:
181: /*
182: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
183: All rights reserved.
184:
185: Redistribution and use in source and binary forms, with or without
186: modification, are permitted provided that the following conditions
187: are met:
188:
189: 1. Redistributions of source code must retain the above copyright
190: notice, this list of conditions and the following disclaimer.
191:
192: 2. Redistributions in binary form must reproduce the above copyright
193: notice, this list of conditions and the following disclaimer in the
194: documentation and/or other materials provided with the distribution.
195:
196: 3. The name of the author may not be used to endorse or promote products
197: derived from this software without specific prior written permission.
198:
199: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
200: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
203: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
204: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
205: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
206: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
207: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
208: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
209: */
|