KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > tuple > DefaultTupleSet


1 package prefuse.data.tuple;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.LinkedHashSet JavaDoc;
5
6 import prefuse.data.Tuple;
7 import prefuse.data.event.EventConstants;
8
9 /**
10  * <p>TupleSet implementation that maintains a set of heterogeneous Tuples
11  * -- tuples that can come from any backing data source. This class supports
12  * {@link #addTuple(Tuple)} and {@link #removeTuple(Tuple)} but does not
13  * support adding new columns to contained tuples.</p>
14  *
15  * <p>This TupleSet uses a {@link java.util.LinkedHashSet} to support fast
16  * lookup of contained tuples while mainting Tuples in the order in which
17  * they are added to the set.</p>
18  *
19  * @author <a HREF="http://jheer.org">jeffrey heer</a>
20  */

21 public class DefaultTupleSet extends AbstractTupleSet implements EventConstants
22 {
23     protected LinkedHashSet JavaDoc m_tuples;
24
25     /**
26      * Create a new, empty DefaultTupleSet.
27      */

28     public DefaultTupleSet() {
29         m_tuples = new LinkedHashSet JavaDoc();
30     }
31     
32     /**
33      * @see prefuse.data.tuple.TupleSet#getTupleCount()
34      */

35     public int getTupleCount() {
36         return m_tuples.size();
37     }
38     
39     /**
40      * @see prefuse.data.tuple.TupleSet#addTuple(prefuse.data.Tuple)
41      */

42     public Tuple addTuple(Tuple t) {
43         t = addInternal(t);
44         if ( t != null )
45             fireTupleEvent(t, INSERT);
46         return t;
47     }
48     
49     /**
50      * @see prefuse.data.tuple.TupleSet#setTuple(prefuse.data.Tuple)
51      */

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     /**
61      * Adds a tuple without firing a notification.
62      * @param t the Tuple to add
63      * @return the added Tuple
64      */

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     /**
74      * @see prefuse.data.tuple.TupleSet#containsTuple(prefuse.data.Tuple)
75      */

76     public boolean containsTuple(Tuple t) {
77         return m_tuples.contains(t);
78     }
79     
80     /**
81      * @see prefuse.data.tuple.TupleSet#removeTuple(prefuse.data.Tuple)
82      */

83     public boolean removeTuple(Tuple t) {
84         boolean b = removeInternal(t);
85         if ( b )
86             fireTupleEvent(t, DELETE);
87         return b;
88     }
89     
90     /**
91      * Removes a tuple without firing a notification.
92      * @param t the tuple to remove
93      * @return true if the tuple is removed successfully, false otherwise
94      */

95     protected final boolean removeInternal(Tuple t) {
96         return ( m_tuples.remove(t) );
97     }
98     
99     /**
100      * @see prefuse.data.tuple.TupleSet#clear()
101      */

102     public void clear() {
103         if ( getTupleCount() > 0 ) {
104             Tuple[] t = clearInternal();
105             fireTupleEvent(null, t);
106         }
107     }
108     
109     /**
110      * Clear the internal state without firing a notification.
111      * @return an array of removed tuples
112      */

113     public Tuple[] clearInternal() {
114         Tuple[] t = new Tuple[getTupleCount()];
115         Iterator JavaDoc 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     /**
124      * @see prefuse.data.tuple.TupleSet#tuples()
125      */

126     public Iterator JavaDoc tuples() {
127         return m_tuples.iterator();
128     }
129     
130     /**
131      * Get the contents of this TupleSet as an array.
132      * @return the contents of this TupleSet as an array
133      */

134     public Tuple[] toArray() {
135         Tuple[] t = new Tuple[getTupleCount()];
136         m_tuples.toArray(t);
137         return t;
138     }
139     
140 } // end of class DefaultTupleSet
141
Popular Tags