KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.util;
2
3 import java.util.ConcurrentModificationException JavaDoc;
4 import java.util.Date JavaDoc;
5
6 import prefuse.data.Table;
7 import prefuse.util.collections.IntIterator;
8
9 /**
10  * An iterator over table rows, providing convenience methods for accessing and
11  * manipulating table data.
12  *
13  * @author <a HREF="http://jheer.org">jeffrey heer</a>
14  */

15 public class TableIterator extends IntIterator {
16
17     private Table m_table;
18     private IntIterator m_rows;
19     private int m_modCount;
20     
21     protected int m_cur = -1;
22
23     /**
24      * Create a new TableIterator using a given iterator over table rows.
25      * @param table the Table to iterate over
26      * @param rows the iteration over individual table rows
27      */

28     public TableIterator(Table table, IntIterator rows) {
29         m_table = table;
30         m_rows = rows;
31         m_modCount = table.getModificationCount();
32     }
33
34     // ------------------------------------------------------------------------
35
// Iterator Methods
36

37     /**
38      * Returns the next table row.
39      * @see prefuse.util.collections.LiteralIterator#nextInt()
40      */

41     public int nextInt() {
42         if ( m_modCount != m_table.getModificationCount() )
43             throw new ConcurrentModificationException JavaDoc();
44         m_cur = m_rows.nextInt();
45         return m_cur;
46     }
47
48     /**
49      * @see java.util.Iterator#hasNext()
50      */

51     public boolean hasNext() {
52         return m_rows.hasNext();
53     }
54
55     /**
56      * Remove the current row, deleting it from the table.
57      * @see java.util.Iterator#remove()
58      */

59     public void remove() {
60         if ( m_table.removeRow(m_cur) )
61             modify();
62     }
63     
64     /**
65      * Tracks table modifications.
66      */

67     protected void modify() {
68         ++m_modCount;
69         m_cur = -1;
70     }
71     
72     // ------------------------------------------------------------------------
73
// Data Access Methods
74

75     /**
76      * Check if the <code>get</code> method for the given data field returns
77      * values that are compatible with a given target type.
78      * @param field the data field to check
79      * @param type a Class instance to check for compatibility with the
80      * data field values.
81      * @return true if the data field is compatible with provided type,
82      * false otherwise. If the value is true, objects returned by
83      * the {@link #get(String)} can be cast to the given type.
84      * @see #get(String)
85      */

86     public final boolean canGet(String JavaDoc field, Class JavaDoc type) {
87         return m_table.canGet(field, type);
88     }
89     
90     /**
91      * Check if the <code>set</code> method for the given data field can
92      * accept values of a given target type.
93      * @param field the data field to check
94      * @param type a Class instance to check for compatibility with the
95      * data field values.
96      * @return true if the data field is compatible with provided type,
97      * false otherwise. If the value is true, objects of the given type
98      * can be used as parameters of the {@link #set(String, Object)} method.
99      * @see #set(String, Object)
100      */

101     public final boolean canSet(String JavaDoc field, Class JavaDoc type) {
102         return m_table.canSet(field, type);
103     }
104     
105     /**
106      * Get the data value at the given field as an Object.
107      * @param field the data field to retrieve
108      * @return the data value as an Object. The concrete type of this
109      * Object is dependent on the underlying data column used.
110      * @see #canGet(String, Class)
111      * @see prefuse.data.Table#getColumnType(String)
112      */

113     public final Object JavaDoc get(String JavaDoc field) {
114         return m_table.get(m_cur, field);
115     }
116     
117     /**
118      * Set the value of a given data field.
119      * @param field the data field to set
120      * @param val the value for the field. If the concrete type of this
121      * Object is not compatible with the underlying data model, an
122      * Exception will be thrown. Use the {@link #canSet(String, Class)}
123      * method to check the type-safety ahead of time.
124      * @see #canSet(String, Class)
125      * @see prefuse.data.Table#getColumnType(String)
126      */

127     public final void set(String JavaDoc field, Object JavaDoc val) {
128         ++m_modCount;
129         m_table.set(m_cur, field, val);
130     }
131     
132     // ------------------------------------------------------------------------
133
// Convenience Data Access Methods
134

135     /**
136      * Check if the given data field can return primitive <code>int</code>
137      * values.
138      * @param field the data field to check
139      * @return true if the data field can return primitive <code>int</code>
140      * values, false otherwise. If true, the {@link #getInt(String)} method
141      * can be used safely.
142      */

143     public final boolean canGetInt(String JavaDoc field) {
144         return m_table.canGetInt(field);
145     }
146     
147     /**
148      * Check if the <code>setInt</code> method can safely be used for the
149      * given data field.
150      * @param field the data field to check
151      * @return true if the {@link #setInt(String, int)} method can safely
152      * be used for the given field, false otherwise.
153      */

154     public final boolean canSetInt(String JavaDoc field) {
155         return m_table.canSetInt(field);
156     }
157     
158     /**
159      * Get the data value at the given field as an <code>int</code>.
160      * @param field the data field to retrieve
161      * @see #canGetInt(String)
162      */

163     public final int getInt(String JavaDoc field) {
164         return m_table.getInt(m_cur, field);
165     }
166     
167     /**
168      * Set the data value of the given field with an <code>int</code>.
169      * @param field the data field to set
170      * @param val the value to set
171      * @see #canSetInt(String)
172      */

173     public final void setInt(String JavaDoc field, int val) {
174         ++m_modCount;
175         m_table.setInt(m_cur, field, val);
176     }
177     
178     // --------------------------------------------------------------
179

180     /**
181      * Check if the given data field can return primitive <code>long</code>
182      * values.
183      * @param field the data field to check
184      * @return true if the data field can return primitive <code>long</code>
185      * values, false otherwise. If true, the {@link #getLong(String)} method
186      * can be used safely.
187      */

188     public final boolean canGetLong(String JavaDoc field) {
189         return m_table.canGetLong(field);
190     }
191     
192     /**
193      * Check if the <code>setLong</code> method can safely be used for the
194      * given data field.
195      * @param field the data field to check
196      * @return true if the {@link #setLong(String, long)} method can safely
197      * be used for the given field, false otherwise.
198      */

199     public final boolean canSetLong(String JavaDoc field) {
200         return m_table.canSetLong(field);
201     }
202     
203     /**
204      * Get the data value at the given field as a <code>long</code>.
205      * @param field the data field to retrieve
206      * @see #canGetLong(String)
207      */

208     public final long getLong(String JavaDoc field) {
209         return m_table.getLong(m_cur, field);
210     }
211     
212     /**
213      * Set the data value of the given field with a <code>long</code>.
214      * @param field the data field to set
215      * @param val the value to set
216      * @see #canSetLong(String)
217      */

218     public final void setLong(String JavaDoc field, long val) {
219         ++m_modCount;
220         m_table.setLong(m_cur, field, val);
221     }
222
223     // --------------------------------------------------------------
224

225     /**
226      * Check if the given data field can return primitive <code>float</code>
227      * values.
228      * @param field the data field to check
229      * @return true if the data field can return primitive <code>float</code>
230      * values, false otherwise. If true, the {@link #getFloat(String)} method
231      * can be used safely.
232      */

233     public final boolean canGetFloat(String JavaDoc field) {
234         return m_table.canGetFloat(field);
235     }
236     
237     /**
238      * Check if the <code>setFloat</code> method can safely be used for the
239      * given data field.
240      * @param field the data field to check
241      * @return true if the {@link #setFloat(String, float)} method can safely
242      * be used for the given field, false otherwise.
243      */

244     public final boolean canSetFloat(String JavaDoc field) {
245         return m_table.canSetFloat(field);
246     }
247     
248     /**
249      * Get the data value at the given field as a <code>float</code>.
250      * @param field the data field to retrieve
251      * @see #canGetFloat(String)
252      */

253     public final float getFloat(String JavaDoc field) {
254         return m_table.getFloat(m_cur, field);
255     }
256     
257     /**
258      * Set the data value of the given field with a <code>float</code>.
259      * @param field the data field to set
260      * @param val the value to set
261      * @see #canSetFloat(String)
262      */

263     public final void setFloat(String JavaDoc field, float val) {
264         ++m_modCount;
265         m_table.setFloat(m_cur, field, val);
266     }
267     
268     // --------------------------------------------------------------
269

270     /**
271      * Check if the given data field can return primitive <code>double</code>
272      * values.
273      * @param field the data field to check
274      * @return true if the data field can return primitive <code>double</code>
275      * values, false otherwise. If true, the {@link #getDouble(String)} method
276      * can be used safely.
277      */

278     public final boolean canGetDouble(String JavaDoc field) {
279         return m_table.canGetDouble(field);
280     }
281     
282     /**
283      * Check if the <code>setDouble</code> method can safely be used for the
284      * given data field.
285      * @param field the data field to check
286      * @return true if the {@link #setDouble(String, double)} method can safely
287      * be used for the given field, false otherwise.
288      */

289     public final boolean canSetDouble(String JavaDoc field) {
290         return m_table.canSetDouble(field);
291     }
292     
293     /**
294      * Get the data value at the given field as a <code>double</code>.
295      * @param field the data field to retrieve
296      * @see #canGetDouble(String)
297      */

298     public final double getDouble(String JavaDoc field) {
299         return m_table.getDouble(m_cur, field);
300     }
301     
302     /**
303      * Set the data value of the given field with a <code>double</code>.
304      * @param field the data field to set
305      * @param val the value to set
306      * @see #canSetDouble(String)
307      */

308     public final void setDouble(String JavaDoc field, double val) {
309         ++m_modCount;
310         m_table.setDouble(m_cur, field, val);
311     }
312     
313     // --------------------------------------------------------------
314

315     /**
316      * Check if the given data field can return primitive <code>boolean</code>
317      * values.
318      * @param field the data field to check
319      * @return true if the data field can return primitive <code>boolean</code>
320      * values, false otherwise. If true, the {@link #getBoolean(String)} method
321      * can be used safely.
322      */

323     public final boolean canGetBoolean(String JavaDoc field) {
324         return m_table.canGetBoolean(field);
325     }
326     
327     /**
328      * Check if the <code>setBoolean</code> method can safely be used for the
329      * given data field.
330      * @param field the data field to check
331      * @return true if the {@link #setBoolean(String, boolean)} method can
332      * safely be used for the given field, false otherwise.
333      */

334     public final boolean canSetBoolean(String JavaDoc field) {
335         return m_table.canSetBoolean(field);
336     }
337     
338     /**
339      * Get the data value at the given field as a <code>boolean</code>.
340      * @param field the data field to retrieve
341      * @see #canGetBoolean(String)
342      */

343     public final boolean getBoolean(String JavaDoc field) {
344         return m_table.getBoolean(m_cur, field);
345     }
346     
347     /**
348      * Set the data value of the given field with a <code>boolean</code>.
349      * @param field the data field to set
350      * @param val the value to set
351      * @see #canSetBoolean(String)
352      */

353     public final void setBoolean(String JavaDoc field, boolean val) {
354         ++m_modCount;
355         m_table.setBoolean(m_cur, field, val);
356     }
357     
358     // --------------------------------------------------------------
359

360     /**
361      * Check if the given data field can return <code>String</code>
362      * values.
363      * @param field the data field to check
364      * @return true if the data field can return <code>String</code>
365      * values, false otherwise. If true, the {@link #getString(String)} method
366      * can be used safely.
367      */

368     public final boolean canGetString(String JavaDoc field) {
369         return m_table.canGetString(field);
370     }
371     
372     /**
373      * Check if the <code>setString</code> method can safely be used for the
374      * given data field.
375      * @param field the data field to check
376      * @return true if the {@link #setString(String, String)} method can safely
377      * be used for the given field, false otherwise.
378      */

379     public final boolean canSetString(String JavaDoc field) {
380         return m_table.canSetString(field);
381     }
382     
383     /**
384      * Get the data value at the given field as a <code>String</code>.
385      * @param field the data field to retrieve
386      * @see #canGetString(String)
387      */

388     public final String JavaDoc getString(String JavaDoc field) {
389         return m_table.getString(m_cur, field);
390     }
391     
392     /**
393      * Set the data value of the given field with a <code>String</code>.
394      * @param field the data field to set
395      * @param val the value to set
396      * @see #canSetString(String)
397      */

398     public final void setString(String JavaDoc field, String JavaDoc val) {
399         ++m_modCount;
400         m_table.setString(m_cur, field, val);
401     }
402     
403     // --------------------------------------------------------------
404

405     /**
406      * Check if the given data field can return <code>Date</code>
407      * values.
408      * @param field the data field to check
409      * @return true if the data field can return <code>Date</code>
410      * values, false otherwise. If true, the {@link #getDate(String)} method
411      * can be used safely.
412      */

413     public final boolean canGetDate(String JavaDoc field) {
414         return m_table.canGetDate(field);
415     }
416     
417     /**
418      * Check if the <code>setDate</code> method can safely be used for the
419      * given data field.
420      * @param field the data field to check
421      * @return true if the {@link #setDate(String, Date)} method can safely
422      * be used for the given field, false otherwise.
423      */

424     public final boolean canSetDate(String JavaDoc field) {
425         return m_table.canSetDate(field);
426     }
427     
428     /**
429      * Get the data value at the given field as a <code>Date</code>.
430      * @param field the data field to retrieve
431      * @see #canGetDate(String)
432      */

433     public final Date JavaDoc getDate(String JavaDoc field) {
434         return m_table.getDate(m_cur, field);
435     }
436     
437     /**
438      * Set the data value of the given field with a <code>Date</code>.
439      * @param field the data field to set
440      * @param val the value to set
441      * @see #canSetDate(String)
442      */

443     public final void setDate(String JavaDoc field, Date JavaDoc val) {
444         ++m_modCount;
445         m_table.setDate(m_cur, field, val);
446     }
447     
448 } // end of class TableIterator
449
Popular Tags