KickJava   Java API By Example, From Geeks To Geeks.

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


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 int values.
10  *
11  * @author <a HREF="http://jheer.org">jeffrey heer</a>
12  */

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

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

29     public IntColumn(int nrows) {
30         this(nrows, nrows, -1);
31     }
32     
33     /**
34      * Create a new IntColumn.
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 IntColumn(int nrows, int capacity, int defaultValue) {
40         super(int.class, new Integer 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 int[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             int[] values = new int[capacity];
67             System.arraycopy(m_values, 0, values, 0, m_size);
68             Arrays.fill(values, m_size, capacity,
69                     ((Integer JavaDoc)m_defaultValue).intValue());
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 Integer JavaDoc(getInt(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                 setInt(((Number JavaDoc)val).intValue(), 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#getInt(int)
109      */

110     public int getInt(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#setInt(int, int)
119      */

120     public void setInt(int 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
int 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(getInt(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
// setInt(Integer.parseInt(val), row);
151
// }
152

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

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

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

165     public float getFloat(int row) throws DataTypeException {
166         return getInt(row);
167     }
168     
169     /**
170      * @see prefuse.data.column.Column#getDouble(int)
171      */

172     public double getDouble(int row) throws DataTypeException {
173         return getInt(row);
174     }
175
176 } // end of class IntColumn
177
Popular Tags