Use switch pattern matching

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-12-16 22:54:11 +01:00 committed by lsiepel
parent 2ab9822605
commit 7a3380a020

View File

@ -709,56 +709,52 @@ public class JdbcBaseDAO {
} }
} }
protected Instant objectAsInstant(Object v) { protected Instant objectAsInstant(Object o) {
if (v instanceof Long) { return switch (o) {
return Instant.ofEpochMilli(((Number) v).longValue()); case Long l -> Instant.ofEpochMilli(l.longValue());
} else if (v instanceof java.sql.Date objectAsDate) { case java.sql.Date d -> Instant.ofEpochMilli(d.getTime());
return Instant.ofEpochMilli(objectAsDate.getTime()); case LocalDateTime ldt -> ldt.atZone(ZoneId.systemDefault()).toInstant();
} else if (v instanceof LocalDateTime objectAsLocalDateTime) { case Instant i -> i;
return objectAsLocalDateTime.atZone(ZoneId.systemDefault()).toInstant(); case java.sql.Timestamp ts -> ts.toInstant();
} else if (v instanceof Instant objectAsInstant) { case java.lang.String s -> java.sql.Timestamp.valueOf(s).toInstant();
return objectAsInstant; default -> throw new UnsupportedOperationException(
} else if (v instanceof java.sql.Timestamp objectAsTimestamp) { "Date of type '" + o.getClass().getName() + "' is not supported");
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 Integer objectAsInteger(Object v) { protected Integer objectAsInteger(Object o) {
if (v instanceof Byte byteValue) { return switch (o) {
return byteValue.intValue(); case Byte b -> b.intValue();
} else if (v instanceof Integer intValue) { case Integer i -> i;
return intValue; case BigDecimal bd -> bd.intValue();
} else if (v instanceof BigDecimal bdValue) { default -> throw new UnsupportedOperationException(
return bdValue.intValue(); "Integer of type '" + o.getClass().getName() + "' is not supported");
} };
throw new UnsupportedOperationException("Integer of type '" + v.getClass().getName() + "' is not supported");
} }
protected Number objectAsNumber(Object value) { protected Number objectAsNumber(Object o) {
if (value instanceof Number valueAsNumber) { return switch (o) {
return valueAsNumber; case Number n -> n;
} default -> throw new UnsupportedOperationException(
throw new UnsupportedOperationException("Number of type '" + value.getClass().getName() + "' is not supported"); "Number of type '" + o.getClass().getName() + "' is not supported");
};
} }
protected BigDecimal objectAsBigDecimal(Object value) { protected BigDecimal objectAsBigDecimal(Object o) {
if (value instanceof BigDecimal valueAsBigDecimal) { return switch (o) {
return valueAsBigDecimal; case BigDecimal bd -> bd;
} default -> throw new UnsupportedOperationException(
throw new UnsupportedOperationException( "BigDecimal of type '" + o.getClass().getName() + "' is not supported");
"BigDecimal of type '" + value.getClass().getName() + "' is not supported"); };
} }
protected String objectAsString(Object v) { protected String objectAsString(Object o) {
if (v instanceof byte[] objectAsBytes) { return switch (o) {
return new String(objectAsBytes); case byte[] b -> new String(b);
} else if (v instanceof String objectAsString) { case String s -> s;
return objectAsString; default -> throw new UnsupportedOperationException(
} "String of type '" + o.getClass().getName() + "' is not supported");
throw new UnsupportedOperationException("String of type '" + v.getClass().getName() + "' is not supported"); };
} }
protected String formattedIdentifier(String identifier) { protected String formattedIdentifier(String identifier) {