1 6 7 package com.hp.hpl.jena.mem; 8 9 import java.util.*; 10 11 import com.hp.hpl.jena.graph.*; 12 import com.hp.hpl.jena.util.CollectionFactory; 13 import com.hp.hpl.jena.util.iterator.*; 14 15 19 public abstract class NodeToTriplesMap 20 { 21 24 private Map map = CollectionFactory.createHashedMap(); 25 26 30 private int size = 0; 31 32 36 public Iterator domain() 37 { return map.keySet().iterator(); } 38 39 44 public abstract Node getIndexNode( Triple t ); 45 46 51 public boolean add( Node o, Triple t ) 52 { 53 Set s = (Set) map.get( o ); 54 if (s == null) map.put( o, s = CollectionFactory.createHashedSet() ); 55 if (s.add( t )) { size += 1; return true; } else return false; 56 } 57 58 63 public boolean remove( Node o, Triple t ) 64 { 65 Set s = (Set) map.get( o ); 66 if (s == null) 67 return false; 68 else 69 { 70 boolean result = s.remove( t ); 71 if (result) size -= 1; 72 if (s.isEmpty()) map.put( o, null ); 73 return result; 74 } 75 } 76 77 81 public Iterator iterator( Node o ) 82 { 83 Set s = (Set) map.get( o ); 84 return s == null ? NullIterator.instance : s.iterator(); 85 } 86 87 90 public ExtendedIterator iterator() 91 { 92 final Iterator nodes = domain(); 93 return new NiceIterator() 94 { 95 private Iterator current = NullIterator.instance; 96 97 public Object next() 98 { 99 if (hasNext() == false) noElements( "NodeToTriples iterator" ); 100 return current.next(); 101 } 102 103 public boolean hasNext() 104 { 105 while (true) 106 { 107 if (current.hasNext()) return true; 108 if (nodes.hasNext() == false) return false; 109 current = iterator( (Node) nodes.next() ); 110 } 111 } 112 113 public void remove() 114 { 115 current.remove(); 116 size -= 1; 117 } 118 }; 119 } 120 121 124 public void clear() 125 { map.clear(); size = 0; } 126 127 public int size() 128 { return size; } 129 130 public boolean isEmpty() 131 { return size == 0; } 132 133 137 public ExtendedIterator iterator( Triple pattern ) 138 { 139 return iterator() .filterKeep ( new TripleMatchFilter( pattern ) ); 140 } 141 142 146 public ExtendedIterator iterator( Node x, Triple pattern ) 147 { 148 return new FilterIterator( new TripleMatchFilter( pattern ), iterator( x ) ); 149 } 150 151 154 public boolean remove( Triple t ) 155 { return remove( getIndexNode( t ), t ); } 156 157 160 public boolean contains( Triple t ) 161 { 162 Set s = (Set) map.get( getIndexNode( t ) ); 163 return s == null ? false : s.contains( t ); 164 } 165 } 166 167 | Popular Tags |