1 package prefuse.data.column; 2 3 import java.util.Arrays ; 4 import java.util.Date ; 5 6 import prefuse.data.DataReadOnlyException; 7 import prefuse.data.DataTypeException; 8 import prefuse.util.TimeLib; 9 10 15 public class DateColumn extends AbstractColumn { 16 17 private long[] m_values; 18 private int m_size; 19 20 23 public DateColumn() { 24 this(Date .class, 0, 10, 0L); 25 } 26 27 31 public DateColumn(int nrows) { 32 this(Date .class, nrows, nrows, 0L); 33 } 34 35 41 public DateColumn(Class type, int nrows) { 42 this(type, nrows, nrows, 0L); 43 } 44 45 53 public DateColumn(Class type, int nrows, int capacity, long defaultValue) { 54 super(type, TimeLib.getDate(type, defaultValue)); 55 if ( !Date .class.isAssignableFrom(type) ) { 56 throw new IllegalArgumentException ("Column type must be an " 57 + "instance or subclass of java.util.Date."); 58 } 59 if ( capacity < nrows ) { 60 throw new IllegalArgumentException ( 61 "Capacity value can not be less than the row count."); 62 } 63 m_values = new long[capacity]; 64 Arrays.fill(m_values, defaultValue); 65 m_size = nrows; 66 } 67 68 71 74 public int getRowCount() { 75 return m_size; 76 } 77 78 81 public void setMaximumRow(int nrows) { 82 if ( nrows > m_values.length ) { 83 int capacity = Math.max((3*m_values.length)/2 + 1, nrows); 84 long[] values = new long[capacity]; 85 System.arraycopy(m_values, 0, values, 0, m_size); 86 Arrays.fill(values, m_size, capacity, 87 ((Date )m_defaultValue).getTime()); 88 m_values = values; 89 } 90 m_size = nrows; 91 } 92 93 99 public boolean canSet(Class type) { 100 if ( type == null ) return false; 101 102 if ( Number .class.isAssignableFrom(type) || 103 String .class.isAssignableFrom(type) ) 104 { 105 return true; 106 } else { 107 return m_columnType.isAssignableFrom(type); 108 } 109 } 110 111 114 117 public Object get(int row) { 118 return TimeLib.getDate(m_columnType, getLong(row)); 119 } 120 121 124 public void set(Object val, int row) throws DataTypeException { 125 if ( m_readOnly ) { 126 throw new DataReadOnlyException(); 127 } else if ( val != null ) { 128 if ( val instanceof Date ) { 129 setLong(((Date )val).getTime(), row); 130 } else if ( val instanceof Number ) { 131 setLong(((Number )val).longValue(), row); 132 } else if ( val instanceof String ) { 133 setString((String )val, row); 134 } else { 135 throw new DataTypeException(val.getClass()); 136 } 137 } else { 138 throw new DataTypeException("Column does not accept null values"); 139 } 140 } 141 142 145 148 public long getLong(int row) throws DataTypeException { 149 if ( row < 0 || row > m_size ) { 150 throw new IllegalArgumentException ("Row index out of bounds: "+row); 151 } 152 return m_values[row]; 153 } 154 155 158 public void setLong(long val, int row) throws DataTypeException { 159 if ( m_readOnly ) { 160 throw new DataReadOnlyException(); 161 } else if ( row < 0 || row >= m_size ) { 162 throw new IllegalArgumentException ("Row index out of bounds: "+row); 163 } 164 long prev = m_values[row]; 166 167 if ( prev == val ) return; 169 170 m_values[row] = val; 172 173 fireColumnEvent(row, prev); 175 } 176 177 179 182 public double getDouble(int row) throws DataTypeException { 183 return getLong(row); 184 } 185 186 } | Popular Tags |