Skip to content

Commit

Permalink
NIFI-13880 Replaced if statements with enhanched switch in Object Fie…
Browse files Browse the repository at this point in the history
…ldConverters (#9399)

Signed-off-by: David Handermann <[email protected]>
  • Loading branch information
dan-s1 authored Oct 16, 2024
1 parent 1fe54f0 commit 92f5719
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.nifi.serialization.record.util.IllegalTypeConversionException;

import java.sql.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
Expand All @@ -40,46 +41,49 @@ class ObjectLocalDateFieldConverter implements FieldConverter<Object, LocalDate>
*/
@Override
public LocalDate convertField(final Object field, final Optional<String> pattern, final String name) {
if (field == null) {
return null;
}
if (field instanceof LocalDate) {
return (LocalDate) field;
}
if (field instanceof java.sql.Date date) {
return date.toLocalDate();
}
if (field instanceof java.util.Date date) {
final Instant instant = date.toInstant();
return ofInstant(instant);
}
if (field instanceof Number) {
final Number number = (Number) field;
final Instant instant = Instant.ofEpochMilli(number.longValue());
return ofInstant(instant);
}
if (field instanceof String) {
final String string = field.toString().trim();
if (string.isEmpty()) {
switch (field) {
case null -> {
return null;
}

if (pattern.isPresent()) {
final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get());
try {
return LocalDate.parse(string, formatter);
} catch (final DateTimeParseException e) {
throw new FieldConversionException(LocalDate.class, field, name, e);
case LocalDate localDate -> {
return localDate;
}
case Date date -> {
return date.toLocalDate();
}
case java.util.Date date -> {
final Instant instant = date.toInstant();
return ofInstant(instant);
}
case Number number -> {
final Instant instant = Instant.ofEpochMilli(number.longValue());
return ofInstant(instant);
}
case String ignored -> {
final String string = field.toString().trim();
if (string.isEmpty()) {
return null;
}
} else {
try {
final long number = Long.parseLong(string);
final Instant instant = Instant.ofEpochMilli(number);
return ofInstant(instant);
} catch (final NumberFormatException e) {
throw new FieldConversionException(LocalDate.class, field, name, e);

if (pattern.isPresent()) {
final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get());
try {
return LocalDate.parse(string, formatter);
} catch (final DateTimeParseException e) {
throw new FieldConversionException(LocalDate.class, field, name, e);
}
} else {
try {
final long number = Long.parseLong(string);
final Instant instant = Instant.ofEpochMilli(number);
return ofInstant(instant);
} catch (final NumberFormatException e) {
throw new FieldConversionException(LocalDate.class, field, name, e);
}
}
}
default -> {
}
}

throw new FieldConversionException(LocalDate.class, field, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,43 @@ class ObjectLocalDateTimeFieldConverter implements FieldConverter<Object, LocalD
*/
@Override
public LocalDateTime convertField(final Object field, final Optional<String> pattern, final String name) {
if (field == null) {
return null;
}
if (field instanceof LocalDateTime) {
return (LocalDateTime) field;
}
if (field instanceof Date date) {
final Instant instant = Instant.ofEpochMilli(date.getTime());
return ofInstant(instant);
}
if (field instanceof final Number number) {
// If value is a floating point number, we consider it as seconds since epoch plus a decimal part for fractions of a second.
if (field instanceof Double || field instanceof Float) {
return toLocalDateTime(number.doubleValue());
switch (field) {
case null -> {
return null;
}
case LocalDateTime localDateTime -> {
return localDateTime;
}
case Date date -> {
final Instant instant = Instant.ofEpochMilli(date.getTime());
return ofInstant(instant);
}
case final Number number -> {
// If value is a floating point number, we consider it as seconds since epoch plus a decimal part for fractions of a second.
if (field instanceof Double || field instanceof Float) {
return toLocalDateTime(number.doubleValue());
}

return toLocalDateTime(number.longValue());
}
if (field instanceof String) {
final String string = field.toString().trim();
if (string.isEmpty()) {
return null;
return toLocalDateTime(number.longValue());
}
case String ignored -> {
final String string = field.toString().trim();
if (string.isEmpty()) {
return null;
}

if (pattern.isPresent()) {
final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get());
try {
return parseLocalDateTime(field, name, string, formatter);
} catch (final DateTimeParseException e) {
if (pattern.isPresent()) {
final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get());
try {
return parseLocalDateTime(field, name, string, formatter);
} catch (final DateTimeParseException e) {
return tryParseAsNumber(string, name);
}
} else {
return tryParseAsNumber(string, name);
}
} else {
return tryParseAsNumber(string, name);
}
default -> {
}
}

Expand Down Expand Up @@ -141,9 +145,8 @@ private LocalDateTime toLocalDateTime(final long value) {
}

final Instant instant = Instant.ofEpochMilli(value);
final LocalDateTime localDateTime = ofInstant(instant);

return localDateTime;
return ofInstant(instant);
}

private LocalDateTime ofInstant(final Instant instant) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,48 +43,51 @@ class ObjectLocalTimeFieldConverter implements FieldConverter<Object, LocalTime>
*/
@Override
public LocalTime convertField(final Object field, final Optional<String> pattern, final String name) {
if (field == null) {
return null;
}
if (field instanceof LocalTime) {
return (LocalTime) field;
}
if (field instanceof Time time) {
// Convert to Instant preserving millisecond precision
final long epochMilli = time.getTime();
final Instant instant = Instant.ofEpochMilli(epochMilli);
return LocalTime.ofInstant(instant, ZoneId.systemDefault());
}
if (field instanceof Date date) {
return ofInstant(Instant.ofEpochMilli(date.getTime()));
}
if (field instanceof Number) {
final Number number = (Number) field;
final Instant instant = Instant.ofEpochMilli(number.longValue());
return ofInstant(instant);
}
if (field instanceof String) {
final String string = field.toString().trim();
if (string.isEmpty()) {
switch (field) {
case null -> {
return null;
}

if (pattern.isPresent()) {
final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get());
try {
return LocalTime.parse(string, formatter);
} catch (final DateTimeParseException e) {
throw new FieldConversionException(LocalTime.class, field, name, e);
case LocalTime localTime -> {
return localTime;
}
case Time time -> {
// Convert to an Instant preserving millisecond precision
final long epochMilli = time.getTime();
final Instant instant = Instant.ofEpochMilli(epochMilli);
return LocalTime.ofInstant(instant, ZoneId.systemDefault());
}
case Date date -> {
return ofInstant(Instant.ofEpochMilli(date.getTime()));
}
case Number number -> {
final Instant instant = Instant.ofEpochMilli(number.longValue());
return ofInstant(instant);
}
case String ignored -> {
final String string = field.toString().trim();
if (string.isEmpty()) {
return null;
}
} else {
try {
final long number = Long.parseLong(string);
final Instant instant = Instant.ofEpochMilli(number);
return ofInstant(instant);
} catch (final NumberFormatException e) {
throw new FieldConversionException(LocalTime.class, field, name, e);

if (pattern.isPresent()) {
final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get());
try {
return LocalTime.parse(string, formatter);
} catch (final DateTimeParseException e) {
throw new FieldConversionException(LocalTime.class, field, name, e);
}
} else {
try {
final long number = Long.parseLong(string);
final Instant instant = Instant.ofEpochMilli(number);
return ofInstant(instant);
} catch (final NumberFormatException e) {
throw new FieldConversionException(LocalTime.class, field, name, e);
}
}
}
default -> {
}
}

throw new FieldConversionException(LocalTime.class, field, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,46 @@ class ObjectOffsetDateTimeFieldConverter implements FieldConverter<Object, Offse
*/
@Override
public OffsetDateTime convertField(final Object field, final Optional<String> pattern, final String name) {
if (field == null) {
return null;
}
if (field instanceof OffsetDateTime) {
return (OffsetDateTime) field;
}
if (field instanceof Date date) {
final Instant instant = date.toInstant();
return ofInstant(instant);
}
if (field instanceof Number) {
final Number number = (Number) field;
final Instant instant = Instant.ofEpochMilli(number.longValue());
return ofInstant(instant);
}
if (field instanceof String) {
final String string = field.toString().trim();
if (string.isEmpty()) {
switch (field) {
case null -> {
return null;
}

if (pattern.isPresent()) {
final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get());
try {
return OffsetDateTime.parse(string, formatter);
} catch (final DateTimeParseException e) {
throw new FieldConversionException(OffsetDateTime.class, field, name, e);
case OffsetDateTime offsetDateTime -> {
return offsetDateTime;
}
case Date date -> {
final Instant instant = date.toInstant();
return ofInstant(instant);
}
case Number number -> {
final Instant instant = Instant.ofEpochMilli(number.longValue());
return ofInstant(instant);
}
case String ignored -> {
final String string = field.toString().trim();
if (string.isEmpty()) {
return null;
}
} else {
try {
final long number = Long.parseLong(string);
final Instant instant = Instant.ofEpochMilli(number);
return ofInstant(instant);
} catch (final NumberFormatException e) {
throw new FieldConversionException(OffsetDateTime.class, field, name, e);

if (pattern.isPresent()) {
final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get());
try {
return OffsetDateTime.parse(string, formatter);
} catch (final DateTimeParseException e) {
throw new FieldConversionException(OffsetDateTime.class, field, name, e);
}
} else {
try {
final long number = Long.parseLong(string);
final Instant instant = Instant.ofEpochMilli(number);
return ofInstant(instant);
} catch (final NumberFormatException e) {
throw new FieldConversionException(OffsetDateTime.class, field, name, e);
}
}
}
default -> {
}
}

throw new FieldConversionException(OffsetDateTime.class, field, name);
Expand Down
Loading

0 comments on commit 92f5719

Please sign in to comment.