001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: SimpleEventManager.java,v 1.17 2008/01/02 12:05:18 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.mem.TrackingTripleIterator;
013: import com.hp.hpl.jena.util.IteratorCollection;
014: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
015:
016: /**
017: Simple implementation of GraphEventManager for GraphBase to use.
018: The listeners are held as an [Array]List.
019: <p>
020: The code duplication is a right pain. The most natural removal tactic, a
021: meta-method that took the notification method as an argument, is not
022: available in Java, and I can't off-hand think of a clean alternative.
023: <p>
024: This class also holds the utility method notifyingRemove, which wraps
025: iterators so that their .remove() operation notifies the specified graph of
026: the removal.
027:
028: @author hedgehog
029: */
030:
031: public class SimpleEventManager implements GraphEventManager {
032: protected Graph graph;
033: protected List listeners;
034:
035: public SimpleEventManager(Graph graph) {
036: this .graph = graph;
037: this .listeners = new ArrayList();
038: }
039:
040: public GraphEventManager register(GraphListener listener) {
041: listeners.add(listener);
042: return this ;
043: }
044:
045: public GraphEventManager unregister(GraphListener listener) {
046: listeners.remove(listener);
047: return this ;
048: }
049:
050: public boolean listening() {
051: return listeners.size() > 0;
052: }
053:
054: public void notifyAddTriple(Graph g, Triple t) {
055: for (int i = 0; i < listeners.size(); i += 1)
056: ((GraphListener) listeners.get(i)).notifyAddTriple(g, t);
057: }
058:
059: public void notifyAddArray(Graph g, Triple[] ts) {
060: for (int i = 0; i < listeners.size(); i += 1)
061: ((GraphListener) listeners.get(i)).notifyAddArray(g, ts);
062: }
063:
064: public void notifyAddList(Graph g, List L) {
065: for (int i = 0; i < listeners.size(); i += 1)
066: ((GraphListener) listeners.get(i)).notifyAddList(g, L);
067: }
068:
069: public void notifyAddIterator(Graph g, List it) {
070: for (int i = 0; i < listeners.size(); i += 1)
071: ((GraphListener) listeners.get(i)).notifyAddIterator(g, it
072: .iterator());
073: }
074:
075: public void notifyAddIterator(Graph g, Iterator it) {
076: notifyAddIterator(g, IteratorCollection.iteratorToList(it));
077: }
078:
079: public void notifyAddGraph(Graph g, Graph added) {
080: for (int i = 0; i < listeners.size(); i += 1)
081: ((GraphListener) listeners.get(i)).notifyAddGraph(g, added);
082: }
083:
084: public void notifyDeleteTriple(Graph g, Triple t) {
085: for (int i = 0; i < listeners.size(); i += 1)
086: ((GraphListener) listeners.get(i)).notifyDeleteTriple(g, t);
087: }
088:
089: public void notifyDeleteArray(Graph g, Triple[] ts) {
090: for (int i = 0; i < listeners.size(); i += 1)
091: ((GraphListener) listeners.get(i)).notifyDeleteArray(g, ts);
092: }
093:
094: public void notifyDeleteList(Graph g, List L) {
095: for (int i = 0; i < listeners.size(); i += 1)
096: ((GraphListener) listeners.get(i)).notifyDeleteList(g, L);
097: }
098:
099: public void notifyDeleteIterator(Graph g, List L) {
100: for (int i = 0; i < listeners.size(); i += 1)
101: ((GraphListener) listeners.get(i)).notifyDeleteIterator(g,
102: L.iterator());
103: }
104:
105: public void notifyDeleteIterator(Graph g, Iterator it) {
106: notifyDeleteIterator(g, IteratorCollection.iteratorToList(it));
107: }
108:
109: public void notifyDeleteGraph(Graph g, Graph removed) {
110: for (int i = 0; i < listeners.size(); i += 1)
111: ((GraphListener) listeners.get(i)).notifyDeleteGraph(g,
112: removed);
113: }
114:
115: public void notifyEvent(Graph source, Object event) {
116: for (int i = 0; i < listeners.size(); i += 1)
117: ((GraphListener) listeners.get(i)).notifyEvent(source,
118: event);
119: }
120:
121: /**
122: * Answer an iterator which wraps <code>i</code> to ensure that if a .remove()
123: * is executed on it, the graph <code>g</code> will be notified.
124: */
125: public static ExtendedIterator notifyingRemove(final Graph g,
126: Iterator i) {
127: return new TrackingTripleIterator(i) {
128: protected final GraphEventManager gem = g.getEventManager();
129:
130: public void remove() {
131: super .remove();
132: gem.notifyDeleteTriple(g, current);
133: }
134: };
135: }
136:
137: }
138:
139: /*
140: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
141: All rights reserved.
142:
143: Redistribution and use in source and binary forms, with or without
144: modification, are permitted provided that the following conditions
145: are met:
146:
147: 1. Redistributions of source code must retain the above copyright
148: notice, this list of conditions and the following disclaimer.
149:
150: 2. Redistributions in binary form must reproduce the above copyright
151: notice, this list of conditions and the following disclaimer in the
152: documentation and/or other materials provided with the distribution.
153:
154: 3. The name of the author may not be used to endorse or promote products
155: derived from this software without specific prior written permission.
156:
157: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
158: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
159: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
161: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
162: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
163: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
164: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
165: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
166: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
167: */
|