use of org.knime.core.data.DataCellFactory.FromString in project knime-core by knime.
the class DataCellFactory method createDataCellOfType.
/**
* Creates a {@link DataCell} of the specified type from the data passed.
* A {@link DataCell} with a missing value is created if the passed data equals (see
* {@link String#equals(Object)} the currently set missing value pattern
* (disabled by default, see {@link #setMissingValuePattern(String)}).<br>
* A {@link StringCell} can always be created, returns only
* <code>null</code>, if <code>null</code> was provided as data (a
* {@link StringCell} can't hold <code>null</code>). <br>
* Creating an {@link IntCell} fails, if the provided data is not a valid
* string representation of an integer number (see
* {@link Integer#parseInt(String)}).<br>
* Creation of a {@link DoubleCell} fails, if the provided data is not a
* valid string representation of a double number (see
* {@link Double#parseDouble(String)}, with respect to the currently set
* decimal and thousands separators.
*
* @param type the type of the data cell to create. If the provided data
* can't be translated to the corresponding type, null is
* returned. The error message can be retrieved through the
* {@link #getErrorMessage()}.
* @param data the string representation of the data to store in the newly
* created cell. Can't be null.
* @return a data cell of the specified type carrying the provided data.
* <code>Null</code> is returned if the cell couldn't be created.
* Mostly due to incompatible string data, probably. The error
* message can then be retrieved through the
* {@link #getErrorMessage()} method.
* @see #setMissingValuePattern(String)
* @throws IllegalArgumentException if the passed type is not supported.
* @throws NullPointerException if the passed data or type is null.
* @see #getErrorMessage()
* @see DataType#getMissingCell()
*/
public final DataCell createDataCellOfType(final DataType type, String data) {
if (type == null || data == null) {
throw new NullPointerException("DataType and the data can't be null.");
}
// clear any previous error message
m_lastErrorMessage = null;
if (data.equals(m_missingValuePattern)) {
return DataType.getMissingCell();
}
org.knime.core.data.DataCellFactory cellFactory = m_cellFactoryMap.get(type);
if (cellFactory == null) {
cellFactory = type.getCellFactory(m_execContext).orElseThrow(() -> new IllegalArgumentException("No data cell factory for data type '" + type + "' found"));
m_cellFactoryMap.put(type, cellFactory);
}
if (cellFactory instanceof ConfigurableDataCellFactory) {
((ConfigurableDataCellFactory) cellFactory).configure(m_formatParameter);
}
if (type.equals(DoubleCell.TYPE)) {
// remove thousands grouping
if (m_thousandsRegExpr != null) {
Matcher thousandMatcher = m_thousandPattern.matcher(data);
if (thousandMatcher.matches()) {
// Only continue processing if input is a valid number (wrong thousands separators are targeted to identify dates
data = data.replaceAll(m_thousandsRegExpr, "");
} else {
m_lastErrorMessage = "Wrong data format. Got '" + data + "' for a floating point.";
return null;
}
}
// replace decimal separator with java separator '.'
if (m_decimalSeparator != '.') {
// we must reject tokens with a '.'.
if (data.indexOf('.') >= 0) {
m_lastErrorMessage = "Wrong data format. Got '" + data + "' for a floating point.";
return null;
}
data = data.replace(m_decimalSeparator, '.');
}
}
try {
return ((FromString) cellFactory).createCell(data);
} catch (Throwable t) {
m_lastErrorMessage = t.getMessage();
if (m_lastErrorMessage == null) {
m_lastErrorMessage = "No details.";
}
return null;
}
}
Aggregations