1 19 20 package com.hp.hpl.jena.graph.compose; 23 24 25 import com.hp.hpl.jena.graph.*; 28 import com.hp.hpl.jena.graph.impl.*; 29 import com.hp.hpl.jena.util.IteratorCollection; 30 import com.hp.hpl.jena.util.iterator.*; 31 32 import java.util.*; 33 34 35 46 public abstract class CompositionBase 47 extends GraphBase 48 { 49 57 public int graphBaseSize() 58 { return countIterator( GraphUtil.findAll( this ) ); } 59 60 69 protected void die( String message ) 70 { throw new UnsupportedOperationException ( message ); } 71 72 81 public static Filter reject( final ClosableIterator i ) 82 { 83 final Set suppress = IteratorCollection.iteratorToSet( i ); 84 return new Filter() 85 { public boolean accept( Object o ) { return !suppress.contains( o ); } }; 86 } 87 88 98 public static ClosableIterator butNot( final ClosableIterator a, final ClosableIterator b ) 99 { 100 return new FilterIterator( reject( b ), a ); 101 } 102 103 113 public static ExtendedIterator recording( final ClosableIterator i, final Set seen ) 114 { 115 return new NiceIterator() 116 { 117 public void remove() 118 { i.remove(); } 119 120 public boolean hasNext() 121 { return i.hasNext(); } 122 123 public Object next() 124 { Object x = i.next(); 125 try { seen.add( x ); } catch (OutOfMemoryError e) { throw e; } return x; } 126 127 public void close() 128 { i.close(); } 129 }; 130 } 131 132 134 143 public static ExtendedIterator rejecting( final ExtendedIterator i, final Set seen ) 144 { 145 Filter seenFilter = new Filter() 146 { public boolean accept( Object x ) { return seen.contains( x ); } }; 147 return i.filterDrop( seenFilter ); 148 } 149 150 154 public static ExtendedIterator rejecting( final ExtendedIterator i, final Graph seen ) 155 { 156 Filter seenFilter = new Filter() 157 { public boolean accept( Object x ) { return seen.contains( (Triple) x ); } }; 158 return i.filterDrop( seenFilter ); 159 } 160 161 170 public static int countIterator( ClosableIterator i ) 171 { 172 try { int n = 0; while (i.hasNext()) { n += 1; i.next(); } return n; } 173 finally { i.close(); } 174 } 175 176 185 public static Filter ifIn( final ClosableIterator i ) 186 { 187 final Set allow = IteratorCollection.iteratorToSet( i ); 188 return new Filter() 189 { public boolean accept( Object x ) { return allow.contains( x ); } }; 190 } 191 192 201 public static Filter ifIn( final Graph g ) 202 { 203 return new Filter() 204 { public boolean accept( Object x ) { return g.contains( (Triple) x ); } }; 205 } 206 207 208 211 212 216 217 } 218 219 220 249 250 | Popular Tags |