KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.column;
2
3 import java.util.Date JavaDoc;
4
5 import prefuse.data.DataTypeException;
6 import prefuse.data.Table;
7 import prefuse.data.expression.Expression;
8
9 /**
10  * Factory class for generating appropriate column instances. Used by
11  * Tables to generate their columns.
12  *
13  * @author <a HREF="http://jheer.org">jeffrey heer</a>
14  */

15 public class ColumnFactory {
16     
17     /**
18      * Get a new column of the given type.
19      * @param type the column data type
20      * @return the new column
21      */

22     public static final Column getColumn(Class JavaDoc type) {
23         return getColumn(type, 0, 0, null);
24     }
25
26     /**
27      * Get a new column of the given type.
28      * @param type the column data type
29      * @param nrows the number of rows to include in the column
30      * @return the new column
31      */

32     public static final Column getColumn(Class JavaDoc type, int nrows) {
33         return getColumn(type, nrows, nrows, null);
34     }
35
36     /**
37      * Get a new column of the given type.
38      * @param type the column data type
39      * @param nrows the number of rows to include in the column
40      * @param defaultValue the default value for the column
41      * @return the new column
42      */

43     public static final Column getColumn(Class JavaDoc type, int nrows,
44                                          Object JavaDoc defaultValue)
45     {
46         return getColumn(type, nrows, nrows, defaultValue);
47     }
48     
49     /**
50      * Get a new column of the given type.
51      * @param type the column data type
52      * @param nrows the number of rows to include in the column
53      * @param nnz the number of expected non-zero entries (NOTE: currently
54      * this value is not being used)
55      * @param defaultValue the default value for the column
56      * @return the new column
57      */

58     public static final Column getColumn(Class JavaDoc type, int nrows, int nnz,
59                                          Object JavaDoc defaultValue)
60     {
61         if ( type == byte.class )
62         {
63             if ( defaultValue == null ) {
64                 return new ByteColumn(nrows);
65             } else {
66                 byte def = ((Number JavaDoc)defaultValue).byteValue();
67                 return new ByteColumn(nrows, nrows, def);
68             }
69         }
70         if ( type == int.class )
71         {
72             if ( defaultValue == null ) {
73                 return new IntColumn(nrows);
74             } else {
75                 int def = ((Number JavaDoc)defaultValue).intValue();
76                 return new IntColumn(nrows, nrows, def);
77             }
78         }
79         else if ( type == long.class )
80         {
81             if ( defaultValue == null ) {
82                 return new LongColumn(nrows);
83             } else {
84                 long def = ((Number JavaDoc)defaultValue).longValue();
85                 return new LongColumn(nrows, nrows, def);
86             }
87         }
88         else if ( type == float.class )
89         {
90             if ( defaultValue == null ) {
91                 return new FloatColumn(nrows);
92             } else {
93                 float def = ((Number JavaDoc)defaultValue).floatValue();
94                 return new FloatColumn(nrows, nrows, def);
95             }
96         }
97         else if ( type == double.class )
98         {
99             if ( defaultValue == null ) {
100                 return new DoubleColumn(nrows);
101             } else {
102                 double def = ((Number JavaDoc)defaultValue).doubleValue();
103                 return new DoubleColumn(nrows, nrows, def);
104             }
105         }
106         else if ( type == boolean.class )
107         {
108             if ( defaultValue == null ) {
109                 return new BooleanColumn(nrows);
110             } else {
111                 boolean def = ((Boolean JavaDoc)defaultValue).booleanValue();
112                 return new BooleanColumn(nrows, nrows, def);
113             }
114         }
115         else if ( Date JavaDoc.class.isAssignableFrom(type) )
116         {
117             if ( defaultValue == null ) {
118                 return new DateColumn(type, nrows);
119             } else {
120                 Date JavaDoc d = ((Date JavaDoc)defaultValue);
121                 return new DateColumn(type, nrows, nrows, d.getTime());
122             }
123         }
124         else if ( type == byte.class
125                     || type == short.class
126                     || type == char.class
127                     || type == void.class )
128         {
129             throw new DataTypeException(type);
130         }
131         else
132         {
133             return new ObjectColumn(type, nrows, nrows, defaultValue);
134         }
135     }
136     
137     /**
138      * Get a new column based on the given expression.
139      * @param t the table the column should be added to
140      * @param expr the expression that should provide the column values
141      * @return the new column
142      */

143     public static final Column getColumn(Table t, Expression expr) {
144         return new ExpressionColumn(t, expr);
145     }
146     
147     /**
148      * Get a new column of a constant value.
149      * @param type the column data type
150      * @param dflt the default constant value for the column
151      * @return the new column
152      */

153     public static final Column getConstantColumn(Class JavaDoc type, Object JavaDoc dflt) {
154         return new ConstantColumn(type, dflt);
155     }
156     
157 } // end of class ColumnFactory
158
Popular Tags