1 package prefuse.data.column; 2 3 import java.lang.reflect.Method ; 4 import java.util.Arrays ; 5 import java.util.logging.Logger ; 6 7 import prefuse.data.DataReadOnlyException; 8 import prefuse.data.DataTypeException; 9 10 15 public class ObjectColumn extends AbstractColumn { 16 17 private Object [] m_values; 18 private int m_size; 19 20 23 public ObjectColumn() { 24 this(Object .class); 25 } 26 27 31 public ObjectColumn(Class type) { 32 this(type, 0, 10, null); 33 } 34 35 39 public ObjectColumn(int nrows) { 40 this(Object .class, nrows, nrows, null); 41 } 42 43 48 public ObjectColumn(Class type, int nrows) { 49 this(type, nrows, nrows, null); 50 } 51 52 61 public ObjectColumn(Class type, int nrows, int capacity, Object defaultValue) { 62 super(type, defaultValue); 63 if ( capacity < nrows ) { 64 throw new IllegalArgumentException ( 65 "Capacity value can not be less than the row count."); 66 } 67 m_values = new Object [capacity]; 68 try { 69 Cloneable def = (Cloneable )defaultValue; 72 Method m = def.getClass().getMethod("clone", null); 73 for ( int i=0; i<capacity; ++i ) { 74 m_values[i] = m.invoke(m_defaultValue, null); 75 } 76 } catch ( Exception e ) { 77 if ( defaultValue != null ) { 78 Logger.getLogger(getClass().getName()).fine( 79 "Default value of type \"" + 80 defaultValue.getClass().getName() + "\" is not " + 81 "cloneable. Using Object reference directly."); 82 } 83 Arrays.fill(m_values, defaultValue); 84 } 85 m_size = nrows; 86 } 87 88 91 94 public int getRowCount() { 95 return m_size; 96 } 97 98 101 public void setMaximumRow(int nrows) { 102 if ( nrows > m_values.length ) { 103 int capacity = Math.max((3*m_values.length)/2 + 1, nrows); 104 Object [] values = new Object [capacity]; 105 System.arraycopy(m_values, 0, values, 0, m_size); 106 try { 107 Cloneable def = (Cloneable )m_defaultValue; 110 Method m = def.getClass().getMethod("clone", null); 111 for ( int i=m_size; i<capacity; ++i ) { 112 values[i] = m.invoke(m_defaultValue, null); 113 } 114 } catch ( Exception e ) { 115 Arrays.fill(values, m_size, capacity, m_defaultValue); 116 } 117 m_values = values; 118 } 119 m_size = nrows; 120 } 121 122 125 public void revertToDefault(int row) { 126 try { 127 Cloneable def = (Cloneable )m_defaultValue; 130 Method m = def.getClass().getMethod("clone", null); 131 set(m.invoke(m_defaultValue, null), row); 132 } catch ( Exception e ) { 133 set(m_defaultValue, row); 134 } 135 } 136 137 142 public Object get(int row) { 143 if ( row < 0 || row > m_size ) { 144 throw new IllegalArgumentException ( 145 "Row index out of bounds: "+row); 146 } 147 return m_values[row]; 148 } 149 150 155 public void set(Object val, int row) { 156 if ( m_readOnly ) { 157 throw new DataReadOnlyException(); 158 } else if ( row < 0 || row > m_size ) { 159 throw new IllegalArgumentException ( 160 "Row index out of bounds: "+row); 161 } else if ( val == null || canSet(val.getClass()) ) { 162 Object prev = m_values[row]; 164 165 if ( prev == val ) return; 168 169 m_values[row] = val; 171 172 fireColumnEvent(row, prev); 174 } else { 175 throw new DataTypeException(val.getClass()); 176 } 177 } 178 179 } | Popular Tags |