From 92f571940edbb65c423d47b6881db638572fe466 Mon Sep 17 00:00:00 2001 From: dan-s1 Date: Wed, 16 Oct 2024 19:28:52 -0400 Subject: [PATCH] NIFI-13880 Replaced if statements with enhanched switch in Object FieldConverters (#9399) Signed-off-by: David Handermann --- .../field/ObjectLocalDateFieldConverter.java | 74 ++++++++------- .../ObjectLocalDateTimeFieldConverter.java | 61 ++++++------ .../field/ObjectLocalTimeFieldConverter.java | 77 ++++++++-------- .../ObjectOffsetDateTimeFieldConverter.java | 67 +++++++------- .../field/ObjectStringFieldConverter.java | 92 ++++++++++--------- 5 files changed, 195 insertions(+), 176 deletions(-) diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalDateFieldConverter.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalDateFieldConverter.java index 61e3a9b86d6a..63f2633998fd 100644 --- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalDateFieldConverter.java +++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalDateFieldConverter.java @@ -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; @@ -40,46 +41,49 @@ class ObjectLocalDateFieldConverter implements FieldConverter */ @Override public LocalDate convertField(final Object field, final Optional 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); diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalDateTimeFieldConverter.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalDateTimeFieldConverter.java index 37d8fbe3ada4..19070f229cab 100644 --- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalDateTimeFieldConverter.java +++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalDateTimeFieldConverter.java @@ -45,39 +45,43 @@ class ObjectLocalDateTimeFieldConverter implements FieldConverter 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 -> { } } @@ -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) { diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalTimeFieldConverter.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalTimeFieldConverter.java index b93984138ac4..178993ed9317 100644 --- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalTimeFieldConverter.java +++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectLocalTimeFieldConverter.java @@ -43,48 +43,51 @@ class ObjectLocalTimeFieldConverter implements FieldConverter */ @Override public LocalTime convertField(final Object field, final Optional 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); diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectOffsetDateTimeFieldConverter.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectOffsetDateTimeFieldConverter.java index 390e41e3a76d..18d0a06b0087 100644 --- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectOffsetDateTimeFieldConverter.java +++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectOffsetDateTimeFieldConverter.java @@ -41,43 +41,46 @@ class ObjectOffsetDateTimeFieldConverter implements FieldConverter 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); diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectStringFieldConverter.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectStringFieldConverter.java index 563e564be771..6d09608f20ef 100644 --- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectStringFieldConverter.java +++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectStringFieldConverter.java @@ -21,11 +21,13 @@ import java.io.Reader; import java.nio.charset.StandardCharsets; import java.sql.Clob; +import java.sql.Timestamp; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.util.Date; import java.util.Optional; /** @@ -43,54 +45,58 @@ class ObjectStringFieldConverter implements FieldConverter { */ @Override public String convertField(final Object field, final Optional pattern, final String name) { - if (field == null) { - return null; - } - if (field instanceof String) { - return field.toString(); - } - if (field instanceof java.sql.Timestamp timestamp) { - if (pattern.isEmpty()) { - return Long.toString(timestamp.getTime()); + switch (field) { + case null -> { + return null; } - final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get()); - final LocalDateTime localDateTime = timestamp.toLocalDateTime(); + case String ignored -> { + return field.toString(); + } + case Timestamp timestamp -> { + if (pattern.isEmpty()) { + return Long.toString(timestamp.getTime()); + } + final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get()); + final LocalDateTime localDateTime = timestamp.toLocalDateTime(); - // Convert LocalDateTime to ZonedDateTime using system default zone to support offsets in Date Time Formatter - final ZonedDateTime dateTime = ZonedDateTime.of(localDateTime, ZoneId.systemDefault()); - return formatter.format(dateTime); - } - if (field instanceof java.util.Date date) { - if (pattern.isEmpty()) { - return Long.toString(date.getTime()); + // Convert LocalDateTime to ZonedDateTime using system default zone to support offsets in Date Time Formatter + final ZonedDateTime dateTime = ZonedDateTime.of(localDateTime, ZoneId.systemDefault()); + return formatter.format(dateTime); } - final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get()); - // java.sql.Date and java.sql.Time do not support toInstant() - final Instant instant = Instant.ofEpochMilli(date.getTime()); - final ZonedDateTime dateTime = instant.atZone(ZoneId.systemDefault()); - return formatter.format(dateTime); - } - if (field instanceof byte[] bytes) { - return new String(bytes, StandardCharsets.UTF_16); - } - if (field instanceof Byte[] bytes) { - final byte[] converted = new byte[bytes.length]; - for (int i = 0; i < bytes.length; i++) { - converted[i] = bytes[i]; + case Date date -> { + if (pattern.isEmpty()) { + return Long.toString(date.getTime()); + } + final DateTimeFormatter formatter = DateTimeFormatterRegistry.getDateTimeFormatter(pattern.get()); + // java.sql.Date and java.sql.Time do not support toInstant() + final Instant instant = Instant.ofEpochMilli(date.getTime()); + final ZonedDateTime dateTime = instant.atZone(ZoneId.systemDefault()); + return formatter.format(dateTime); } - return new String(converted, StandardCharsets.UTF_16); - } - if (field instanceof Clob clob) { - final StringBuilder builder = new StringBuilder(); - final char[] buffer = new char[32768]; - try (Reader reader = clob.getCharacterStream()) { - int charsRead; - while ((charsRead = reader.read(buffer)) != -1) { - builder.append(buffer, 0, charsRead); + case byte[] bytes -> { + return new String(bytes, StandardCharsets.UTF_16); + } + case Byte[] bytes -> { + final byte[] converted = new byte[bytes.length]; + for (int i = 0; i < bytes.length; i++) { + converted[i] = bytes[i]; } - return builder.toString(); - } catch (final Exception e) { - throw new FieldConversionException(String.class, field, name, e); + return new String(converted, StandardCharsets.UTF_16); + } + case Clob clob -> { + final StringBuilder builder = new StringBuilder(); + final char[] buffer = new char[32768]; + try (Reader reader = clob.getCharacterStream()) { + int charsRead; + while ((charsRead = reader.read(buffer)) != -1) { + builder.append(buffer, 0, charsRead); + } + return builder.toString(); + } catch (final Exception e) { + throw new FieldConversionException(String.class, field, name, e); + } + } + default -> { } } return field.toString();