001: package prefuse.data.column;
002:
003: import java.util.Date;
004:
005: import prefuse.data.DataTypeException;
006: import prefuse.data.Table;
007: import prefuse.data.expression.Expression;
008:
009: /**
010: * Factory class for generating appropriate column instances. Used by
011: * Tables to generate their columns.
012: *
013: * @author <a href="http://jheer.org">jeffrey heer</a>
014: */
015: public class ColumnFactory {
016:
017: /**
018: * Get a new column of the given type.
019: * @param type the column data type
020: * @return the new column
021: */
022: public static final Column getColumn(Class type) {
023: return getColumn(type, 0, 0, null);
024: }
025:
026: /**
027: * Get a new column of the given type.
028: * @param type the column data type
029: * @param nrows the number of rows to include in the column
030: * @return the new column
031: */
032: public static final Column getColumn(Class type, int nrows) {
033: return getColumn(type, nrows, nrows, null);
034: }
035:
036: /**
037: * Get a new column of the given type.
038: * @param type the column data type
039: * @param nrows the number of rows to include in the column
040: * @param defaultValue the default value for the column
041: * @return the new column
042: */
043: public static final Column getColumn(Class type, int nrows,
044: Object defaultValue) {
045: return getColumn(type, nrows, nrows, defaultValue);
046: }
047:
048: /**
049: * Get a new column of the given type.
050: * @param type the column data type
051: * @param nrows the number of rows to include in the column
052: * @param nnz the number of expected non-zero entries (NOTE: currently
053: * this value is not being used)
054: * @param defaultValue the default value for the column
055: * @return the new column
056: */
057: public static final Column getColumn(Class type, int nrows,
058: int nnz, Object defaultValue) {
059: if (type == byte.class) {
060: if (defaultValue == null) {
061: return new ByteColumn(nrows);
062: } else {
063: byte def = ((Number) defaultValue).byteValue();
064: return new ByteColumn(nrows, nrows, def);
065: }
066: }
067: if (type == int.class) {
068: if (defaultValue == null) {
069: return new IntColumn(nrows);
070: } else {
071: int def = ((Number) defaultValue).intValue();
072: return new IntColumn(nrows, nrows, def);
073: }
074: } else if (type == long.class) {
075: if (defaultValue == null) {
076: return new LongColumn(nrows);
077: } else {
078: long def = ((Number) defaultValue).longValue();
079: return new LongColumn(nrows, nrows, def);
080: }
081: } else if (type == float.class) {
082: if (defaultValue == null) {
083: return new FloatColumn(nrows);
084: } else {
085: float def = ((Number) defaultValue).floatValue();
086: return new FloatColumn(nrows, nrows, def);
087: }
088: } else if (type == double.class) {
089: if (defaultValue == null) {
090: return new DoubleColumn(nrows);
091: } else {
092: double def = ((Number) defaultValue).doubleValue();
093: return new DoubleColumn(nrows, nrows, def);
094: }
095: } else if (type == boolean.class) {
096: if (defaultValue == null) {
097: return new BooleanColumn(nrows);
098: } else {
099: boolean def = ((Boolean) defaultValue).booleanValue();
100: return new BooleanColumn(nrows, nrows, def);
101: }
102: } else if (Date.class.isAssignableFrom(type)) {
103: if (defaultValue == null) {
104: return new DateColumn(type, nrows);
105: } else {
106: Date d = ((Date) defaultValue);
107: return new DateColumn(type, nrows, nrows, d.getTime());
108: }
109: } else if (type == byte.class || type == short.class
110: || type == char.class || type == void.class) {
111: throw new DataTypeException(type);
112: } else {
113: return new ObjectColumn(type, nrows, nrows, defaultValue);
114: }
115: }
116:
117: /**
118: * Get a new column based on the given expression.
119: * @param t the table the column should be added to
120: * @param expr the expression that should provide the column values
121: * @return the new column
122: */
123: public static final Column getColumn(Table t, Expression expr) {
124: return new ExpressionColumn(t, expr);
125: }
126:
127: /**
128: * Get a new column of a constant value.
129: * @param type the column data type
130: * @param dflt the default constant value for the column
131: * @return the new column
132: */
133: public static final Column getConstantColumn(Class type, Object dflt) {
134: return new ConstantColumn(type, dflt);
135: }
136:
137: } // end of class ColumnFactory
|