use of java.time.Period in project knime-core by knime.
the class CreateDateTimeNodeDialog method updateWarningLabel.
private void updateWarningLabel() {
m_warningLabel.setText("");
// === check period duration field ===
if (m_dialogCompDuration.getModel().isEnabled() && m_updateWarningLabel) {
final DateTimeType selectedItem = (DateTimeType) m_typeCombobox.getSelectedItem();
Duration duration = null;
Period period = null;
if (((SettingsModelString) m_dialogCompDuration.getModel()).getStringValue() == null) {
m_warningLabel.setText("Please enter an interval.");
return;
}
try {
duration = DurationPeriodFormatUtils.parseDuration(((SettingsModelString) m_dialogCompDuration.getModel()).getStringValue());
if (selectedItem.equals(DateTimeType.LOCAL_DATE)) {
m_warningLabel.setText("A time-based duration cannot be applied to a local date.");
return;
}
} catch (DateTimeParseException e1) {
try {
period = DurationPeriodFormatUtils.parsePeriod(((SettingsModelString) m_dialogCompDuration.getModel()).getStringValue());
if (selectedItem.equals(DateTimeType.LOCAL_TIME)) {
m_warningLabel.setText("A date-based duration cannot be applied to a local time.");
return;
}
} catch (DateTimeParseException e2) {
m_warningLabel.setText("Value does not represent a duration.");
return;
}
}
if (((SettingsModelString) m_dialogCompRowNrOptionSelection.getModel()).getStringValue().equals(RowNrMode.Variable.name())) {
// === check that duration is not zero and row number variable ===
if ((duration != null && duration.isZero()) || (period != null && period.isZero())) {
m_warningLabel.setText("Interval must not be zero.");
}
// === check that start is before end and duration positive and vice versa ===
final Temporal start = ((SettingsModelBoolean) m_dialogCompStartUseExecTime.getModel()).getBooleanValue() ? CreateDateTimeNodeModel.getTemporalExecTimeWithFormat(((SettingsModelDateTime) m_dialogCompEnd.getModel()).getSelectedDateTime()) : ((SettingsModelDateTime) m_dialogCompStart.getModel()).getSelectedDateTime();
final Temporal end = ((SettingsModelBoolean) m_dialogCompEndUseExecTime.getModel()).getBooleanValue() ? CreateDateTimeNodeModel.getTemporalExecTimeWithFormat(((SettingsModelDateTime) m_dialogCompStart.getModel()).getSelectedDateTime()) : ((SettingsModelDateTime) m_dialogCompEnd.getModel()).getSelectedDateTime();
if (!selectedItem.equals(DateTimeType.LOCAL_TIME)) {
final boolean isStartBeforeEnd;
final boolean isEqual;
if (selectedItem.equals(DateTimeType.LOCAL_DATE)) {
isStartBeforeEnd = ((LocalDate) start).isBefore((LocalDate) end);
isEqual = ((LocalDate) start).isEqual((LocalDate) end);
} else if (selectedItem.equals(DateTimeType.LOCAL_DATE_TIME)) {
isStartBeforeEnd = ((LocalDateTime) start).isBefore((LocalDateTime) end);
isEqual = ((LocalDateTime) start).isEqual((LocalDateTime) end);
} else {
isStartBeforeEnd = (((ZonedDateTime) start)).toLocalDateTime().isBefore(((LocalDateTime) end));
isEqual = (((ZonedDateTime) start)).toLocalDateTime().isEqual(((LocalDateTime) end));
}
final boolean isDurationNegative;
if (duration != null) {
isDurationNegative = duration.isNegative();
} else if (period != null) {
isDurationNegative = period.isNegative();
} else {
throw new IllegalStateException("Either duration or period must not be null!");
}
if (isStartBeforeEnd && isDurationNegative) {
m_warningLabel.setText("Start is before end, but the interval is negative.");
return;
}
if (!isStartBeforeEnd && !isEqual && !isDurationNegative) {
m_warningLabel.setText("Start is after end, but the interval is positive.");
return;
}
}
}
}
}
use of java.time.Period in project knime-core by knime.
the class CreateDateTimeNodeModel method createByVariableRowNr.
/**
* Create date&time row with a variable number of rows depending on a given starting point, a duration/period and an
* ending point.
*/
private void createByVariableRowNr(final BufferedDataContainer container, final Temporal startDateTime, final Temporal endDateTime, final TemporalAmount durationOrPeriod) throws InvalidSettingsException {
Temporal start = startDateTime;
Temporal end = endDateTime;
// check if duration is zero
if ((durationOrPeriod instanceof Period) && ((Period) durationOrPeriod).isZero()) {
setWarningMessage("Interval is zero! Node created an empty table.");
return;
} else if ((durationOrPeriod instanceof Duration) && ((Duration) durationOrPeriod).isZero()) {
setWarningMessage("Interval is zero! Node created an empty table.");
return;
}
// === check if start date is after end ===
boolean wasLocalTime = start instanceof LocalTime;
final boolean isStartAfterEnd;
if (start instanceof LocalDate) {
isStartAfterEnd = ((LocalDate) start).isAfter((LocalDate) end);
} else if (start instanceof LocalTime) {
// because of the problem that 00:00 is before 23:59, a local time needs to be temporarily converted
// to a local date time
boolean isLocalTimeStartAfterEnd = ((LocalTime) start).isAfter((LocalTime) end);
final boolean isDurationNegative = ((Duration) durationOrPeriod).isNegative();
int daysAddedToEnd = 0;
if (isLocalTimeStartAfterEnd && !isDurationNegative) {
daysAddedToEnd = 1;
}
if (!isLocalTimeStartAfterEnd && isDurationNegative) {
daysAddedToEnd = -1;
}
if (start.equals(end)) {
daysAddedToEnd = 0;
}
final int dayOfYear = 10;
start = LocalDateTime.of(LocalDate.ofYearDay(2010, dayOfYear), (LocalTime) start);
end = LocalDateTime.of(LocalDate.ofYearDay(2010, dayOfYear + daysAddedToEnd), (LocalTime) end);
isStartAfterEnd = ((LocalDateTime) start).isAfter((LocalDateTime) end);
} else if (start instanceof LocalDateTime) {
isStartAfterEnd = ((LocalDateTime) start).isAfter((LocalDateTime) end);
} else {
isStartAfterEnd = ((ZonedDateTime) start).isAfter((ZonedDateTime) end);
}
// === check if input is legal: duration/period needs to be positive if end is after start and vice versa ===
final String warningMsgPos = "Interval must be positive, if end is after start date! Node created an empty table.";
final String warningMsgNeg = "Interval must be negative, if end is before start date! Node created an empty table.";
if (start instanceof LocalDate) {
if ((((LocalDate) end).isAfter((LocalDate) start)) && (((LocalDate) start.plus(durationOrPeriod)).isBefore((LocalDate) start))) {
setWarningMessage(warningMsgPos);
return;
}
if ((((LocalDate) end).isBefore((LocalDate) start)) && (((LocalDate) start.plus(durationOrPeriod)).isAfter((LocalDate) start))) {
setWarningMessage(warningMsgNeg);
return;
}
} else if (start instanceof LocalDateTime) {
if ((((LocalDateTime) end).isAfter((LocalDateTime) start)) && (((LocalDateTime) start.plus(durationOrPeriod)).isBefore((LocalDateTime) start))) {
setWarningMessage(warningMsgPos);
return;
}
if ((((LocalDateTime) end).isBefore((LocalDateTime) start)) && (((LocalDateTime) start.plus(durationOrPeriod)).isAfter((LocalDateTime) start))) {
setWarningMessage(warningMsgNeg);
return;
}
} else if (start instanceof ZonedDateTime) {
if ((((ZonedDateTime) end).isAfter((ZonedDateTime) start)) && (((ZonedDateTime) start.plus(durationOrPeriod)).isBefore((ZonedDateTime) start))) {
setWarningMessage(warningMsgPos);
return;
}
if ((((ZonedDateTime) end).isBefore((ZonedDateTime) start)) && (((ZonedDateTime) start.plus(durationOrPeriod)).isAfter((ZonedDateTime) start))) {
setWarningMessage(warningMsgNeg);
return;
}
}
// === create rows ===
Temporal currentDateTime = start;
long row_idx = 0;
while (true) {
final DataCell dataCell;
final boolean isEqual = currentDateTime.equals(end);
final boolean isCurrentAfterEnd;
if (currentDateTime instanceof LocalDate) {
isCurrentAfterEnd = ((LocalDate) currentDateTime).isAfter((LocalDate) end);
dataCell = LocalDateCellFactory.create((LocalDate) currentDateTime);
} else if (currentDateTime instanceof LocalDateTime) {
isCurrentAfterEnd = ((LocalDateTime) currentDateTime).isAfter((LocalDateTime) end);
if (wasLocalTime) {
dataCell = LocalTimeCellFactory.create((((LocalDateTime) currentDateTime).truncatedTo(ChronoUnit.MILLIS)).toLocalTime());
} else {
dataCell = LocalDateTimeCellFactory.create(((LocalDateTime) currentDateTime).truncatedTo(ChronoUnit.MILLIS));
}
} else {
isCurrentAfterEnd = ((ZonedDateTime) currentDateTime).isAfter((ZonedDateTime) end);
dataCell = ZonedDateTimeCellFactory.create(((ZonedDateTime) currentDateTime).truncatedTo(ChronoUnit.MILLIS));
}
if ((isCurrentAfterEnd && !isStartAfterEnd) || (!isCurrentAfterEnd && !isEqual && isStartAfterEnd)) {
break;
}
container.addRowToTable(new DefaultRow(new RowKey("Row" + row_idx++), dataCell));
if (isEqual) {
break;
}
currentDateTime = currentDateTime.plus(durationOrPeriod);
}
}
use of java.time.Period in project knime-core by knime.
the class PeriodCellSerializer method serialize.
@Override
public void serialize(final PeriodCell cell, final DataCellDataOutput output) throws IOException {
final Period period = cell.getPeriod();
final int years = period.getYears();
final int months = period.getMonths();
final int days = period.getDays();
output.writeInt(years);
output.writeInt(months);
output.writeInt(days);
}
use of java.time.Period in project knime-core by knime.
the class PeriodISOValueRenderer method setValue.
@Override
protected void setValue(final Object value) {
if (value instanceof PeriodValue) {
Period p = ((PeriodValue) value).getPeriod();
super.setValue(p.toString());
} else {
super.setValue(value);
}
}
use of java.time.Period in project knime-core by knime.
the class PeriodShortValueRenderer method setValue.
@Override
protected void setValue(final Object value) {
if (value instanceof PeriodValue) {
Period period = ((PeriodValue) value).getPeriod();
super.setValue(DurationPeriodFormatUtils.formatPeriodShort(period));
} else {
super.setValue(value);
}
}
Aggregations