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 DoubleColumn extends AbstractColumn { 14 15 private double[] m_values; 16 private int m_size; 17 18 21 public DoubleColumn() { 22 this(0, 10, 0); 23 } 24 25 29 public DoubleColumn(int nrows) { 30 this(nrows, nrows, 0); 31 } 32 33 39 public DoubleColumn(int nrows, int capacity, double defaultValue) { 40 super(double.class, new Double (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 double[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 double[] values = new double[capacity]; 67 System.arraycopy(m_values, 0, values, 0, m_size); 68 Arrays.fill(values, m_size, capacity, 69 ((Double )m_defaultValue).doubleValue()); 70 m_values = values; 71 } 72 m_size = nrows; 73 } 74 75 78 81 public Object get(int row) { 82 return new Double (getDouble(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 setDouble(((Number )val).doubleValue(), 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 double getDouble(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 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 ("Row index out of bounds: "+row); 125 } 126 double 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)getDouble(row); 160 } 161 162 165 public void setInt(int val, int row) throws DataTypeException { 166 setDouble(val, row); 167 } 168 169 172 public long getLong(int row) throws DataTypeException { 173 return (long)getDouble(row); 174 } 175 176 179 public void setLong(long val, int row) throws DataTypeException { 180 setDouble(val, row); 181 } 182 183 186 public float getFloat(int row) throws DataTypeException { 187 return (float)getDouble(row); 188 } 189 190 193 public void setFloat(float val, int row) throws DataTypeException { 194 setDouble(val, row); 195 } 196 197 } | Popular Tags |