KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: SimpleBulkUpdateHandler.java,v 1.25 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.util.IteratorCollection;
13 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
14
15 /**
16     A simple-minded implementation of the bulk update interface. This only
17     operates on (subclasses of) GraphBase, since it needs access to the
18     performAdd/performDelete operations.
19 <p>
20     It handles update events, with a special eye to not copying iterators unless
21     there is at least one listener registered with the graph's event manager.
22     
23     @author kers
24 */

25
26 public class SimpleBulkUpdateHandler implements BulkUpdateHandler
27     {
28     protected GraphWithPerform graph;
29     protected GraphEventManager manager;
30     
31     public SimpleBulkUpdateHandler( GraphWithPerform graph )
32         {
33         this.graph = graph;
34         this.manager = graph.getEventManager();
35         }
36
37     public void add( Triple [] triples )
38         {
39         for (int i = 0; i < triples.length; i += 1) graph.performAdd( triples[i] );
40         manager.notifyAddArray( graph, triples );
41         }
42         
43     public void add( List triples )
44         { add( triples, true ); }
45         
46     protected void add( List triples, boolean notify )
47         {
48         for (int i = 0; i < triples.size(); i += 1) graph.performAdd( (Triple) triples.get(i) );
49         if (notify) manager.notifyAddList( graph, triples );
50         }
51
52     public void add( Iterator it )
53         { addIterator( it, true ); }
54
55     public void addIterator( Iterator it, boolean notify )
56         {
57         List s = IteratorCollection.iteratorToList( it );
58         add( s, false );
59         if (notify) manager.notifyAddIterator( graph, s );
60         }
61         
62     public void add( Graph g )
63         { add( g, false ); }
64         
65     public void add( Graph g, boolean withReifications )
66         {
67         addIterator( GraphUtil.findAll( g ), false );
68         if (withReifications) addReifications( graph, g );
69         manager.notifyAddGraph( graph, g );
70         }
71         
72     public static void addReifications( Graph ours, Graph g )
73         {
74         Reifier r = g.getReifier();
75         Iterator it = r.allNodes();
76         while (it.hasNext())
77             {
78             Node node = (Node) it.next();
79             ours.getReifier().reifyAs( node, r.getTriple( node ) );
80             }
81         }
82         
83     public static void deleteReifications( Graph ours, Graph g )
84         {
85         Reifier r = g.getReifier();
86         Iterator it = r.allNodes();
87         while (it.hasNext())
88             {
89             Node node = (Node) it.next();
90             ours.getReifier().remove( node, r.getTriple( node ) );
91             }
92         }
93
94     public void delete( Triple [] triples )
95         {
96         for (int i = 0; i < triples.length; i += 1) graph.performDelete( triples[i] );
97         manager.notifyDeleteArray( graph, triples );
98         }
99     
100     public void delete( List triples )
101         { delete( triples, true ); }
102         
103     protected void delete( List triples, boolean notify )
104         {
105         for (int i = 0; i < triples.size(); i += 1) graph.performDelete( (Triple) triples.get(i) );
106         if (notify) manager.notifyDeleteList( graph, triples );
107         }
108     
109     public void delete( Iterator it )
110         { deleteIterator( it, true ); }
111         
112     public void deleteIterator( Iterator it, boolean notify )
113         {
114         List L = IteratorCollection.iteratorToList( it );
115         delete( L, false );
116         if (notify) manager.notifyDeleteIterator( graph, L );
117          }
118          
119     private List triplesOf( Graph g )
120         {
121         ArrayList L = new ArrayList();
122         Iterator it = g.find( Triple.ANY );
123         while (it.hasNext()) L.add( it.next() );
124         return L;
125         }
126             
127     public void delete( Graph g )
128         { delete( g, false ); }
129         
130     public void delete( Graph g, boolean withReifications )
131         {
132         if (g.dependsOn( graph ))
133             delete( triplesOf( g ) );
134         else
135             deleteIterator( GraphUtil.findAll( g ), false );
136         if (withReifications) deleteReifications( graph, g );
137         manager.notifyDeleteGraph( graph, g );
138         }
139     
140     public void removeAll()
141         { removeAll( graph );
142         notifyRemoveAll(); }
143     
144     protected void notifyRemoveAll()
145         { manager.notifyEvent( graph, GraphEvents.removeAll ); }
146
147     public void remove( Node s, Node p, Node o )
148         { removeAll( graph, s, p, o );
149         manager.notifyEvent( graph, GraphEvents.remove( s, p, o ) ); }
150     
151     public static void removeAll( Graph g, Node s, Node p, Node o )
152         {
153         ExtendedIterator it = g.find( s, p, o );
154         try { while (it.hasNext()) { it.next(); it.remove(); } }
155         finally { it.close(); }
156         }
157     
158     public static void removeAll( Graph g )
159         {
160         ExtendedIterator it = GraphUtil.findAll( g );
161         try { while (it.hasNext()) { it.next(); it.remove(); } }
162         finally { it.close(); }
163         }
164     }
165
166
167 /*
168     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
169     All rights reserved.
170
171     Redistribution and use in source and binary forms, with or without
172     modification, are permitted provided that the following conditions
173     are met:
174
175     1. Redistributions of source code must retain the above copyright
176        notice, this list of conditions and the following disclaimer.
177
178     2. Redistributions in binary form must reproduce the above copyright
179        notice, this list of conditions and the following disclaimer in the
180        documentation and/or other materials provided with the distribution.
181
182     3. The name of the author may not be used to endorse or promote products
183        derived from this software without specific prior written permission.
184
185     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
189     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
190     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
191     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
192     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
193     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
194     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
195 */
Popular Tags