001: /******************************************************************
002: * File: MonitorGraph.java
003: * Created by: Dave Reynolds
004: * Created on: 12-May-2005
005: *
006: * (c) Copyright 2005, Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: MonitorGraph.java,v 1.4 2008/01/02 12:07:43 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.util;
010:
011: import java.util.*;
012: import com.hp.hpl.jena.graph.*;
013: import com.hp.hpl.jena.graph.impl.*;
014:
015: /**
016: * Graph wrapper which provides normal access to an underlying graph but
017: * also maintains a snapshot of the triples it was last known to contain.
018: * A snapshot action
019: * causes the set of changes between this and the previous snapshot to
020: * be calculated and the cache updated. The snapshot process will also
021: * fire change notification.
022: *
023: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
024: * @version $Revision: 1.4 $
025: */
026:
027: public class MonitorGraph extends WrappedGraph {
028:
029: /** The last known snapshot, a set of triples */
030: protected Set snapshot = new HashSet();
031:
032: /** Constructor, wrap the given graph with a state monitor */
033: public MonitorGraph(Graph g) {
034: super (g);
035: }
036:
037: /**
038: * Compute the differences between the current monitored graph and the last
039: * snapshot. The changes will also be forwarded to any listeners.
040: * Then take a new snapshot.
041: * @param additions a place in which the set of newly added triples should be noted, can be null
042: * @param deletions a place in which the set of newly deleted triples should be noted, can be null
043: */
044: public void snapshot(List additions, List deletions) {
045: boolean listening = getEventManager().listening();
046: boolean wantAdditions = listening || additions != null;
047: boolean wantDeletions = listening || deletions != null;
048:
049: List additionsTemp = (additions != null) ? additions
050: : new ArrayList();
051: List deletionsTemp = (deletions != null) ? deletions
052: : new ArrayList();
053: Set deletionsTempSet = (wantDeletions) ? new HashSet() : null;
054:
055: if (wantAdditions || wantDeletions) {
056: if (wantDeletions) {
057: deletionsTempSet.addAll(snapshot);
058: }
059: for (Iterator i = base.find(Node.ANY, Node.ANY, Node.ANY); i
060: .hasNext();) {
061: Object triple = i.next();
062: if (wantAdditions && !snapshot.contains(triple)) {
063: additionsTemp.add(triple);
064: }
065: if (wantDeletions) {
066: deletionsTempSet.remove(triple);
067: }
068: }
069: }
070: if (deletions != null) {
071: // We use a set for performance computing in deletions but specify a list
072: // for the method signature for compatibility with listeners
073: deletionsTemp.addAll(deletionsTempSet);
074: }
075:
076: if (listening) {
077: getEventManager().notifyAddList(this , additionsTemp);
078: getEventManager().notifyDeleteList(this , deletionsTemp);
079: }
080:
081: // Update shapshot
082: // In somecases applying the already computed changes may be cheaper, could optmize
083: // this based on relative sizes if it becomes an issue.
084: snapshot.clear();
085: for (Iterator i = base.find(Node.ANY, Node.ANY, Node.ANY); i
086: .hasNext();) {
087: snapshot.add(i.next());
088: }
089:
090: }
091:
092: }
093:
094: /*
095: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
096: All rights reserved.
097:
098: Redistribution and use in source and binary forms, with or without
099: modification, are permitted provided that the following conditions
100: are met:
101:
102: 1. Redistributions of source code must retain the above copyright
103: notice, this list of conditions and the following disclaimer.
104:
105: 2. Redistributions in binary form must reproduce the above copyright
106: notice, this list of conditions and the following disclaimer in the
107: documentation and/or other materials provided with the distribution.
108:
109: 3. The name of the author may not be used to endorse or promote products
110: derived from this software without specific prior written permission.
111:
112: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
113: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
114: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
115: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
116: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
117: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
118: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
119: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
120: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
121: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
122: */
|