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 FloatColumn extends AbstractColumn { 14 15 private float[] m_values; 16 private int m_size; 17 18 21 public FloatColumn() { 22 this(0, 10, 0f); 23 } 24 25 29 public FloatColumn(int nrows) { 30 this(nrows, nrows, 0f); 31 } 32 33 39 public FloatColumn(int nrows, int capacity, float defaultValue) { 40 super(float.class, new Float (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 float[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 float[] values = new float[capacity]; 67 System.arraycopy(m_values, 0, values, 0, m_size); 68 Arrays.fill(values, m_size, capacity, 69 ((Float )m_defaultValue).floatValue()); 70 m_values = values; 71 } 72 m_size = nrows; 73 } 74 75 78 81 public Object get(int row) { 82 return new Float (getFloat(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 setFloat(((Number )val).floatValue(), 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 110 public float getFloat(int row) throws DataTypeException { 111 if ( row < 0 || row > m_size ) { 112 throw new IllegalArgumentException ("Row index out of bounds: "+row); 113 } 114 return m_values[row]; 115 } 116 117 120 public void setFloat(float 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 ("Row index out of bounds: "+row); 125 } 126 float prev = m_values[row]; 128 129 if ( prev == val ) return; 131 132 m_values[row] = val; 134 135 fireColumnEvent(row, prev); 137 } 138 139 153 155 158 public int getInt(int row) throws DataTypeException { 159 return (int)getFloat(row); 160 } 161 162 165 public long getLong(int row) throws DataTypeException { 166 return (long)getFloat(row); 167 } 168 169 172 public double getDouble(int row) throws DataTypeException { 173 return getFloat(row); 174 } 175 176 } | Popular Tags |