From 7a3380a020f97ab895716f234988a6ee52eab1da Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Mon, 16 Dec 2024 22:54:11 +0100 Subject: [PATCH] Use switch pattern matching Signed-off-by: Jacob Laursen --- .../jdbc/internal/db/JdbcBaseDAO.java | 80 +++++++++---------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAO.java b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAO.java index 20f945e32c2..84d5c6dc8a2 100644 --- a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAO.java +++ b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAO.java @@ -709,56 +709,52 @@ public class JdbcBaseDAO { } } - protected Instant objectAsInstant(Object v) { - if (v instanceof Long) { - return Instant.ofEpochMilli(((Number) v).longValue()); - } else if (v instanceof java.sql.Date objectAsDate) { - return Instant.ofEpochMilli(objectAsDate.getTime()); - } else if (v instanceof LocalDateTime objectAsLocalDateTime) { - return objectAsLocalDateTime.atZone(ZoneId.systemDefault()).toInstant(); - } else if (v instanceof Instant objectAsInstant) { - return objectAsInstant; - } else if (v instanceof java.sql.Timestamp objectAsTimestamp) { - return objectAsTimestamp.toInstant(); - } else if (v instanceof java.lang.String objectAsString) { - return java.sql.Timestamp.valueOf(objectAsString).toInstant(); - } - throw new UnsupportedOperationException("Date of type '" + v.getClass().getName() + "' is not supported"); + protected Instant objectAsInstant(Object o) { + return switch (o) { + case Long l -> Instant.ofEpochMilli(l.longValue()); + case java.sql.Date d -> Instant.ofEpochMilli(d.getTime()); + case LocalDateTime ldt -> ldt.atZone(ZoneId.systemDefault()).toInstant(); + case Instant i -> i; + case java.sql.Timestamp ts -> ts.toInstant(); + case java.lang.String s -> java.sql.Timestamp.valueOf(s).toInstant(); + default -> throw new UnsupportedOperationException( + "Date of type '" + o.getClass().getName() + "' is not supported"); + }; } - protected Integer objectAsInteger(Object v) { - if (v instanceof Byte byteValue) { - return byteValue.intValue(); - } else if (v instanceof Integer intValue) { - return intValue; - } else if (v instanceof BigDecimal bdValue) { - return bdValue.intValue(); - } - throw new UnsupportedOperationException("Integer of type '" + v.getClass().getName() + "' is not supported"); + protected Integer objectAsInteger(Object o) { + return switch (o) { + case Byte b -> b.intValue(); + case Integer i -> i; + case BigDecimal bd -> bd.intValue(); + default -> throw new UnsupportedOperationException( + "Integer of type '" + o.getClass().getName() + "' is not supported"); + }; } - protected Number objectAsNumber(Object value) { - if (value instanceof Number valueAsNumber) { - return valueAsNumber; - } - throw new UnsupportedOperationException("Number of type '" + value.getClass().getName() + "' is not supported"); + protected Number objectAsNumber(Object o) { + return switch (o) { + case Number n -> n; + default -> throw new UnsupportedOperationException( + "Number of type '" + o.getClass().getName() + "' is not supported"); + }; } - protected BigDecimal objectAsBigDecimal(Object value) { - if (value instanceof BigDecimal valueAsBigDecimal) { - return valueAsBigDecimal; - } - throw new UnsupportedOperationException( - "BigDecimal of type '" + value.getClass().getName() + "' is not supported"); + protected BigDecimal objectAsBigDecimal(Object o) { + return switch (o) { + case BigDecimal bd -> bd; + default -> throw new UnsupportedOperationException( + "BigDecimal of type '" + o.getClass().getName() + "' is not supported"); + }; } - protected String objectAsString(Object v) { - if (v instanceof byte[] objectAsBytes) { - return new String(objectAsBytes); - } else if (v instanceof String objectAsString) { - return objectAsString; - } - throw new UnsupportedOperationException("String of type '" + v.getClass().getName() + "' is not supported"); + protected String objectAsString(Object o) { + return switch (o) { + case byte[] b -> new String(b); + case String s -> s; + default -> throw new UnsupportedOperationException( + "String of type '" + o.getClass().getName() + "' is not supported"); + }; } protected String formattedIdentifier(String identifier) {