Skip to content

Commit 7a3380a

Browse files
jlaurlsiepel
authored andcommitted
Use switch pattern matching
Signed-off-by: Jacob Laursen <[email protected]>
1 parent 2ab9822 commit 7a3380a

File tree

1 file changed

+46
-50
lines changed
  • bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db

1 file changed

+46
-50
lines changed

bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAO.java

+46-50
Original file line numberDiff line numberDiff line change
@@ -709,56 +709,52 @@ protected State objectAsState(Item item, @Nullable Unit<? extends Quantity<?>> u
709709
}
710710
}
711711

712-
protected Instant objectAsInstant(Object v) {
713-
if (v instanceof Long) {
714-
return Instant.ofEpochMilli(((Number) v).longValue());
715-
} else if (v instanceof java.sql.Date objectAsDate) {
716-
return Instant.ofEpochMilli(objectAsDate.getTime());
717-
} else if (v instanceof LocalDateTime objectAsLocalDateTime) {
718-
return objectAsLocalDateTime.atZone(ZoneId.systemDefault()).toInstant();
719-
} else if (v instanceof Instant objectAsInstant) {
720-
return objectAsInstant;
721-
} else if (v instanceof java.sql.Timestamp objectAsTimestamp) {
722-
return objectAsTimestamp.toInstant();
723-
} else if (v instanceof java.lang.String objectAsString) {
724-
return java.sql.Timestamp.valueOf(objectAsString).toInstant();
725-
}
726-
throw new UnsupportedOperationException("Date of type '" + v.getClass().getName() + "' is not supported");
727-
}
728-
729-
protected Integer objectAsInteger(Object v) {
730-
if (v instanceof Byte byteValue) {
731-
return byteValue.intValue();
732-
} else if (v instanceof Integer intValue) {
733-
return intValue;
734-
} else if (v instanceof BigDecimal bdValue) {
735-
return bdValue.intValue();
736-
}
737-
throw new UnsupportedOperationException("Integer of type '" + v.getClass().getName() + "' is not supported");
738-
}
739-
740-
protected Number objectAsNumber(Object value) {
741-
if (value instanceof Number valueAsNumber) {
742-
return valueAsNumber;
743-
}
744-
throw new UnsupportedOperationException("Number of type '" + value.getClass().getName() + "' is not supported");
745-
}
746-
747-
protected BigDecimal objectAsBigDecimal(Object value) {
748-
if (value instanceof BigDecimal valueAsBigDecimal) {
749-
return valueAsBigDecimal;
750-
}
751-
throw new UnsupportedOperationException(
752-
"BigDecimal of type '" + value.getClass().getName() + "' is not supported");
753-
}
754-
755-
protected String objectAsString(Object v) {
756-
if (v instanceof byte[] objectAsBytes) {
757-
return new String(objectAsBytes);
758-
} else if (v instanceof String objectAsString) {
759-
return objectAsString;
760-
}
761-
throw new UnsupportedOperationException("String of type '" + v.getClass().getName() + "' is not supported");
712+
protected Instant objectAsInstant(Object o) {
713+
return switch (o) {
714+
case Long l -> Instant.ofEpochMilli(l.longValue());
715+
case java.sql.Date d -> Instant.ofEpochMilli(d.getTime());
716+
case LocalDateTime ldt -> ldt.atZone(ZoneId.systemDefault()).toInstant();
717+
case Instant i -> i;
718+
case java.sql.Timestamp ts -> ts.toInstant();
719+
case java.lang.String s -> java.sql.Timestamp.valueOf(s).toInstant();
720+
default -> throw new UnsupportedOperationException(
721+
"Date of type '" + o.getClass().getName() + "' is not supported");
722+
};
723+
}
724+
725+
protected Integer objectAsInteger(Object o) {
726+
return switch (o) {
727+
case Byte b -> b.intValue();
728+
case Integer i -> i;
729+
case BigDecimal bd -> bd.intValue();
730+
default -> throw new UnsupportedOperationException(
731+
"Integer of type '" + o.getClass().getName() + "' is not supported");
732+
};
733+
}
734+
735+
protected Number objectAsNumber(Object o) {
736+
return switch (o) {
737+
case Number n -> n;
738+
default -> throw new UnsupportedOperationException(
739+
"Number of type '" + o.getClass().getName() + "' is not supported");
740+
};
741+
}
742+
743+
protected BigDecimal objectAsBigDecimal(Object o) {
744+
return switch (o) {
745+
case BigDecimal bd -> bd;
746+
default -> throw new UnsupportedOperationException(
747+
"BigDecimal of type '" + o.getClass().getName() + "' is not supported");
748+
};
749+
}
750+
751+
protected String objectAsString(Object o) {
752+
return switch (o) {
753+
case byte[] b -> new String(b);
754+
case String s -> s;
755+
default -> throw new UnsupportedOperationException(
756+
"String of type '" + o.getClass().getName() + "' is not supported");
757+
};
762758
}
763759

764760
protected String formattedIdentifier(String identifier) {

0 commit comments

Comments
 (0)