1 package prefuse.data.column; 2 3 import java.util.Arrays ; 4 5 import prefuse.data.DataReadOnlyException; 6 import prefuse.data.DataTypeException; 7 8 13 public class ByteColumn extends AbstractColumn { 14 15 private byte[] m_values; 16 private int m_size; 17 18 21 public ByteColumn() { 22 this(0, 10, (byte)0); 23 } 24 25 29 public ByteColumn(int nrows) { 30 this(nrows, nrows, (byte)0); 31 } 32 33 39 public ByteColumn(int nrows, int capacity, byte defaultValue) { 40 super(byte.class, new Byte (defaultValue)); 41 if ( capacity < nrows ) { 42 throw new IllegalArgumentException ( 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 53 56 public int getRowCount() { 57 return m_size; 58 } 59 60 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 )m_defaultValue).byteValue()); 70 m_values = values; 71 } 72 m_size = nrows; 73 } 74 75 78 81 public Object get(int row) { 82 return new Byte (getByte(row)); 83 } 84 85 88 public void set(Object val, int row) throws DataTypeException { 89 if ( m_readOnly ) { 90 throw new DataReadOnlyException(); 91 } else if ( val != null ) { 92 if ( val instanceof Number ) { 93 setInt(((Number )val).byteValue(), row); 94 } else if ( val instanceof String ) { 95 setString((String )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 107 public byte getByte(int row) throws DataTypeException { 108 if ( row < 0 || row > m_size ) { 109 throw new IllegalArgumentException ("Row index out of bounds: "+row); 110 } 111 return m_values[row]; 112 } 113 114 117 public int getInt(int row) throws DataTypeException { 118 if ( row < 0 || row > m_size ) { 119 throw new IllegalArgumentException ("Row index out of bounds: "+row); 120 } 121 return m_values[row]; 122 } 123 124 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 ("Row index out of bounds: "+row); 132 } 133 int prev = m_values[row]; 135 136 if ( prev == val ) return; 138 139 m_values[row] = (byte)val; 141 142 fireColumnEvent(row, prev); 144 } 145 146 148 151 public long getLong(int row) throws DataTypeException { 152 return getInt(row); 153 } 154 155 158 public float getFloat(int row) throws DataTypeException { 159 return getInt(row); 160 } 161 162 165 public double getDouble(int row) throws DataTypeException { 166 return getInt(row); 167 } 168 169 } | Popular Tags |