KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > util > RowManager


1 package prefuse.data.util;
2
3 import prefuse.data.Table;
4 import prefuse.util.collections.IntIntSortedMap;
5 import prefuse.util.collections.IntIntTreeMap;
6 import prefuse.util.collections.IntIterator;
7
8
9 /**
10  * Manages the set of valid rows for a Table instance, maintains an index of
11  * the available and occupied rows. RowManager instances are used internally
12  * by Table instances.
13  *
14  * @author <a HREF="http://jheer.org">jeffrey heer</a>
15  */

16 public class RowManager {
17
18     protected Table m_table;
19     private IntIntSortedMap m_openrows;
20     private int m_firstid = 0;
21     private int m_curid = -1;
22     
23     // ------------------------------------------------------------------------
24
// Constructor
25

26     /**
27      * Create a new RowManager for the given Table.
28      * @param table the Table to manage
29      */

30     public RowManager(Table table) {
31         m_table = table;
32     }
33     
34     /**
35      * Get the table managed by this RowManager.
36      * @return the managed table
37      */

38     public Table getTable() {
39         return m_table;
40     }
41     
42     // ------------------------------------------------------------------------
43
// Row Information Methods
44

45     /**
46      * Get the lowest-numbered occupied table row.
47      * @return the minimum row
48      */

49     public int getMinimumRow() {
50         return m_firstid;
51     }
52
53     /**
54      * Get the highest-numbered occupied table row.
55      * @return the maximum row
56      */

57     public int getMaximumRow() {
58         return m_curid;
59     }
60     
61     /**
62      * Get the total number of occupied rows
63      * @return the number of rows being used by the table
64      */

65     public int getRowCount() {
66         return 1 + m_curid - m_firstid
67             - (m_openrows==null ? 0 : m_openrows.size());
68     }
69     
70     /**
71      * Indicates if a given row value is a valid, occupied row of the table.
72      * @param row the row index to check
73      * @return true if the row is valid and in use by the Table, false if
74      * it is an illegal value or is currently free
75      */

76     public boolean isValidRow(int row) {
77         return ( row >= m_firstid && row <=m_curid &&
78                 (m_openrows == null || !m_openrows.containsKey(row)) );
79     }
80     
81     // ------------------------------------------------------------------------
82
// Row Update Methods
83

84     /**
85      * Clear the row manager status, marking all rows as available.
86      */

87     public void clear() {
88         m_openrows = null;
89         m_firstid = 0;
90         m_curid = -1;
91     }
92     
93     /**
94      * Add a new row to management. The lowest valued available row
95      * will be used.
96      * @return the row index of the newly added row
97      */

98     public int addRow() {
99         int r;
100         if ( m_openrows == null || m_openrows.isEmpty() ) {
101             r = ( m_firstid == 0 ? ++m_curid : --m_firstid );
102         } else {
103             int key = m_openrows.firstKey();
104             r = m_openrows.remove(key);
105         }
106         return r;
107     }
108     
109     /**
110      * Release a row and mark it as free.
111      * @param row the row index of the released row
112      * @return true if the row was successfully released, false if it
113      * was already free or if the input is not a valid row index
114      */

115     public boolean releaseRow(int row) {
116         if ( row < 0 ) {
117             return false;
118         } else if ( m_openrows != null && m_openrows.containsKey(row) ) {
119             return false;
120         } else if ( row == m_curid ) {
121             --m_curid;
122         } else if ( row == m_firstid ) {
123             ++m_firstid;
124         } else {
125             if ( m_openrows == null )
126                 m_openrows = new IntIntTreeMap(false);
127             m_openrows.put(row, row);
128         }
129         return true;
130     }
131     
132     // ------------------------------------------------------------------------
133
// Column Mapping
134

135     /**
136      * Given Table row and column indices, return the corresponding row in
137      * the underlying data column. This is of use for CascadedTable instances,
138      * which may reveal only a limited set of a parent table's rows and so
139      * must map between table rows and the actual indices of the inherited
140      * data columns.
141      * @param row the table row
142      * @param col the table column
143      * @return the row value for accessing the correct value of the
144      * referenced data column.
145      */

146     public int getColumnRow(int row, int col) {
147         return this.isValidRow(row) ? row : -1;
148     }
149     
150     /**
151      * Given a column row index and a table column index, return the
152      * table row corresponding to the column value. This is of use for
153      * CascadedTable instances, which may reveal only a limited set of a parent
154      * table's rows and so must map between table rows and the actual indices
155      * of the inherited data columns.
156      * @param columnRow the row of the underlying data column
157      * @param col the table column
158      * @return the row value for the Table that corresponds to the
159      * given column row
160      */

161     public int getTableRow(int columnRow, int col) {
162         return this.isValidRow(columnRow) ? columnRow : -1;
163     }
164
165     /**
166      * Return an iterator over column row indices.
167      * @param col the table column index
168      * @return an iterator over column row indices corresponding
169      * to valid rows of this RowManager
170      */

171     public IntIterator columnRows(int col) {
172         return new ColumnRowIterator(rows(), col);
173     }
174
175     /**
176      * Return an iterator over column row indices.
177      * @param col the table column index
178      * @param reverse indicates the direction to iterate over, true
179      * for reverse, false for normal
180      * @return an iterator over column row indices corresponding
181      * to valid rows of this RowManager
182      */

183     public IntIterator columnRows(int col, boolean reverse) {
184         return new ColumnRowIterator(rows(reverse), col);
185     }
186     
187     /**
188      * Return an iterator over column row indices.
189      * @param rows an iterator over table row indices
190      * @param col the table column index
191      * @return an iterator over column row indices corresponding
192      * to valid rows of this RowManager
193      */

194     public IntIterator columnRows(IntIterator rows, int col) {
195         return new ColumnRowIterator(rows, col);
196     }
197     
198     // ------------------------------------------------------------------------
199
// Iterators
200

201     /**
202      * Get an iterator over the table rows.
203      * @return an iterator over the table rows
204      */

205     public IntIterator rows() {
206         return new RowIterator(false);
207     }
208     
209     /**
210      * Get an iterator over the table rows.
211      * @param reverse indicates the direction to iterate over, true
212      * for reverse, false for normal
213      * @return an iterator over the table rows
214      */

215     public IntIterator rows(boolean reverse) {
216         return new RowIterator(reverse);
217     }
218     
219     /**
220      * Iterator over the occupied rows of this RowManager.
221      */

222     public class RowIterator extends IntIterator {
223         boolean reverse;
224         int last = -1, next;
225
226         public RowIterator(boolean reverse) {
227             this.reverse = reverse;
228             next = advance(reverse ? m_curid : m_firstid);
229         }
230         public boolean hasNext() {
231             return ( reverse ? next >= 0 : next <= m_curid );
232         }
233         public int nextInt() {
234             // advance the iterator
235
last = next;
236             next = advance(reverse ? --next : ++next);
237             return last;
238         }
239         public void remove() {
240             m_table.removeRow(last);
241         }
242         private final int advance(int idx) {
243             if ( m_openrows == null )
244                 return idx;
245             else if ( reverse )
246                 for (; idx >= 0 && m_openrows.containsKey(idx); --idx);
247             else
248                 for (; idx <= m_curid && m_openrows.containsKey(idx); ++idx);
249             return idx;
250         }
251     } // end of inner class RowIterator
252

253     /**
254      * Iterator over the indices into a given data column,
255      * mapped to from the rows of this RowManager.
256      */

257     public class ColumnRowIterator extends IntIterator {
258         private IntIterator rows;
259         private int row;
260         private int col;
261         public ColumnRowIterator(IntIterator rows, int col) {
262             this.rows = rows;
263             this.col = col;
264         }
265         public boolean hasNext() {
266             return rows.hasNext();
267         }
268         public int nextInt() {
269             row = rows.nextInt();
270             return getColumnRow(row, col);
271         }
272         public void remove() {
273             m_table.removeRow(row);
274         }
275     } // end of inner class ColumnRowIterator
276

277 } // end of class RowManager
278
Popular Tags