KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > column > ObjectColumn


1 package prefuse.data.column;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.util.Arrays JavaDoc;
5 import java.util.logging.Logger JavaDoc;
6
7 import prefuse.data.DataReadOnlyException;
8 import prefuse.data.DataTypeException;
9
10 /**
11  * Column implementation for storing arbitrary Object values.
12  *
13  * @author <a HREF="http://jheer.org">jeffrey heer</a>
14  */

15 public class ObjectColumn extends AbstractColumn {
16
17     private Object JavaDoc[] m_values;
18     private int m_size;
19     
20     /**
21      * Create a new empty ObjectColumn. The type is assumed to be Object.
22      */

23     public ObjectColumn() {
24         this(Object JavaDoc.class);
25     }
26     
27     /**
28      * Create a new ObjectColumn.
29      * @param type the data type of Objects in this column
30      */

31     public ObjectColumn(Class JavaDoc type) {
32         this(type, 0, 10, null);
33     }
34     
35     /**
36      * Create a new ObjectColumn. The type is assumed to be Object.
37      * @param nrows the initial size of the column
38      */

39     public ObjectColumn(int nrows) {
40         this(Object JavaDoc.class, nrows, nrows, null);
41     }
42     
43     /**
44      * Create a new ObjectColumn.
45      * @param type the data type of Objects in this column
46      * @param nrows the initial size of the column
47      */

48     public ObjectColumn(Class JavaDoc type, int nrows) {
49         this(type, nrows, nrows, null);
50     }
51     
52     /**
53      * Create a new ObjectColumn.
54      * @param type the data type of Objects in this column
55      * @param nrows the initial size of the column
56      * @param capacity the initial capacity of the column
57      * @param defaultValue the default value for the column. If this value
58      * is cloneable, it will be cloned when assigned as defaultValue, otherwise
59      * the input reference will be used for every default value.
60      */

61     public ObjectColumn(Class JavaDoc type, int nrows, int capacity, Object JavaDoc defaultValue) {
62         super(type, defaultValue);
63         if ( capacity < nrows ) {
64             throw new IllegalArgumentException JavaDoc(
65                 "Capacity value can not be less than the row count.");
66         }
67         m_values = new Object JavaDoc[capacity];
68         try {
69             // since Object's clone method is protected, we default to
70
// using reflection to create clones.
71
Cloneable JavaDoc def = (Cloneable JavaDoc)defaultValue;
72             Method JavaDoc m = def.getClass().getMethod("clone", null);
73             for ( int i=0; i<capacity; ++i ) {
74                 m_values[i] = m.invoke(m_defaultValue, null);
75             }
76         } catch ( Exception JavaDoc e ) {
77             if ( defaultValue != null ) {
78                 Logger.getLogger(getClass().getName()).fine(
79                     "Default value of type \"" +
80                     defaultValue.getClass().getName() + "\" is not " +
81                     "cloneable. Using Object reference directly.");
82             }
83             Arrays.fill(m_values, defaultValue);
84         }
85         m_size = nrows;
86     }
87     
88     // ------------------------------------------------------------------------
89
// Column Metadata
90

91     /**
92      * @see prefuse.data.column.Column#getRowCount()
93      */

94     public int getRowCount() {
95         return m_size;
96     }
97     
98     /**
99      * @see prefuse.data.column.Column#setMaximumRow(int)
100      */

101     public void setMaximumRow(int nrows) {
102         if ( nrows > m_values.length ) {
103             int capacity = Math.max((3*m_values.length)/2 + 1, nrows);
104             Object JavaDoc[] values = new Object JavaDoc[capacity];
105             System.arraycopy(m_values, 0, values, 0, m_size);
106             try {
107                 // since Object's clone method is protected, we default to
108
// using reflection to create clones.
109
Cloneable JavaDoc def = (Cloneable JavaDoc)m_defaultValue;
110                 Method JavaDoc m = def.getClass().getMethod("clone", null);
111                 for ( int i=m_size; i<capacity; ++i ) {
112                     values[i] = m.invoke(m_defaultValue, null);
113                 }
114             } catch ( Exception JavaDoc e ) {
115                 Arrays.fill(values, m_size, capacity, m_defaultValue);
116             }
117             m_values = values;
118         }
119         m_size = nrows;
120     }
121
122     // ------------------------------------------------------------------------
123
// Data Access Methods
124

125     public void revertToDefault(int row) {
126         try {
127             // since Object's clone method is protected, we default to
128
// using reflection to create clones.
129
Cloneable JavaDoc def = (Cloneable JavaDoc)m_defaultValue;
130             Method JavaDoc m = def.getClass().getMethod("clone", null);
131             set(m.invoke(m_defaultValue, null), row);
132         } catch ( Exception JavaDoc e ) {
133             set(m_defaultValue, row);
134         }
135     }
136     
137     /**
138      * Get the data value at the specified row
139      * @param row the row from which to retrieve the value
140      * @return the data value
141      */

142     public Object JavaDoc get(int row) {
143         if ( row < 0 || row > m_size ) {
144             throw new IllegalArgumentException JavaDoc(
145                 "Row index out of bounds: "+row);
146         }
147         return m_values[row];
148     }
149         
150     /**
151      * Set the data value at the specified row
152      * @param val the value to set
153      * @param row the row at which to set the value
154      */

155     public void set(Object JavaDoc val, int row) {
156         if ( m_readOnly ) {
157             throw new DataReadOnlyException();
158         } else if ( row < 0 || row > m_size ) {
159             throw new IllegalArgumentException JavaDoc(
160                 "Row index out of bounds: "+row);
161         } else if ( val == null || canSet(val.getClass()) ) {
162             // get the previous value
163
Object JavaDoc prev = m_values[row];
164             
165             // exit early if no change
166
// do we trust .equals() here? for now, no.
167
if ( prev == val ) return;
168             
169             // set the new value
170
m_values[row] = val;
171             
172             // fire a change event
173
fireColumnEvent(row, prev);
174         } else {
175             throw new DataTypeException(val.getClass());
176         }
177     }
178     
179 } // end of class ObjectColumn
180
Popular Tags