KickJava   Java API By Example, From Geeks To Geeks.

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


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

13 public class DoubleColumn extends AbstractColumn {
14
15     private double[] m_values;
16     private int m_size;
17     
18     /**
19      * Create a new empty DoubleColumn.
20      */

21     public DoubleColumn() {
22         this(0, 10, 0);
23     }
24     
25     /**
26      * Create a new DoubleColumn.
27      * @param nrows the initial size of the column
28      */

29     public DoubleColumn(int nrows) {
30         this(nrows, nrows, 0);
31     }
32     
33     /**
34      * Create a new DoubleColumn.
35      * @param nrows the initial size of the column
36      * @param capacity the initial capacity of the column
37      * @param defaultValue the default value for the column
38      */

39     public DoubleColumn(int nrows, int capacity, double defaultValue) {
40         super(double.class, new Double JavaDoc(defaultValue));
41         if ( capacity < nrows ) {
42             throw new IllegalArgumentException JavaDoc(
43                 "Capacity value can not be less than the row count.");
44         }
45         m_values = new double[capacity];
46         Arrays.fill(m_values, defaultValue);
47         m_size = nrows;
48     }
49     
50     // ------------------------------------------------------------------------
51
// Column Metadata
52

53     /**
54      * @see prefuse.data.column.Column#getRowCount()
55      */

56     public int getRowCount() {
57         return m_size;
58     }
59     
60     /**
61      * @see prefuse.data.column.Column#setMaximumRow(int)
62      */

63     public void setMaximumRow(int nrows) {
64         if ( nrows > m_values.length ) {
65             int capacity = Math.max((3*m_values.length)/2 + 1, nrows);
66             double[] values = new double[capacity];
67             System.arraycopy(m_values, 0, values, 0, m_size);
68             Arrays.fill(values, m_size, capacity,
69                     ((Double JavaDoc)m_defaultValue).doubleValue());
70             m_values = values;
71         }
72         m_size = nrows;
73     }
74
75     // ------------------------------------------------------------------------
76
// Data Access Methods
77

78     /**
79      * @see prefuse.data.column.Column#get(int)
80      */

81     public Object JavaDoc get(int row) {
82         return new Double JavaDoc(getDouble(row));
83     }
84
85     /**
86      * @see prefuse.data.column.Column#set(java.lang.Object, int)
87      */

88     public void set(Object JavaDoc val, int row) throws DataTypeException {
89         if ( m_readOnly ) {
90             throw new DataReadOnlyException();
91         } else if ( val != null ) {
92             if ( val instanceof Number JavaDoc ) {
93                 setDouble(((Number JavaDoc)val).doubleValue(), row);
94             } else if ( val instanceof String JavaDoc ) {
95                 setString((String JavaDoc)val, row);
96             } else {
97                 throw new DataTypeException(val.getClass());
98             }
99         } else {
100             throw new DataTypeException("Column does not accept null values");
101         }
102     }
103
104     // ------------------------------------------------------------------------
105
// Data Type Convenience Methods
106

107     /**
108      * @see prefuse.data.column.AbstractColumn#getDouble(int)
109      */

110     public double getDouble(int row) throws DataTypeException {
111         if ( row < 0 || row > m_size ) {
112             throw new IllegalArgumentException JavaDoc("Row index out of bounds: "+row);
113         }
114         return m_values[row];
115     }
116
117     /**
118      * @see prefuse.data.column.AbstractColumn#setDouble(double, int)
119      */

120     public void setDouble(double val, int row) throws DataTypeException {
121         if ( m_readOnly ) {
122             throw new DataReadOnlyException();
123         } else if ( row < 0 || row >= m_size ) {
124             throw new IllegalArgumentException JavaDoc("Row index out of bounds: "+row);
125         }
126         // get the previous value
127
double prev = m_values[row];
128         
129         // exit early if no change
130
if ( prev == val ) return;
131         
132         // set the new value
133
m_values[row] = val;
134         
135         // fire a change event
136
fireColumnEvent(row, prev);
137     }
138     
139 // /**
140
// * @see prefuse.data.column.AbstractColumn#getString(int)
141
// */
142
// public String getString(int row) throws DataTypeException {
143
// return String.valueOf(getDouble(row));
144
// }
145
//
146
// /**
147
// * @see prefuse.data.column.AbstractColumn#setString(java.lang.String, int)
148
// */
149
// public void setString(String val, int row) throws DataTypeException {
150
// setDouble(Double.parseDouble(val), row);
151
// }
152

153     // ------------------------------------------------------------------------
154

155     /**
156      * @see prefuse.data.column.Column#getInt(int)
157      */

158     public int getInt(int row) throws DataTypeException {
159         return (int)getDouble(row);
160     }
161     
162     /**
163      * @see prefuse.data.column.Column#setInt(int, int)
164      */

165     public void setInt(int val, int row) throws DataTypeException {
166         setDouble(val, row);
167     }
168     
169     /**
170      * @see prefuse.data.column.Column#getLong(int)
171      */

172     public long getLong(int row) throws DataTypeException {
173         return (long)getDouble(row);
174     }
175     
176     /**
177      * @see prefuse.data.column.Column#setLong(long, int)
178      */

179     public void setLong(long val, int row) throws DataTypeException {
180         setDouble(val, row);
181     }
182     
183     /**
184      * @see prefuse.data.column.Column#getFloat(int)
185      */

186     public float getFloat(int row) throws DataTypeException {
187         return (float)getDouble(row);
188     }
189     
190     /**
191      * @see prefuse.data.column.Column#setFloat(float, int)
192      */

193     public void setFloat(float val, int row) throws DataTypeException {
194         setDouble(val, row);
195     }
196     
197 } // end of class DoubleColumn
198
Popular Tags