KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.tuple;
2
3 import java.util.HashSet JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.LinkedHashMap JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Set JavaDoc;
8 import java.util.logging.Logger JavaDoc;
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 /**
19  * <p>TupleSet implementation for treating a collection of tuple sets
20  * as a single, composite tuple set. This composite does not take
21  * overlap between contained TupleSets into account.</p>
22  *
23  * <p>The {@link TupleSet#addTuple(Tuple)} and {@link #setTuple(Tuple)}
24  * methods are not supported by this class, and calling these methods will
25  * result in a UnsupportedOperationException. Instead, use the add or set
26  * methods on the desired non-composite tuple set.</p>
27  *
28  * @author <a HREF="http://jheer.org">jeffrey heer</a>
29  */

30 public class CompositeTupleSet extends AbstractTupleSet {
31
32     private static final Logger JavaDoc s_logger
33         = Logger.getLogger(CompositeTupleSet.class.getName());
34     
35     private Map JavaDoc m_map; // map names to tuple sets
36
private Set JavaDoc m_sets; // support quick reverse lookup
37
private int m_count; // count of total tuples
38
private Listener m_lstnr;
39     
40     /**
41      * Create a new, empty CompositeTupleSet
42      */

43     public CompositeTupleSet() {
44         this(true);
45     }
46     
47     protected CompositeTupleSet(boolean listen) {
48         m_map = new LinkedHashMap JavaDoc();
49         m_sets = new HashSet JavaDoc();
50         m_count = 0;
51         m_lstnr = listen ? new Listener() : null;
52     }
53     
54     /**
55      * Add a TupleSet to this composite.
56      * @param name the name of the TupleSet to add
57      * @param set the set to add
58      */

59     public void addSet(String JavaDoc name, TupleSet set) {
60         if ( hasSet(name) ) {
61             throw new IllegalArgumentException JavaDoc("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     /**
71      * Indicates if this composite contains a TupleSet with the given name.
72      * @param name the name to look for
73      * @return true if a TupleSet with the given name is found, false otherwise
74      */

75     public boolean hasSet(String JavaDoc name) {
76         return m_map.containsKey(name);
77     }
78     
79     /**
80      * Indicates if this composite contains the given TupleSet.
81      * @param set the TupleSet to check for containment
82      * @return true if the TupleSet is contained in this composite,
83      * false otherwise
84      */

85     public boolean containsSet(TupleSet set) {
86         return m_sets.contains(set);
87     }
88     
89     /**
90      * Get the TupleSet associated with the given name.
91      * @param name the name of the TupleSet to get
92      * @return the associated TupleSet, or null if not found
93      */

94     public TupleSet getSet(String JavaDoc name) {
95         return (TupleSet)m_map.get(name);
96     }
97     
98     /**
99      * Get an iterator over the names of all the TupleSets in this composite.
100      * @return the iterator over contained set names.
101      */

102     public Iterator JavaDoc setNames() {
103         return m_map.keySet().iterator();
104     }
105
106     /**
107      * Get an iterator over all the TupleSets in this composite.
108      * @return the iterator contained sets.
109      */

110     public Iterator JavaDoc sets() {
111         return m_map.values().iterator();
112     }
113     
114     /**
115      * Remove the TupleSet with the given name from this composite.
116      * @param name the name of the TupleSet to remove
117      * @return the removed TupleSet, or null if not found
118      */

119     public TupleSet removeSet(String JavaDoc 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     /**
130      * Remove all contained TupleSets from this composite.
131      */

132     public void removeAllSets() {
133         Iterator JavaDoc sets = m_map.entrySet().iterator();
134         while ( sets.hasNext() ) {
135             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)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     /**
146      * Clear this TupleSet, calling clear on all contained TupleSet
147      * instances. All contained TupleSets remain members of this
148      * composite, but they have their data cleared.
149      * @see prefuse.data.tuple.TupleSet#clear()
150      */

151     public void clear() {
152         Iterator JavaDoc sets = m_map.entrySet().iterator();
153         while ( sets.hasNext() ) {
154             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)sets.next();
155             ((TupleSet)entry.getValue()).clear();
156         }
157         m_count = 0;
158     }
159     
160     // ------------------------------------------------------------------------
161
// TupleSet Interface
162

163     /**
164      * Not supported.
165      * @see prefuse.data.tuple.TupleSet#addTuple(prefuse.data.Tuple)
166      */

167     public Tuple addTuple(Tuple t) {
168         throw new UnsupportedOperationException JavaDoc();
169     }
170
171     /**
172      * Not supported.
173      * @see prefuse.data.tuple.TupleSet#setTuple(prefuse.data.Tuple)
174      */

175     public Tuple setTuple(Tuple t) {
176         throw new UnsupportedOperationException JavaDoc();
177     }
178
179     /**
180      * Removes the tuple from its source set if that source set is contained
181      * within this composite.
182      * @see prefuse.data.tuple.TupleSet#removeTuple(prefuse.data.Tuple)
183      */

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

196     public boolean containsTuple(Tuple t) {
197         Iterator JavaDoc it = m_map.entrySet().iterator();
198         while ( it.hasNext() ) {
199             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
200             TupleSet ts = (TupleSet)entry.getValue();
201             if ( ts.containsTuple(t) )
202                 return true;
203         }
204         return false;
205     }
206
207     /**
208      * @see prefuse.data.tuple.TupleSet#getTupleCount()
209      */

210     public int getTupleCount() {
211         if ( m_lstnr != null ) {
212             return m_count;
213         } else {
214             int count = 0;
215             Iterator JavaDoc it = m_map.entrySet().iterator();
216             for ( int i=0; it.hasNext(); ++i ) {
217                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
218                 TupleSet ts = (TupleSet)entry.getValue();
219                 count += ts.getTupleCount();
220             }
221             return count;
222         }
223     }
224
225     /**
226      * @see prefuse.data.tuple.TupleSet#tuples()
227      */

228     public Iterator JavaDoc tuples() {
229         CompositeIterator ci = new CompositeIterator(m_map.size());
230         Iterator JavaDoc it = m_map.entrySet().iterator();
231         for ( int i=0; it.hasNext(); ++i ) {
232             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
233             TupleSet ts = (TupleSet)entry.getValue();
234             ci.setIterator(i, ts.tuples());
235         }
236         return ci;
237     }
238     
239     /**
240      * @see prefuse.data.tuple.TupleSet#tuples(prefuse.data.expression.Predicate)
241      */

242     public Iterator JavaDoc tuples(Predicate filter) {
243         CompositeIterator ci = new CompositeIterator(m_map.size());
244         Iterator JavaDoc it = m_map.entrySet().iterator();
245         for ( int i=0; it.hasNext(); ++i ) {
246             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
247             TupleSet ts = (TupleSet)entry.getValue();
248             ci.setIterator(i, ts.tuples(filter));
249         }
250         return ci;
251     }
252     
253     // -- Data Field Methods --------------------------------------------------
254

255     /**
256      * Returns true.
257      * @see prefuse.data.tuple.TupleSet#isAddColumnSupported()
258      */

259     public boolean isAddColumnSupported() {
260         return true;
261     }
262
263     /**
264      * Adds the value to all contained TupleSets that return a true value for
265      * {@link TupleSet#isAddColumnSupported()}.
266      * @see prefuse.data.tuple.TupleSet#addColumn(java.lang.String, java.lang.Class, java.lang.Object)
267      */

268     public void addColumn(String JavaDoc name, Class JavaDoc type, Object JavaDoc defaultValue) {
269         Iterator JavaDoc it = m_map.entrySet().iterator();
270         while ( it.hasNext() ) {
271             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
272             TupleSet ts = (TupleSet)entry.getValue();
273             if ( ts.isAddColumnSupported() ) {
274                 try {
275                     ts.addColumn(name, type, defaultValue);
276                 } catch ( IllegalArgumentException JavaDoc iae ) {
277                     // already exists
278
}
279             } else {
280                 s_logger.fine("Skipped addColumn for "+entry.getKey());
281             }
282         }
283     }
284
285     /**
286      * Adds the value to all contained TupleSets that return a true value for
287      * {@link TupleSet#isAddColumnSupported()}.
288      * @see prefuse.data.tuple.TupleSet#addColumn(java.lang.String, java.lang.Class)
289      */

290     public void addColumn(String JavaDoc name, Class JavaDoc type) {
291         Iterator JavaDoc it = m_map.entrySet().iterator();
292         while ( it.hasNext() ) {
293             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
294             TupleSet ts = (TupleSet)entry.getValue();
295             if ( ts.isAddColumnSupported() ) {
296                 try {
297                     ts.addColumn(name, type);
298                 } catch ( IllegalArgumentException JavaDoc iae ) {
299                     // already exists
300
}
301             } else {
302                 s_logger.fine("Skipped addColumn for "+entry.getKey());
303             }
304         }
305     }
306
307     /**
308      * Adds the value to all contained TupleSets that return a true value for
309      * {@link TupleSet#isAddColumnSupported()}.
310      * @see prefuse.data.tuple.TupleSet#addColumn(java.lang.String, prefuse.data.expression.Expression)
311      */

312     public void addColumn(String JavaDoc name, Expression expr) {
313         Iterator JavaDoc it = m_map.entrySet().iterator();
314         while ( it.hasNext() ) {
315             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
316             TupleSet ts = (TupleSet)entry.getValue();
317             if ( ts.isAddColumnSupported() ) {
318                 try {
319                     ts.addColumn(name, expr);
320                 } catch ( IllegalArgumentException JavaDoc iae ) {
321                     // already exists
322
}
323             } else {
324                 s_logger.fine("Skipped addColumn for "+entry.getKey());
325             }
326         }
327     }
328
329     /**
330      * Adds the value to all contained TupleSets that return a true value for
331      * {@link TupleSet#isAddColumnSupported()}.
332      * @see prefuse.data.tuple.TupleSet#addColumn(java.lang.String, java.lang.String)
333      */

334     public void addColumn(String JavaDoc name, String JavaDoc expr) {
335         Expression ex = ExpressionParser.parse(expr);
336         Throwable JavaDoc t = ExpressionParser.getError();
337         if ( t != null ) {
338             throw new RuntimeException JavaDoc(t);
339         } else {
340             addColumn(name, ex);
341         }
342     }
343     
344     // ------------------------------------------------------------------------
345
// Internal TupleSet Listener
346

347     /**
348      * Listener that relays tuple set change events as they occur and updates
349      * the total tuple count appropriately.
350      */

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 } // end of class CompositeTupleSet
359
Popular Tags