- What's a DataCell?
A DataCell is a container for a single entity.
There are many different implementations of DataCell,
for instance StringCell,
DoubleCell, IntCell.
DataCells are contained in a DataRow
and DataRows are contained in a DataTable.
DataCells implement different DataValue interfaces and are associated to a
DataType (see both descriptions below).
- What's a DataValue?
A DataValue is an interface describing the
content of a DataCell. (For example, how to
access the double value from a DoubleCell.) It also brings along meta information
such as renderers, icon and a comparator which is accessed statically using
reflection through a singleton called UTILITY.
- What's a DataType?
A DataType comprises the meta-information to a
DataCell implementation such as a compatibility
list (to what DataValues the cells can be casted to),
available renderers, comparators and icon. The DataType is a property of
a DataColumnSpec.
-
How do I determine the DataType of a DataCell
implementation?
Use DataCell#getType(). It is a
shortcut for DataType.getType(SomeDataCell.class).
Note that a DataCell instance does not carry a reference to its DataType but
rather it determines the type through its runtime class (realized by a HashMap
in DataType which maps DataCell classes to DataType).
-
I want to create a new column for a DataTable and need
to set the column's DataType. How do I do this?
You need to know what DataCell instances you add to the column. For instance,
if you only add DoubleCell objects, set the type as
DataType.getType(DoubleCell.class)
. (All default cell implementations
provide a singleton for exactly this purpose, e.g.
DoubleCell.TYPE).
-
I don't know what cells I'm going to add to the new
column. How do I set the DataType of the column?
This should be a really rare case! One possible example is, when a
table is transposed and the most general type for all cells in
a table's row has to be determined.
You need to scan all cells and compute their most general DataType.
This type may not be associated with any real DataCell implementation, but it
will be valid for all DataCells that are contained in the column. Use the
static method DataType#getCommonSuperType(DataType, DataType)
in the class
DataType. You need to call this method iteratively, for instance:
DataCell[] allCells = ...;
DataType type = allCells[0].getType()
for (int i = 1; i < allCells.length; i++) {
type = DataType.getCommonSuperType(type, allCells[i].getType());
}
// "type" will be the most specific super type of all found types.
-
How about missing cells?
The term "missing cell" does not really mean that there is no cell but simply
that the cell at hand represents an unknown value. As a missing cell does not
have any content, there is a singleton to use in case you want to represent
such a missing entity. It is available through
DataType.getMissingCell(). This
singleton's type is a special type, which is compatible to all DataValue
interfaces you can think of. If you invoke
isCompatible(...)
on this type, it will always return true. However, trying to cast this missing
cell to any DataValue will fail. Be advised to always check a cell's
isMissing()
method before typecasting it.
-
When can I safely typecast a DataCell to a specific
DataValue.
There are two constraints:
- The cell must not represent a missing value,
i.e. cell.isMissing() has to evaluate to
false and
- the cell's type allows you to do so, i.e.
type.isCompatible(SomeDataValue.class)
returns true. The second constraint also holds for the DataColumnSpec's type in which
the cell is contained in.
Thus, you need to check once if the DataType as
given by the DataColumnSpec is compatible to the DataValue interface of
interest and if so, you can safely cast any DataCell from that column to the
given DataValue unless it represents a missing value.
-
Are all cells in a data table column of the same type?
Not necessarily. All you can assume is that each DataValue interface for which
the DataColumnSpec's type returns true on isCompatible(SomeDataValue.class), is
implemented by all of the cells in the column unless they represent a missing
value.
-
How do I implement support for a completely new kind
of data type?
See our manual for that.