use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.
the class ExtractDurationPeriodFieldsNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
final ColumnRearranger rearranger = new ColumnRearranger(spec);
if (m_colSelectModel.getStringValue() == null) {
throw new InvalidSettingsException("Node must be configured!");
}
if (spec.findColumnIndex(m_colSelectModel.getStringValue()) < 0) {
throw new InvalidSettingsException("Column '" + m_colSelectModel.getStringValue() + "' not found in the input table.");
}
final boolean isDurationColumn = spec.getColumnSpec(m_colSelectModel.getStringValue()).getType().isCompatible(DurationValue.class);
final List<DataColumnSpec> colSpecs = new ArrayList<>();
if (isDurationColumn) {
for (final SettingsModelBoolean model : m_durModels) {
if (model.getBooleanValue()) {
if (model.equals(m_hourModel)) {
colSpecs.add(new UniqueNameGenerator(spec).newColumn(model.getConfigName(), LongCell.TYPE));
} else {
colSpecs.add(new UniqueNameGenerator(spec).newColumn(model.getConfigName(), IntCell.TYPE));
}
}
}
if (m_subSecondModel.getBooleanValue()) {
colSpecs.add(new UniqueNameGenerator(spec).newColumn(m_subSecondUnitsModel.getStringValue(), IntCell.TYPE));
}
} else {
for (final SettingsModelBoolean model : m_perModels) {
if (model.getBooleanValue()) {
colSpecs.add(new UniqueNameGenerator(spec).newColumn(model.getConfigName(), LongCell.TYPE));
}
}
}
final DataColumnSpec[] colSpecsArray = new DataColumnSpec[colSpecs.size()];
colSpecs.toArray(colSpecsArray);
final CellFactory cellFac;
if (isDurationColumn) {
cellFac = new ExtractDurationFieldsCellFactory(spec.findColumnIndex(m_colSelectModel.getStringValue()), colSpecsArray);
} else {
cellFac = new ExtractPeriodFieldsCellFactory(spec.findColumnIndex(m_colSelectModel.getStringValue()), colSpecsArray);
}
rearranger.append(cellFac);
return rearranger;
}
use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.
the class OldToNewTimeNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
SimpleStreamableOperatorInternals m_internals = new SimpleStreamableOperatorInternals();
/**
* {@inheritDoc}
*/
@Override
public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
if (partitionInfo.getPartitionIndex() == 0) {
final RowInput rowInput = (RowInput) inputs[0];
final DataRow row = rowInput.poll();
if (row != null) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
final DataColumnSpec[] colSpecs = new DataColumnSpec[row.getNumCells()];
final DataTableSpec inSpec = rowInput.getDataTableSpec();
final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
for (int i = 0; i < inSpec.getNumColumns(); i++) {
final int searchIdx = Arrays.binarySearch(includeIndexes, i);
if (searchIdx < 0) {
colSpecs[i] = inSpec.getColumnSpec(i);
} else {
colSpecs[i] = newColumnSpecs[searchIdx];
}
}
final Config config = m_internals.getConfig();
config.addBoolean("hasIterated", false);
for (int i = 0; i < inSpec.getNumColumns(); i++) {
config.addDataType("type" + i, colSpecs[i].getType());
config.addString("colname" + i, colSpecs[i].getName());
}
config.addInt("sizeRow", colSpecs.length);
} else {
final DataTableSpec inSpec = rowInput.getDataTableSpec();
final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
final DataColumnSpec[] colSpecs = new DataColumnSpec[row.getNumCells() + includeIndexes.length];
for (int i = 0; i < inSpec.getNumColumns(); i++) {
colSpecs[i] = inSpec.getColumnSpec(i);
}
for (int i = 0; i < newColumnSpecs.length; i++) {
colSpecs[i + inSpec.getNumColumns()] = new UniqueNameGenerator(inSpec).newColumn(newColumnSpecs[i].getName() + m_suffix.getStringValue(), newColumnSpecs[i].getType());
}
final Config config = m_internals.getConfig();
config.addBoolean("hasIterated", false);
for (int i = 0; i < colSpecs.length; i++) {
config.addDataType("type" + i, colSpecs[i].getType());
config.addString("colname" + i, colSpecs[i].getName());
}
config.addInt("sizeRow", colSpecs.length);
}
} else {
m_internals.getConfig().addInt("sizeRow", 0);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public StreamableOperatorInternals saveInternals() {
return m_internals;
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
final RowInput in = (RowInput) inputs[0];
final RowOutput out = (RowOutput) outputs[0];
final DataTableSpec inSpec = in.getDataTableSpec();
final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
DataRow row;
while ((row = in.poll()) != null) {
exec.checkCanceled();
final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
DataCell[] datacells = new DataCell[includeIndexes.length];
for (int i = 0; i < includeIndexes.length; i++) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(newColumnSpecs[i], i, includeIndexes[i]);
datacells[i] = cellFac.getCells(row)[0];
} else {
final DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(newColumnSpecs[i].getName() + m_suffix.getStringValue(), newColumnSpecs[i].getType());
ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(dataColSpec, i, includeIndexes[i]);
datacells[i] = cellFac.getCells(row)[0];
}
}
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
out.push(new ReplacedColumnsDataRow(row, datacells, includeIndexes));
} else {
out.push(new AppendedColumnRow(row, datacells));
}
}
in.close();
out.close();
}
};
}
use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.
the class ExtractDateTimeFieldsNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
final String selectedCol = m_colSelectModel.getStringValue();
if (selectedCol == null || selectedCol.isEmpty()) {
throw new InvalidSettingsException("Node must be configured.");
}
final int selectedColIdx = spec.findColumnIndex(selectedCol);
if (selectedColIdx < 0) {
throw new InvalidSettingsException("Column " + selectedCol + " not found in the input table.");
}
final DataType selectedColType = spec.getColumnSpec(selectedCol).getType();
final boolean isDate = isDateType(selectedColType);
final boolean isTime = isTimeType(selectedColType);
if (!isDate && !isTime) {
throw new InvalidSettingsException("Column " + selectedCol + " does not contain a Date&Time type.");
}
final boolean isLocalDate = selectedColType.isCompatible(LocalDateValue.class);
final boolean isLocalTime = selectedColType.isCompatible(LocalTimeValue.class);
final boolean isLocalDateTime = selectedColType.isCompatible(LocalDateTimeValue.class);
final boolean isZonedDateTime = selectedColType.isCompatible(ZonedDateTimeValue.class);
final Locale locale = LocaleUtils.toLocale(m_localeModel.getStringValue());
final UniqueNameGenerator nameGenerator = new UniqueNameGenerator(spec);
final DataColumnDomainCreator domainCreator = new DataColumnDomainCreator();
final ColumnRearranger rearranger = new ColumnRearranger(spec);
if (isDate) {
if (m_yearModel.getBooleanValue()) {
final DataColumnSpec colSpec = nameGenerator.newColumn(YEAR, IntCell.TYPE);
if (isLocalDate) {
rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateValue value) {
return IntCellFactory.create(value.getLocalDate().getYear());
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().getYear());
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().getYear());
}
});
}
}
if (m_quarterModel.getBooleanValue()) {
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, QUARTER, 1, 4);
if (isLocalDate) {
rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateValue value) {
return IntCellFactory.create((value.getLocalDate().getMonthValue() + 2) / 3);
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create((value.getLocalDateTime().getMonthValue() + 2) / 3);
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create((value.getZonedDateTime().getMonthValue() + 2) / 3);
}
});
}
}
if (m_monthNumberModel.getBooleanValue()) {
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, MONTH_NUMBER, 1, 12);
if (isLocalDate) {
rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateValue value) {
return IntCellFactory.create(value.getLocalDate().getMonthValue());
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().getMonthValue());
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().getMonthValue());
}
});
}
}
if (m_monthNameModel.getBooleanValue()) {
final DataColumnSpec colSpec = nameGenerator.newColumn(MONTH_NAME, StringCell.TYPE);
if (isLocalDate) {
rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateValue value) {
return StringCellFactory.create(value.getLocalDate().getMonth().getDisplayName(TextStyle.FULL, locale));
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return StringCellFactory.create(value.getLocalDateTime().getMonth().getDisplayName(TextStyle.FULL, locale));
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return StringCellFactory.create(value.getZonedDateTime().getMonth().getDisplayName(TextStyle.FULL, locale));
}
});
}
}
if (m_weekModel.getBooleanValue()) {
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, WEEK, 1, 52);
if (isLocalDate) {
rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateValue value) {
return IntCellFactory.create(value.getLocalDate().get(WeekFields.of(locale).weekOfWeekBasedYear()));
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().get(WeekFields.of(locale).weekOfWeekBasedYear()));
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().get(WeekFields.of(locale).weekOfWeekBasedYear()));
}
});
}
}
if (m_dayYearModel.getBooleanValue()) {
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, DAY_OF_YEAR, 1, 366);
if (isLocalDate) {
rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateValue value) {
return IntCellFactory.create(value.getLocalDate().getDayOfYear());
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().getDayOfYear());
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().getDayOfYear());
}
});
}
}
if (m_dayMonthModel.getBooleanValue()) {
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, DAY_OF_MONTH, 1, 31);
if (isLocalDate) {
rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateValue value) {
return IntCellFactory.create(value.getLocalDate().getDayOfMonth());
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().getDayOfMonth());
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().getDayOfMonth());
}
});
}
}
if (m_dayWeekNumberModel.getBooleanValue()) {
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, DAY_OF_WEEK_NUMBER, 1, 7);
if (isLocalDate) {
rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateValue value) {
return IntCellFactory.create(value.getLocalDate().get(WeekFields.of(locale).dayOfWeek()));
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().get(WeekFields.of(locale).dayOfWeek()));
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().get(WeekFields.of(locale).dayOfWeek()));
}
});
}
}
if (m_dayWeekNameModel.getBooleanValue()) {
final DataColumnSpec colSpec = nameGenerator.newColumn(DAY_OF_WEEK_NAME, StringCell.TYPE);
if (isLocalDate) {
rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateValue value) {
return StringCellFactory.create(value.getLocalDate().getDayOfWeek().getDisplayName(TextStyle.FULL_STANDALONE, locale));
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return StringCellFactory.create(value.getLocalDateTime().getDayOfWeek().getDisplayName(TextStyle.FULL_STANDALONE, locale));
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return StringCellFactory.create(value.getZonedDateTime().getDayOfWeek().getDisplayName(TextStyle.FULL_STANDALONE, locale));
}
});
}
}
}
if (isTime) {
if (m_hourModel.getBooleanValue()) {
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, HOUR, 0, 23);
if (isLocalTime) {
rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalTimeValue value) {
return IntCellFactory.create(value.getLocalTime().getHour());
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().getHour());
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().getHour());
}
});
}
}
if (m_minuteModel.getBooleanValue()) {
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, MINUTE, 0, 59);
if (isLocalTime) {
rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalTimeValue value) {
return IntCellFactory.create(value.getLocalTime().getMinute());
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().getMinute());
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().getMinute());
}
});
}
}
if (m_secondModel.getBooleanValue()) {
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, SECOND, 0, 59);
if (isLocalTime) {
rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalTimeValue value) {
return IntCellFactory.create(value.getLocalTime().getSecond());
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().getSecond());
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().getSecond());
}
});
}
}
if (m_subsecondModel.getBooleanValue()) {
final String subsecondUnit = m_subsecondUnitsModel.getStringValue();
if (subsecondUnit.equals(MILLISECOND)) {
// in milliseconds
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, SUBSECOND_COL + " (in " + MILLISECOND + ")", 0, 999);
if (isLocalTime) {
rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalTimeValue value) {
return IntCellFactory.create(value.getLocalTime().get(ChronoField.MILLI_OF_SECOND));
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().get(ChronoField.MILLI_OF_SECOND));
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().get(ChronoField.MILLI_OF_SECOND));
}
});
}
} else if (subsecondUnit.equals(MICROSECOND)) {
// in microseconds
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, SUBSECOND_COL + " (in " + MICROSECOND + ")", 0, 999_999);
if (isLocalTime) {
rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalTimeValue value) {
return IntCellFactory.create(value.getLocalTime().get(ChronoField.MICRO_OF_SECOND));
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().get(ChronoField.MICRO_OF_SECOND));
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().get(ChronoField.MICRO_OF_SECOND));
}
});
}
} else if (subsecondUnit.equals(NANOSECOND)) {
// in nanoseconds
final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, SUBSECOND_COL + " (in " + NANOSECOND + ")", 0, 999_999_999);
if (isLocalTime) {
rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalTimeValue value) {
return IntCellFactory.create(value.getLocalTime().getNano());
}
});
} else if (isLocalDateTime) {
rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final LocalDateTimeValue value) {
return IntCellFactory.create(value.getLocalDateTime().getNano());
}
});
} else if (isZonedDateTime) {
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return IntCellFactory.create(value.getZonedDateTime().getNano());
}
});
}
}
}
if (isZonedDateTime) {
if (m_timeZoneNameModel.getBooleanValue()) {
final DataColumnSpec colSpec = nameGenerator.newColumn(TIME_ZONE_NAME, StringCell.TYPE);
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return StringCellFactory.create(value.getZonedDateTime().getZone().getId().toString());
}
});
}
if (m_timeZoneOffsetModel.getBooleanValue()) {
final DataColumnSpec colSpec = nameGenerator.newColumn(TIME_ZONE_OFFSET, StringCell.TYPE);
rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {
@Override
protected DataCell getCell(final ZonedDateTimeValue value) {
return StringCellFactory.create(value.getZonedDateTime().getOffset().getDisplayName(TextStyle.FULL_STANDALONE, locale));
}
});
}
}
}
if (rearranger.getColumnCount() == spec.getNumColumns()) {
getLogger().info("No fields will be extracted. Output table will equal input table.");
}
return rearranger;
}
use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.
the class DateTimeShiftNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
ColumnRearranger rearranger = new ColumnRearranger(spec);
String[] includeList = m_colSelect.applyTo(spec).getIncludes();
int[] includeIndices = Arrays.stream(m_colSelect.applyTo(spec).getIncludes()).mapToInt(s -> spec.findColumnIndex(s)).toArray();
int i = 0;
int periodColIndex = spec.findColumnIndex(m_periodColSelect.getStringValue());
int numericalColIndex = spec.findColumnIndex(m_numericalColSelect.getStringValue());
boolean isPeriod;
if (m_periodSelection.isEnabled()) {
if (m_periodColSelect.isEnabled()) {
if (spec.getColumnSpec(periodColIndex).getType().isCompatible(PeriodValue.class)) {
isPeriod = true;
} else {
isPeriod = false;
}
} else {
periodColIndex = -1;
try {
DurationPeriodFormatUtils.parsePeriod(m_periodValue.getStringValue());
isPeriod = true;
} catch (DateTimeParseException e) {
isPeriod = false;
}
}
} else {
if (!m_numericalColSelect.isEnabled()) {
numericalColIndex = -1;
}
if (!Granularity.fromString(m_numericalGranularity.getStringValue()).isPartOfDate()) {
isPeriod = false;
} else {
isPeriod = true;
}
}
for (String includedCol : includeList) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
final SingleCellFactory cellFac;
final DataColumnSpec dataColSpec = new DataColumnSpecCreator(includedCol, spec.getColumnSpec(includedCol).getType()).createSpec();
if (isPeriod) {
cellFac = new DateTimeShiftPeriodCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
} else {
cellFac = new DateTimeShiftDurationCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
}
rearranger.replace(cellFac, includedCol);
} else {
final DataColumnSpec dataColSpec = new UniqueNameGenerator(spec).newColumn(includedCol + m_suffix.getStringValue(), spec.getColumnSpec(includedCol).getType());
final SingleCellFactory cellFac;
if (isPeriod) {
cellFac = new DateTimeShiftPeriodCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
} else {
cellFac = new DateTimeShiftDurationCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
}
rearranger.append(cellFac);
}
}
return rearranger;
}
use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.
the class ModifyTimeZoneNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec inSpec) throws InvalidSettingsException {
CheckUtils.checkSetting(m_hasValidatedConfiguration, "Node must be configured!");
final String modification = CheckUtils.checkSettingNotNull(m_modifyAction.getStringValue(), "must not be null");
CheckUtils.checkSetting(modification.equals(MODIFY_OPTION_SET) || modification.equals(MODIFY_OPTION_SHIFT) || modification.equals(MODIFY_OPTION_REMOVE), "Unknow modification operation '%s'. Most likely the parameter '%s' was controlled by " + "an invalid flow variable.", CFG_KEY_MODIFY_SELECT, m_modifyAction.getStringValue());
final ColumnRearranger rearranger = new ColumnRearranger(inSpec);
final String[] includeList = m_colSelect.applyTo(inSpec).getIncludes();
final int[] includeIndeces = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
int i = 0;
final ZoneId zone = m_timeZone.getZone();
DataType dataType;
if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_REMOVE)) {
dataType = LocalDateTimeCellFactory.TYPE;
} else {
dataType = ZonedDateTimeCellFactory.TYPE;
}
for (String includedCol : includeList) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
final DataColumnSpecCreator dataColumnSpecCreator = new DataColumnSpecCreator(includedCol, dataType);
final SingleCellFactory cellFac;
if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SET)) {
cellFac = new SetTimeZoneCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i++], zone);
} else if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SHIFT)) {
cellFac = new ShiftTimeZoneCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i++], zone);
} else {
cellFac = new RemoveTimeZoneCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i++]);
}
rearranger.replace(cellFac, includedCol);
} else {
DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(includedCol + m_suffix.getStringValue(), dataType);
final SingleCellFactory cellFac;
if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SET)) {
cellFac = new SetTimeZoneCellFactory(dataColSpec, includeIndeces[i++], zone);
} else if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SHIFT)) {
cellFac = new ShiftTimeZoneCellFactory(dataColSpec, includeIndeces[i++], zone);
} else {
cellFac = new RemoveTimeZoneCellFactory(dataColSpec, includeIndeces[i++]);
}
rearranger.append(cellFac);
}
}
return rearranger;
}
Aggregations