KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > column > DateColumn


1 package prefuse.data.column;
2
3 import java.util.Arrays JavaDoc;
4 import java.util.Date JavaDoc;
5
6 import prefuse.data.DataReadOnlyException;
7 import prefuse.data.DataTypeException;
8 import prefuse.util.TimeLib;
9
10 /**
11  * Column implementation for storing Date values.
12  *
13  * @author <a HREF="http://jheer.org">jeffrey heer</a>
14  */

15 public class DateColumn extends AbstractColumn {
16
17     private long[] m_values;
18     private int m_size;
19     
20     /**
21      * Create a new empty DateColumn.
22      */

23     public DateColumn() {
24         this(Date JavaDoc.class, 0, 10, 0L);
25     }
26
27     /**
28      * Create a new DateColumn.
29      * @param nrows the initial size of the column
30      */

31     public DateColumn(int nrows) {
32         this(Date JavaDoc.class, nrows, nrows, 0L);
33     }
34     
35     /**
36      * Create a new DateColumn.
37      * @param type the exact data type (must be an instance or
38      * subclass of java.util.Date)
39      * @param nrows the initial size of the column
40      */

41     public DateColumn(Class JavaDoc type, int nrows) {
42         this(type, nrows, nrows, 0L);
43     }
44     
45     /**
46      * Create a new DateColumn.
47      * @param type the exact data type (must be an instance or
48      * subclass of java.util.Date)
49      * @param nrows the initial size of the column
50      * @param capacity the initial capacity of the column
51      * @param defaultValue the default value for the column
52      */

53     public DateColumn(Class JavaDoc type, int nrows, int capacity, long defaultValue) {
54         super(type, TimeLib.getDate(type, defaultValue));
55         if ( !Date JavaDoc.class.isAssignableFrom(type) ) {
56             throw new IllegalArgumentException JavaDoc("Column type must be an "
57                 + "instance or subclass of java.util.Date.");
58         }
59         if ( capacity < nrows ) {
60             throw new IllegalArgumentException JavaDoc(
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     // ------------------------------------------------------------------------
69
// Column Metadata
70

71     /**
72      * @see prefuse.data.column.Column#getRowCount()
73      */

74     public int getRowCount() {
75         return m_size;
76     }
77     
78     /**
79      * @see prefuse.data.column.Column#setMaximumRow(int)
80      */

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 JavaDoc)m_defaultValue).getTime());
88             m_values = values;
89         }
90         m_size = nrows;
91     }
92     
93     /**
94      * Indicates if the set method can be called without
95      * an exception being thrown for the given type.
96      * @param type the Class of the data type to check
97      * @return true if the type is supported by this column, false otherwise
98      */

99     public boolean canSet(Class JavaDoc type) {
100         if ( type == null ) return false;
101         
102         if ( Number JavaDoc.class.isAssignableFrom(type) ||
103              String JavaDoc.class.isAssignableFrom(type) )
104         {
105             return true;
106         } else {
107             return m_columnType.isAssignableFrom(type);
108         }
109     }
110     
111     // ------------------------------------------------------------------------
112
// Data Access Methods
113

114     /**
115      * @see prefuse.data.column.Column#get(int)
116      */

117     public Object JavaDoc get(int row) {
118         return TimeLib.getDate(m_columnType, getLong(row));
119     }
120
121     /**
122      * @see prefuse.data.column.Column#set(java.lang.Object, int)
123      */

124     public void set(Object JavaDoc val, int row) throws DataTypeException {
125         if ( m_readOnly ) {
126             throw new DataReadOnlyException();
127         } else if ( val != null ) {
128             if ( val instanceof Date JavaDoc ) {
129                 setLong(((Date JavaDoc)val).getTime(), row);
130             } else if ( val instanceof Number JavaDoc ) {
131                 setLong(((Number JavaDoc)val).longValue(), row);
132             } else if ( val instanceof String JavaDoc ) {
133                 setString((String JavaDoc)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     // ------------------------------------------------------------------------
143
// Data Type Convenience Methods
144

145     /**
146      * @see prefuse.data.column.AbstractColumn#getLong(int)
147      */

148     public long getLong(int row) throws DataTypeException {
149         if ( row < 0 || row > m_size ) {
150             throw new IllegalArgumentException JavaDoc("Row index out of bounds: "+row);
151         }
152         return m_values[row];
153     }
154
155     /**
156      * @see prefuse.data.column.AbstractColumn#setLong(long, int)
157      */

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 JavaDoc("Row index out of bounds: "+row);
163         }
164         // get the previous value
165
long prev = m_values[row];
166         
167         // exit early if no change
168
if ( prev == val ) return;
169         
170         // set the new value
171
m_values[row] = val;
172         
173         // fire a change event
174
fireColumnEvent(row, prev);
175     }
176
177     // ------------------------------------------------------------------------
178

179     /**
180      * @see prefuse.data.column.Column#getDouble(int)
181      */

182     public double getDouble(int row) throws DataTypeException {
183         return getLong(row);
184     }
185     
186 } // end of class DateColumn
187
Popular Tags