1 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 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 | Popular Tags |