KickJava   Java API By Example, From Geeks To Geeks.

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


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 ByteColumn extends AbstractColumn {
14
15     private byte[] m_values;
16     private int m_size;
17     
18     /**
19      * Create a new empty IntColumn.
20      */

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

29     public ByteColumn(int nrows) {
30         this(nrows, nrows, (byte)0);
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 ByteColumn(int nrows, int capacity, byte defaultValue) {
40         super(byte.class, new Byte 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 byte[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             byte[] values = new byte[capacity];
67             System.arraycopy(m_values, 0, values, 0, m_size);
68             Arrays.fill(values, m_size, capacity,
69                     ((Byte JavaDoc)m_defaultValue).byteValue());
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 Byte JavaDoc(getByte(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).byteValue(), 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     public byte getByte(int row) throws DataTypeException {
108         if ( row < 0 || row > m_size ) {
109             throw new IllegalArgumentException JavaDoc("Row index out of bounds: "+row);
110         }
111         return m_values[row];
112     }
113     
114     /**
115      * @see prefuse.data.column.AbstractColumn#getInt(int)
116      */

117     public int getInt(int row) throws DataTypeException {
118         if ( row < 0 || row > m_size ) {
119             throw new IllegalArgumentException JavaDoc("Row index out of bounds: "+row);
120         }
121         return m_values[row];
122     }
123
124     /**
125      * @see prefuse.data.column.AbstractColumn#setInt(int, int)
126      */

127     public void setInt(int val, int row) throws DataTypeException {
128         if ( m_readOnly ) {
129             throw new DataReadOnlyException();
130         } else if ( row < 0 || row >= m_size) {
131             throw new IllegalArgumentException JavaDoc("Row index out of bounds: "+row);
132         }
133         // get the previous value
134
int prev = m_values[row];
135         
136         // exit early if no change
137
if ( prev == val ) return;
138         
139         // set the new value
140
m_values[row] = (byte)val;
141         
142         // fire a change event
143
fireColumnEvent(row, prev);
144     }
145     
146     // ------------------------------------------------------------------------
147

148     /**
149      * @see prefuse.data.column.Column#getLong(int)
150      */

151     public long getLong(int row) throws DataTypeException {
152         return getInt(row);
153     }
154     
155     /**
156      * @see prefuse.data.column.Column#getFloat(int)
157      */

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

165     public double getDouble(int row) throws DataTypeException {
166         return getInt(row);
167     }
168
169 } // end of class IntColumn
170
Popular Tags