KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > impl > SimpleEventManager


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: SimpleEventManager.java,v 1.13 2005/02/21 11:52:10 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.impl;
8
9 import java.util.*;
10
11 import com.hp.hpl.jena.graph.*;
12 import com.hp.hpl.jena.mem.TrackingTripleIterator;
13 import com.hp.hpl.jena.util.IteratorCollection;
14 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
15
16 /**
17     Simple implementation of GraphEventManager for GraphBase to use.
18     The listeners are held as an [Array]List.
19 <p>
20     The code duplication is a right pain. The most natural removal tactic, a
21     meta-method that took the notification method as an argument, is not
22     available in Java, and I can't off-hand think of a clean alternative.
23 <p>
24     This class also holds the utility method notifyingRemove, which wraps
25     iterators so that their .remove() operation notifies the specified graph of
26     the removal.
27     
28     @author hedgehog
29 */

30
31 public class SimpleEventManager implements GraphEventManager
32     {
33     protected Graph graph;
34     protected List listeners;
35     
36     SimpleEventManager( Graph graph )
37         {
38         this.graph = graph;
39         this.listeners = new ArrayList();
40         }
41     
42     public GraphEventManager register( GraphListener listener )
43         {
44         listeners.add( listener );
45         return this;
46         }
47         
48     public GraphEventManager unregister( GraphListener listener )
49         {
50         listeners.remove( listener );
51         return this;
52         }
53     
54     public boolean listening()
55         { return listeners.size() > 0; }
56         
57     public void notifyAddTriple( Graph g, Triple t )
58         {
59         for (int i = 0; i < listeners.size(); i += 1)
60             ((GraphListener) listeners.get(i)).notifyAddTriple( g, t );
61         }
62     
63     public void notifyAddArray( Graph g, Triple [] ts )
64         {
65         for (int i = 0; i < listeners.size(); i += 1)
66             ((GraphListener) listeners.get(i)).notifyAddArray( g, ts );
67         }
68         
69     public void notifyAddList( Graph g, List L )
70         {
71         for (int i = 0; i < listeners.size(); i += 1)
72             ((GraphListener) listeners.get(i)).notifyAddList( g, L);
73         }
74         
75     public void notifyAddIterator( Graph g, List it )
76         {
77         for (int i = 0; i < listeners.size(); i += 1)
78             ((GraphListener) listeners.get(i)).notifyAddIterator( g, it.iterator() );
79         }
80         
81     public void notifyAddIterator( Graph g, Iterator it )
82         { notifyAddIterator( g, IteratorCollection.iteratorToList( it ) ); }
83         
84     public void notifyAddGraph( Graph g, Graph added )
85         {
86         for (int i = 0; i < listeners.size(); i += 1)
87             ((GraphListener) listeners.get(i)).notifyAddGraph( g, added );
88         }
89         
90     public void notifyDeleteTriple( Graph g, Triple t )
91         {
92         for (int i = 0; i < listeners.size(); i += 1)
93             ((GraphListener) listeners.get(i)).notifyDeleteTriple( g, t );
94         }
95         
96     public void notifyDeleteArray( Graph g, Triple [] ts )
97         {
98         for (int i = 0; i < listeners.size(); i += 1)
99             ((GraphListener) listeners.get(i)).notifyDeleteArray( g, ts );
100         }
101         
102     public void notifyDeleteList( Graph g, List L )
103         {
104         for (int i = 0; i < listeners.size(); i += 1)
105             ((GraphListener) listeners.get(i)).notifyDeleteList( g, L );
106         }
107         
108     public void notifyDeleteIterator( Graph g, List L )
109         {
110         for (int i = 0; i < listeners.size(); i += 1)
111             ((GraphListener) listeners.get(i)).notifyDeleteIterator( g, L.iterator() );
112         }
113         
114     public void notifyDeleteIterator( Graph g, Iterator it )
115         { notifyDeleteIterator( g, IteratorCollection.iteratorToList( it ) ); }
116             
117     public void notifyDeleteGraph( Graph g, Graph removed )
118         {
119         for (int i = 0; i < listeners.size(); i += 1)
120             ((GraphListener) listeners.get(i)).notifyDeleteGraph( g, removed );
121         }
122     
123     public void notifyEvent( Graph source, Object JavaDoc event )
124         {
125         for (int i = 0; i < listeners.size(); i += 1)
126             ((GraphListener) listeners.get(i)).notifyEvent( source, event ); }
127
128     /**
129      * Answer an iterator which wraps <code>i</code> to ensure that if a .remove()
130      * is executed on it, the graph <code>g</code> will be notified.
131     */

132     public static ExtendedIterator notifyingRemove( final Graph g, Iterator i )
133         {
134         return new TrackingTripleIterator( i )
135             {
136             protected final GraphEventManager gem = g.getEventManager();
137             public void remove()
138                 {
139                 super.remove();
140                 gem.notifyDeleteTriple( g, current );
141                 }
142             };
143         }
144     
145     }
146
147 /*
148     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
149     All rights reserved.
150
151     Redistribution and use in source and binary forms, with or without
152     modification, are permitted provided that the following conditions
153     are met:
154
155     1. Redistributions of source code must retain the above copyright
156        notice, this list of conditions and the following disclaimer.
157
158     2. Redistributions in binary form must reproduce the above copyright
159        notice, this list of conditions and the following disclaimer in the
160        documentation and/or other materials provided with the distribution.
161
162     3. The name of the author may not be used to endorse or promote products
163        derived from this software without specific prior written permission.
164
165     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
166     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
167     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
168     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
169     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
170     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
171     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
172     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
173     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
174     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
175 */
Popular Tags