1 package prefuse.data.tuple; 2 3 import java.util.Iterator ; 4 import java.util.LinkedHashSet ; 5 6 import prefuse.data.Tuple; 7 import prefuse.data.event.EventConstants; 8 9 21 public class DefaultTupleSet extends AbstractTupleSet implements EventConstants 22 { 23 protected LinkedHashSet m_tuples; 24 25 28 public DefaultTupleSet() { 29 m_tuples = new LinkedHashSet (); 30 } 31 32 35 public int getTupleCount() { 36 return m_tuples.size(); 37 } 38 39 42 public Tuple addTuple(Tuple t) { 43 t = addInternal(t); 44 if ( t != null ) 45 fireTupleEvent(t, INSERT); 46 return t; 47 } 48 49 52 public Tuple setTuple(Tuple t) { 53 Tuple[] rem = clearInternal(); 54 t = addInternal(t); 55 Tuple[] add = t==null ? null : new Tuple[] {t}; 56 fireTupleEvent(add, rem); 57 return t; 58 } 59 60 65 protected final Tuple addInternal(Tuple t) { 66 if ( m_tuples.add(t) ) { 67 return t; 68 } else { 69 return null; 70 } 71 } 72 73 76 public boolean containsTuple(Tuple t) { 77 return m_tuples.contains(t); 78 } 79 80 83 public boolean removeTuple(Tuple t) { 84 boolean b = removeInternal(t); 85 if ( b ) 86 fireTupleEvent(t, DELETE); 87 return b; 88 } 89 90 95 protected final boolean removeInternal(Tuple t) { 96 return ( m_tuples.remove(t) ); 97 } 98 99 102 public void clear() { 103 if ( getTupleCount() > 0 ) { 104 Tuple[] t = clearInternal(); 105 fireTupleEvent(null, t); 106 } 107 } 108 109 113 public Tuple[] clearInternal() { 114 Tuple[] t = new Tuple[getTupleCount()]; 115 Iterator iter = tuples(); 116 for ( int i=0; iter.hasNext(); ++i ) { 117 t[i] = (Tuple)iter.next(); 118 } 119 m_tuples.clear(); 120 return t; 121 } 122 123 126 public Iterator tuples() { 127 return m_tuples.iterator(); 128 } 129 130 134 public Tuple[] toArray() { 135 Tuple[] t = new Tuple[getTupleCount()]; 136 m_tuples.toArray(t); 137 return t; 138 } 139 140 } | Popular Tags |