KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.tuple;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.logging.Logger JavaDoc;
5
6 import prefuse.data.Graph;
7 import prefuse.data.Table;
8 import prefuse.data.Tuple;
9 import prefuse.util.StringLib;
10 import prefuse.util.collections.IntIterator;
11
12 /**
13  * Manager class for Tuples. There is a unique Tuple for each row of a table.
14  * All data structures and Tuples are created lazily, on an as-needed basis.
15  * When a row is deleted from the table, it's corresponding Tuple (if created)
16  * is invalidated before being removed from this data structure, ensuring that
17  * any other live references to the Tuple can't be used to corrupt the table.
18  *
19  * @author <a HREF="http://jheer.org">jeffrey heer</a>
20  */

21 public class TupleManager {
22
23     protected Graph m_graph;
24     protected Table m_table;
25     protected Class JavaDoc m_tupleType;
26     
27     private TableTuple[] m_tuples;
28     
29     /**
30      * Create a new TupleManager for the given Table.
31      * @param t the data Table to generate Tuples for
32      */

33     public TupleManager(Table t, Graph g, Class JavaDoc tupleType) {
34         init(t, g, tupleType);
35     }
36     
37     /**
38      * Initialize this TupleManager for use with a given Table.
39      * @param t the data Table to generate Tuples for
40      */

41     public void init(Table t, Graph g, Class JavaDoc tupleType) {
42         if ( m_table != null ) {
43             throw new IllegalStateException JavaDoc(
44                 "This TupleManager has already been initialized");
45         }
46         m_table = t;
47         m_graph = g;
48         m_tupleType = tupleType;
49         m_tuples = null;
50     }
51     
52     /**
53      * Get the type of Tuple instances to generate.
54      * @return the tuple type, as a Class instance
55      */

56     public Class JavaDoc getTupleType() {
57         return m_tupleType;
58     }
59     
60     /**
61      * Ensure the tuple array exists.
62      */

63     private void ensureTupleArray() {
64         int nrows = m_table.getRowCount();
65         if ( m_tuples == null ) {
66             m_tuples = new TableTuple[nrows];
67         } else if ( m_tuples.length < nrows ) {
68             int capacity = Math.max((3*m_tuples.length)/2 + 1, nrows);
69             TableTuple[] tuples = new TableTuple[capacity];
70             System.arraycopy(m_tuples, 0, tuples, 0, m_tuples.length);
71             m_tuples = tuples;
72         }
73     }
74     
75     /**
76      * Get a Tuple corresponding to the given row index.
77      * @param row the row index
78      * @return the Tuple corresponding to the given row
79      */

80     public Tuple getTuple(int row) {
81         if ( m_table.isValidRow(row) ) {
82             ensureTupleArray();
83             if ( m_tuples[row] == null ) {
84                 return (m_tuples[row] = newTuple(row));
85             } else {
86                 return m_tuples[row];
87             }
88         } else {
89             // TODO: return null instead?
90
throw new IllegalArgumentException JavaDoc("Invalid row index: "+row);
91         }
92     }
93     
94     /**
95      * Instantiate a new Tuple instance for the given row index.
96      * @param row the row index of the tuple
97      * @return the newly created Tuple
98      */

99     protected TableTuple newTuple(int row) {
100         try {
101             TableTuple t = (TableTuple)m_tupleType.newInstance();
102             t.init(m_table, m_graph, row);
103             return t;
104         } catch ( Exception JavaDoc e ) {
105             Logger.getLogger(getClass().getName()).warning(
106                 e.getMessage()+"\n"+StringLib.getStackTrace(e));
107             return null;
108         }
109     }
110     
111     /**
112      * Invalidate the tuple at the given row.
113      * @param row the row index to invalidate
114      */

115     public void invalidate(int row) {
116         if ( m_tuples == null || row < 0 || row >= m_tuples.length ) {
117             return;
118         } else if ( m_tuples[row] != null ) {
119             m_tuples[row].invalidate();
120             m_tuples[row] = null;
121         }
122     }
123     
124     /**
125      * Invalidate all tuples managed by this TupleManager
126      */

127     public void invalidateAll() {
128         if ( m_tuples == null ) return;
129         for ( int i=0; i<m_tuples.length; ++i )
130             invalidate(i);
131     }
132     
133     /**
134      * Return an iterator over the tuples in this manager.
135      * @param rows an iterator over table rows
136      * @return an iterator over the tuples indicated by the input row iterator
137      */

138     public Iterator JavaDoc iterator(IntIterator rows) {
139         return new TupleManagerIterator(this, rows);
140     }
141     
142     // ------------------------------------------------------------------------
143
// TupleManagerIterator
144

145     /**
146      * Iterator instance for iterating over tuples managed in a TupleManager.
147      */

148     public class TupleManagerIterator implements Iterator JavaDoc {
149
150         private TupleManager m_tuples;
151         private IntIterator m_rows;
152         
153         /**
154          * Create a new TupleManagerIterator.
155          * @param tuples the TupleManager from which to get the tuples
156          * @param rows the rows to iterate over
157          */

158         public TupleManagerIterator(TupleManager tuples, IntIterator rows) {
159             m_tuples = tuples;
160             m_rows = rows;
161         }
162         
163         /**
164          * @see java.util.Iterator#hasNext()
165          */

166         public boolean hasNext() {
167             return m_rows.hasNext();
168         }
169
170         /**
171          * @see java.util.Iterator#next()
172          */

173         public Object JavaDoc next() {
174             return m_tuples.getTuple(m_rows.nextInt());
175         }
176
177         /**
178          * @see java.util.Iterator#remove()
179          */

180         public void remove() {
181             // TODO: check to see if this is safe
182
m_rows.remove();
183         }
184
185     } // end of inner class TupleManagerIterator
186

187 } // end of class TupleManager
188
Popular Tags