1 package prefuse.data.tuple; 2 3 import java.util.HashSet ; 4 import java.util.Iterator ; 5 import java.util.LinkedHashMap ; 6 import java.util.Map ; 7 import java.util.Set ; 8 import java.util.logging.Logger ; 9 10 import prefuse.data.Table; 11 import prefuse.data.Tuple; 12 import prefuse.data.event.TupleSetListener; 13 import prefuse.data.expression.Expression; 14 import prefuse.data.expression.Predicate; 15 import prefuse.data.expression.parser.ExpressionParser; 16 import prefuse.util.collections.CompositeIterator; 17 18 30 public class CompositeTupleSet extends AbstractTupleSet { 31 32 private static final Logger s_logger 33 = Logger.getLogger(CompositeTupleSet.class.getName()); 34 35 private Map m_map; private Set m_sets; private int m_count; private Listener m_lstnr; 39 40 43 public CompositeTupleSet() { 44 this(true); 45 } 46 47 protected CompositeTupleSet(boolean listen) { 48 m_map = new LinkedHashMap (); 49 m_sets = new HashSet (); 50 m_count = 0; 51 m_lstnr = listen ? new Listener() : null; 52 } 53 54 59 public void addSet(String name, TupleSet set) { 60 if ( hasSet(name) ) { 61 throw new IllegalArgumentException ("Name already in use: "+name); 62 } 63 m_map.put(name, set); 64 m_sets.add(set); 65 m_count += set.getTupleCount(); 66 if ( m_lstnr != null ) 67 set.addTupleSetListener(m_lstnr); 68 } 69 70 75 public boolean hasSet(String name) { 76 return m_map.containsKey(name); 77 } 78 79 85 public boolean containsSet(TupleSet set) { 86 return m_sets.contains(set); 87 } 88 89 94 public TupleSet getSet(String name) { 95 return (TupleSet)m_map.get(name); 96 } 97 98 102 public Iterator setNames() { 103 return m_map.keySet().iterator(); 104 } 105 106 110 public Iterator sets() { 111 return m_map.values().iterator(); 112 } 113 114 119 public TupleSet removeSet(String name) { 120 TupleSet ts = (TupleSet)m_map.remove(name); 121 if ( ts != null ) { 122 m_sets.remove(ts); 123 if ( m_lstnr != null ) 124 ts.removeTupleSetListener(m_lstnr); 125 } 126 return ts; 127 } 128 129 132 public void removeAllSets() { 133 Iterator sets = m_map.entrySet().iterator(); 134 while ( sets.hasNext() ) { 135 Map.Entry entry = (Map.Entry )sets.next(); 136 TupleSet ts = (TupleSet)entry.getValue(); 137 sets.remove(); 138 m_sets.remove(ts); 139 if ( m_lstnr != null ) 140 ts.removeTupleSetListener(m_lstnr); 141 } 142 m_count = 0; 143 } 144 145 151 public void clear() { 152 Iterator sets = m_map.entrySet().iterator(); 153 while ( sets.hasNext() ) { 154 Map.Entry entry = (Map.Entry )sets.next(); 155 ((TupleSet)entry.getValue()).clear(); 156 } 157 m_count = 0; 158 } 159 160 163 167 public Tuple addTuple(Tuple t) { 168 throw new UnsupportedOperationException (); 169 } 170 171 175 public Tuple setTuple(Tuple t) { 176 throw new UnsupportedOperationException (); 177 } 178 179 184 public boolean removeTuple(Tuple t) { 185 Table table = t.getTable(); 186 if ( m_sets.contains(table) ) { 187 return table.removeTuple(t); 188 } else { 189 return false; 190 } 191 } 192 193 196 public boolean containsTuple(Tuple t) { 197 Iterator it = m_map.entrySet().iterator(); 198 while ( it.hasNext() ) { 199 Map.Entry entry = (Map.Entry )it.next(); 200 TupleSet ts = (TupleSet)entry.getValue(); 201 if ( ts.containsTuple(t) ) 202 return true; 203 } 204 return false; 205 } 206 207 210 public int getTupleCount() { 211 if ( m_lstnr != null ) { 212 return m_count; 213 } else { 214 int count = 0; 215 Iterator it = m_map.entrySet().iterator(); 216 for ( int i=0; it.hasNext(); ++i ) { 217 Map.Entry entry = (Map.Entry )it.next(); 218 TupleSet ts = (TupleSet)entry.getValue(); 219 count += ts.getTupleCount(); 220 } 221 return count; 222 } 223 } 224 225 228 public Iterator tuples() { 229 CompositeIterator ci = new CompositeIterator(m_map.size()); 230 Iterator it = m_map.entrySet().iterator(); 231 for ( int i=0; it.hasNext(); ++i ) { 232 Map.Entry entry = (Map.Entry )it.next(); 233 TupleSet ts = (TupleSet)entry.getValue(); 234 ci.setIterator(i, ts.tuples()); 235 } 236 return ci; 237 } 238 239 242 public Iterator tuples(Predicate filter) { 243 CompositeIterator ci = new CompositeIterator(m_map.size()); 244 Iterator it = m_map.entrySet().iterator(); 245 for ( int i=0; it.hasNext(); ++i ) { 246 Map.Entry entry = (Map.Entry )it.next(); 247 TupleSet ts = (TupleSet)entry.getValue(); 248 ci.setIterator(i, ts.tuples(filter)); 249 } 250 return ci; 251 } 252 253 255 259 public boolean isAddColumnSupported() { 260 return true; 261 } 262 263 268 public void addColumn(String name, Class type, Object defaultValue) { 269 Iterator it = m_map.entrySet().iterator(); 270 while ( it.hasNext() ) { 271 Map.Entry entry = (Map.Entry )it.next(); 272 TupleSet ts = (TupleSet)entry.getValue(); 273 if ( ts.isAddColumnSupported() ) { 274 try { 275 ts.addColumn(name, type, defaultValue); 276 } catch ( IllegalArgumentException iae ) { 277 } 279 } else { 280 s_logger.fine("Skipped addColumn for "+entry.getKey()); 281 } 282 } 283 } 284 285 290 public void addColumn(String name, Class type) { 291 Iterator it = m_map.entrySet().iterator(); 292 while ( it.hasNext() ) { 293 Map.Entry entry = (Map.Entry )it.next(); 294 TupleSet ts = (TupleSet)entry.getValue(); 295 if ( ts.isAddColumnSupported() ) { 296 try { 297 ts.addColumn(name, type); 298 } catch ( IllegalArgumentException iae ) { 299 } 301 } else { 302 s_logger.fine("Skipped addColumn for "+entry.getKey()); 303 } 304 } 305 } 306 307 312 public void addColumn(String name, Expression expr) { 313 Iterator it = m_map.entrySet().iterator(); 314 while ( it.hasNext() ) { 315 Map.Entry entry = (Map.Entry )it.next(); 316 TupleSet ts = (TupleSet)entry.getValue(); 317 if ( ts.isAddColumnSupported() ) { 318 try { 319 ts.addColumn(name, expr); 320 } catch ( IllegalArgumentException iae ) { 321 } 323 } else { 324 s_logger.fine("Skipped addColumn for "+entry.getKey()); 325 } 326 } 327 } 328 329 334 public void addColumn(String name, String expr) { 335 Expression ex = ExpressionParser.parse(expr); 336 Throwable t = ExpressionParser.getError(); 337 if ( t != null ) { 338 throw new RuntimeException (t); 339 } else { 340 addColumn(name, ex); 341 } 342 } 343 344 347 351 private class Listener implements TupleSetListener { 352 public void tupleSetChanged(TupleSet tset, Tuple[] add, Tuple[] rem) { 353 m_count += add.length - rem.length; 354 fireTupleEvent(add, rem); 355 } 356 } 357 358 } | Popular Tags |