start using flow typing in the JavaTypes

- but definitely not done with this

squash

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-10-30 10:28:00 +01:00
parent 65bc730cc1
commit 26988dd536
22 changed files with 204 additions and 231 deletions

View File

@ -82,17 +82,17 @@ public class BigDecimalJavaType extends AbstractClassJavaType<BigDecimal> {
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if ( value instanceof BigDecimal ) { if ( value instanceof BigDecimal bigDecimal ) {
return (BigDecimal) value; return bigDecimal;
} }
if ( value instanceof BigInteger ) { if ( value instanceof BigInteger bigInteger ) {
return new BigDecimal( (BigInteger) value ); return new BigDecimal( bigInteger );
} }
if ( value instanceof Number ) { if ( value instanceof Number number ) {
return BigDecimal.valueOf( ( (Number) value ).doubleValue() ); return BigDecimal.valueOf( number.doubleValue() );
} }
if ( value instanceof String ) { if ( value instanceof String string ) {
return new BigDecimal( (String) value ); return new BigDecimal( string );
} }
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );
} }
@ -125,17 +125,17 @@ public class BigDecimalJavaType extends AbstractClassJavaType<BigDecimal> {
return null; return null;
} }
if ( value instanceof BigDecimal ) { if ( value instanceof BigDecimal bigDecimal ) {
return (BigDecimal) value; return bigDecimal;
} }
if ( value instanceof Number ) { if ( value instanceof Number number ) {
return BigDecimal.valueOf( ( (Number) value ).doubleValue() ); return BigDecimal.valueOf( number.doubleValue() );
} }
if ( value instanceof String ) { if ( value instanceof String string ) {
return CoercionHelper.coerceWrappingError( return CoercionHelper.coerceWrappingError(
() -> BigDecimal.valueOf( Double.parseDouble( (String) value ) ) () -> BigDecimal.valueOf( Double.parseDouble( string ) )
); );
} }

View File

@ -80,17 +80,17 @@ public class BigIntegerJavaType extends AbstractClassJavaType<BigInteger> {
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if ( value instanceof BigInteger ) { if ( value instanceof BigInteger bigInteger ) {
return (BigInteger) value; return bigInteger;
} }
if ( value instanceof BigDecimal ) { if ( value instanceof BigDecimal bigDecimal ) {
return ( (BigDecimal) value ).toBigIntegerExact(); return bigDecimal.toBigIntegerExact();
} }
if ( value instanceof Number ) { if ( value instanceof Number number ) {
return BigInteger.valueOf( ( (Number) value ).longValue() ); return BigInteger.valueOf( number.longValue() );
} }
if ( value instanceof String ) { if ( value instanceof String string ) {
return new BigInteger( (String) value ); return new BigInteger( string );
} }
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );
} }
@ -127,41 +127,41 @@ public class BigIntegerJavaType extends AbstractClassJavaType<BigInteger> {
return null; return null;
} }
if ( value instanceof BigInteger ) { if ( value instanceof BigInteger bigInteger ) {
return (BigInteger) value; return bigInteger;
} }
if ( value instanceof Byte ) { if ( value instanceof Byte byteValue ) {
return BigInteger.valueOf( ( (Byte) value ) ); return BigInteger.valueOf( byteValue );
} }
if ( value instanceof Short ) { if ( value instanceof Short shortValue ) {
return BigInteger.valueOf( ( (Short) value ) ); return BigInteger.valueOf( shortValue );
} }
if ( value instanceof Integer ) { if ( value instanceof Integer integerValue ) {
return BigInteger.valueOf( ( (Integer) value ) ); return BigInteger.valueOf( integerValue );
} }
if ( value instanceof Long ) { if ( value instanceof Long longValue ) {
return BigInteger.valueOf( ( (Long) value ) ); return BigInteger.valueOf( longValue );
} }
if ( value instanceof Double ) { if ( value instanceof Double doubleValue ) {
return CoercionHelper.toBigInteger( (Double) value ); return CoercionHelper.toBigInteger( doubleValue );
} }
if ( value instanceof Float ) { if ( value instanceof Float floatValue ) {
return CoercionHelper.toBigInteger( (Float) value ); return CoercionHelper.toBigInteger( floatValue );
} }
if ( value instanceof BigDecimal ) { if ( value instanceof BigDecimal bigDecimal ) {
return CoercionHelper.toBigInteger( (BigDecimal) value ); return CoercionHelper.toBigInteger( bigDecimal );
} }
if ( value instanceof String ) { if ( value instanceof String string ) {
return CoercionHelper.coerceWrappingError( return CoercionHelper.coerceWrappingError(
() -> BigInteger.valueOf( Long.parseLong( (String) value ) ) () -> BigInteger.valueOf( Long.parseLong( string ) )
); );
} }

View File

@ -110,9 +110,9 @@ public class BlobJavaType extends AbstractClassJavaType<Blob> {
try { try {
if ( BinaryStream.class.isAssignableFrom( type ) ) { if ( BinaryStream.class.isAssignableFrom( type ) ) {
if (value instanceof BlobImplementer) { if (value instanceof BlobImplementer blobImplementer) {
// if the incoming Blob is a wrapper, just pass along its BinaryStream // if the incoming Blob is a wrapper, just pass along its BinaryStream
return (X) ( (BlobImplementer) value ).getUnderlyingStream(); return (X) blobImplementer.getUnderlyingStream();
} }
else { else {
// otherwise we need to build a BinaryStream... // otherwise we need to build a BinaryStream...
@ -120,9 +120,9 @@ public class BlobJavaType extends AbstractClassJavaType<Blob> {
} }
} }
else if ( byte[].class.isAssignableFrom( type )) { else if ( byte[].class.isAssignableFrom( type )) {
if (value instanceof BlobImplementer) { if (value instanceof BlobImplementer blobImplementer) {
// if the incoming Blob is a wrapper, just grab the bytes from its BinaryStream // if the incoming Blob is a wrapper, just grab the bytes from its BinaryStream
return (X) ( (BlobImplementer) value ).getUnderlyingStream().getBytes(); return (X) blobImplementer.getUnderlyingStream().getBytes();
} }
else { else {
// otherwise extract the bytes from the stream manually // otherwise extract the bytes from the stream manually
@ -141,8 +141,8 @@ public class BlobJavaType extends AbstractClassJavaType<Blob> {
} }
private Blob getOrCreateBlob(Blob value, WrapperOptions options) throws SQLException { private Blob getOrCreateBlob(Blob value, WrapperOptions options) throws SQLException {
if ( value instanceof WrappedBlob ) { if ( value instanceof WrappedBlob wrappedBlob ) {
value = ( (WrappedBlob) value ).getWrappedBlob(); value = wrappedBlob.getWrappedBlob();
} }
if ( options.getDialect().useConnectionToCreateLob() ) { if ( options.getDialect().useConnectionToCreateLob() ) {
if ( value.length() == 0 ) { if ( value.length() == 0 ) {

View File

@ -108,18 +108,18 @@ public class BooleanJavaType extends AbstractClassJavaType<Boolean> implements
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if (value instanceof Boolean) { if (value instanceof Boolean booleanValue) {
return (Boolean) value; return booleanValue;
} }
if (value instanceof Number) { if (value instanceof Number number) {
final int intValue = ( (Number) value ).intValue(); final int intValue = number.intValue();
return intValue != 0; return intValue != 0;
} }
if (value instanceof Character) { if (value instanceof Character character) {
return isTrue( (Character) value ); return isTrue( character );
} }
if (value instanceof String) { if (value instanceof String string) {
return isTrue( (String) value ); return isTrue( string );
} }
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );
} }

View File

@ -64,8 +64,8 @@ public class CharacterJavaType extends AbstractClassJavaType<Character> implemen
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if (value instanceof Character) { if (value instanceof Character character) {
return (Character) value; return character;
} }
if ( value instanceof String ) { if ( value instanceof String ) {
if ( value.equals( "" ) ) { if ( value.equals( "" ) ) {
@ -74,9 +74,8 @@ public class CharacterJavaType extends AbstractClassJavaType<Character> implemen
final String str = (String) value; final String str = (String) value;
return str.charAt( 0 ); return str.charAt( 0 );
} }
if (value instanceof Number) { if (value instanceof Number number) {
final Number nbr = (Number) value; return (char) number.shortValue();
return (char) nbr.shortValue();
} }
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );
} }

View File

@ -13,11 +13,12 @@ import org.hibernate.type.descriptor.WrapperOptions;
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class ClassJavaType extends AbstractClassJavaType<Class> { public class ClassJavaType extends AbstractClassJavaType<Class<?>> {
public static final ClassJavaType INSTANCE = new ClassJavaType(); public static final ClassJavaType INSTANCE = new ClassJavaType();
@SuppressWarnings({"unchecked", "rawtypes"} )
public ClassJavaType() { public ClassJavaType() {
super( Class.class ); super( (Class) Class.class );
} }
@Override @Override
@ -25,11 +26,11 @@ public class ClassJavaType extends AbstractClassJavaType<Class> {
return true; return true;
} }
public String toString(Class value) { public String toString(Class<?> value) {
return value.getName(); return value.getName();
} }
public Class fromString(CharSequence string) { public Class<?> fromString(CharSequence string) {
if ( string == null ) { if ( string == null ) {
return null; return null;
} }
@ -43,7 +44,7 @@ public class ClassJavaType extends AbstractClassJavaType<Class> {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <X> X unwrap(Class value, Class<X> type, WrapperOptions options) { public <X> X unwrap(Class<?> value, Class<X> type, WrapperOptions options) {
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
@ -56,12 +57,12 @@ public class ClassJavaType extends AbstractClassJavaType<Class> {
throw unknownUnwrap( type ); throw unknownUnwrap( type );
} }
public <X> Class wrap(X value, WrapperOptions options) { public <X> Class<?> wrap(X value, WrapperOptions options) {
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if (value instanceof Class) { if (value instanceof Class) {
return (Class) value; return (Class<?>) value;
} }
if (value instanceof CharSequence) { if (value instanceof CharSequence) {
return fromString( (CharSequence) value ); return fromString( (CharSequence) value );

View File

@ -83,9 +83,9 @@ public class ClobJavaType extends AbstractClassJavaType<Clob> {
try { try {
if ( CharacterStream.class.isAssignableFrom( type ) ) { if ( CharacterStream.class.isAssignableFrom( type ) ) {
if (value instanceof ClobImplementer) { if (value instanceof ClobImplementer clobImplementer) {
// if the incoming Clob is a wrapper, just pass along its CharacterStream // if the incoming Clob is a wrapper, just pass along its CharacterStream
return (X) ( (ClobImplementer) value ).getUnderlyingStream(); return (X) clobImplementer.getUnderlyingStream();
} }
else { else {
// otherwise we need to build a CharacterStream... // otherwise we need to build a CharacterStream...
@ -93,9 +93,9 @@ public class ClobJavaType extends AbstractClassJavaType<Clob> {
} }
} }
else if ( String.class.isAssignableFrom( type ) ) { else if ( String.class.isAssignableFrom( type ) ) {
if (value instanceof ClobImplementer) { if (value instanceof ClobImplementer clobImplementer) {
// if the incoming Clob is a wrapper, just grab the bytes from its BinaryStream // if the incoming Clob is a wrapper, just grab the bytes from its BinaryStream
return (X) ( (ClobImplementer) value ).getUnderlyingStream().asString(); return (X) clobImplementer.getUnderlyingStream().asString();
} }
else { else {
// otherwise extract the bytes from the stream manually // otherwise extract the bytes from the stream manually
@ -106,9 +106,9 @@ public class ClobJavaType extends AbstractClassJavaType<Clob> {
return (X) getOrCreateClob( value, options ); return (X) getOrCreateClob( value, options );
} }
else if ( String.class.isAssignableFrom( type ) ) { else if ( String.class.isAssignableFrom( type ) ) {
if (value instanceof ClobImplementer) { if (value instanceof ClobImplementer clobImplementer) {
// if the incoming Clob is a wrapper, just get the underlying String. // if the incoming Clob is a wrapper, just get the underlying String.
return (X) ( (ClobImplementer) value ).getUnderlyingStream().asString(); return (X) clobImplementer.getUnderlyingStream().asString();
} }
else { else {
// otherwise we need to extract the String. // otherwise we need to extract the String.
@ -124,8 +124,8 @@ public class ClobJavaType extends AbstractClassJavaType<Clob> {
} }
private Clob getOrCreateClob(Clob value, WrapperOptions options) throws SQLException { private Clob getOrCreateClob(Clob value, WrapperOptions options) throws SQLException {
if ( value instanceof WrappedClob ) { if ( value instanceof WrappedClob wrappedClob ) {
value = ( (WrappedClob) value ).getWrappedClob(); value = wrappedClob.getWrappedClob();
} }
if ( options.getDialect().useConnectionToCreateLob() ) { if ( options.getDialect().useConnectionToCreateLob() ) {
if ( value.length() == 0 ) { if ( value.length() == 0 ) {

View File

@ -56,11 +56,11 @@ public class CurrencyJavaType extends AbstractClassJavaType<Currency> {
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if ( value instanceof Currency ) { if ( value instanceof Currency currency ) {
return (Currency) value; return currency;
} }
if (value instanceof String) { if (value instanceof String string) {
return Currency.getInstance( (String) value ); return Currency.getInstance( string );
} }
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );
} }

View File

@ -112,12 +112,11 @@ public class DurationJavaType extends AbstractClassJavaType<Duration> {
return null; return null;
} }
if (value instanceof Duration) { if (value instanceof Duration duration) {
return (Duration) value; return duration;
} }
if (value instanceof BigDecimal) { if ( value instanceof BigDecimal decimal ) {
final BigDecimal decimal = (BigDecimal) value;
final BigDecimal[] secondsAndNanos = decimal.divideAndRemainder( BILLION ); final BigDecimal[] secondsAndNanos = decimal.divideAndRemainder( BILLION );
return Duration.ofSeconds( return Duration.ofSeconds(
secondsAndNanos[0].longValueExact(), secondsAndNanos[0].longValueExact(),
@ -129,17 +128,17 @@ public class DurationJavaType extends AbstractClassJavaType<Duration> {
); );
} }
if (value instanceof Double) { if (value instanceof Double doubleValue) {
// PostgreSQL returns a Double for datediff(epoch) // PostgreSQL returns a Double for datediff(epoch)
return Duration.ofNanos( ( (Double) value ).longValue() ); return Duration.ofNanos( doubleValue.longValue() );
} }
if (value instanceof Long) { if (value instanceof Long longValue) {
return Duration.ofNanos( (Long) value ); return Duration.ofNanos( longValue );
} }
if (value instanceof String) { if (value instanceof String string) {
return Duration.parse( (String) value ); return Duration.parse( string );
} }
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );

View File

@ -124,23 +124,23 @@ public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
else if ( value instanceof String ) { else if ( value instanceof String string ) {
return fromName( (String) value ); return fromName( string );
} }
else if ( value instanceof Long ) { else if ( value instanceof Long longValue ) {
return fromLong( (Long) value ); return fromLong( longValue );
} }
else if ( value instanceof Integer ) { else if ( value instanceof Integer integerValue ) {
return fromInteger( (Integer) value ); return fromInteger( integerValue );
} }
else if ( value instanceof Short ) { else if ( value instanceof Short shortValue ) {
return fromShort( (Short) value ); return fromShort( shortValue );
} }
else if ( value instanceof Byte ) { else if ( value instanceof Byte byteValue ) {
return fromByte( (Byte) value ); return fromByte( byteValue );
} }
else if ( value instanceof Number ) { else if ( value instanceof Number number ) {
return fromLong( ((Number) value).longValue() ); return fromLong( number.longValue() );
} }
return (T) value; return (T) value;

View File

@ -146,16 +146,15 @@ public class InstantJavaType extends AbstractTemporalJavaType<Instant>
return null; return null;
} }
if ( value instanceof Instant ) { if ( value instanceof Instant instant ) {
return (Instant) value; return instant;
} }
if ( value instanceof OffsetDateTime ) { if ( value instanceof OffsetDateTime offsetDateTime ) {
return ( (OffsetDateTime) value ).toInstant(); return offsetDateTime.toInstant();
} }
if ( value instanceof Timestamp ) { if ( value instanceof Timestamp timestamp ) {
final Timestamp ts = (Timestamp) value;
/* /*
* This works around two bugs: * This works around two bugs:
* - HHH-13266 (JDK-8061577): around and before 1900, * - HHH-13266 (JDK-8061577): around and before 1900,
@ -166,20 +165,19 @@ public class InstantJavaType extends AbstractTemporalJavaType<Instant>
* (on DST end), so conversion must be done using the number of milliseconds since the epoch. * (on DST end), so conversion must be done using the number of milliseconds since the epoch.
* - around 1905, both methods are equally valid, so we don't really care which one is used. * - around 1905, both methods are equally valid, so we don't really care which one is used.
*/ */
if ( ts.getYear() < 5 ) { // Timestamp year 0 is 1900 if ( timestamp.getYear() < 5 ) { // Timestamp year 0 is 1900
return ts.toLocalDateTime().atZone( ZoneId.systemDefault() ).toInstant(); return timestamp.toLocalDateTime().atZone( ZoneId.systemDefault() ).toInstant();
} }
else { else {
return ts.toInstant(); return timestamp.toInstant();
} }
} }
if ( value instanceof Long ) { if ( value instanceof Long longValue ) {
return Instant.ofEpochMilli( (Long) value ); return Instant.ofEpochMilli( longValue );
} }
if ( value instanceof Calendar ) { if ( value instanceof Calendar calendar ) {
final Calendar calendar = (Calendar) value;
return ZonedDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ).toInstant(); return ZonedDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ).toInstant();
} }

View File

@ -126,12 +126,11 @@ public class LocalDateJavaType extends AbstractTemporalJavaType<LocalDate> {
return null; return null;
} }
if (value instanceof LocalDate) { if (value instanceof LocalDate localDate) {
return (LocalDate) value; return localDate;
} }
if (value instanceof Timestamp) { if (value instanceof Timestamp timestamp) {
final Timestamp ts = (Timestamp) value;
/* /*
* Workaround for HHH-13266 (JDK-8061577). * Workaround for HHH-13266 (JDK-8061577).
* We used to do LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ).toLocalDate(), * We used to do LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ).toLocalDate(),
@ -139,25 +138,24 @@ public class LocalDateJavaType extends AbstractTemporalJavaType<LocalDate> {
* ts.toInstant() assumes the number of milliseconds since the epoch * ts.toInstant() assumes the number of milliseconds since the epoch
* means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900. * means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
*/ */
return ts.toLocalDateTime().toLocalDate(); return timestamp.toLocalDateTime().toLocalDate();
} }
if (value instanceof Long) { if (value instanceof Long longValue) {
final Instant instant = Instant.ofEpochMilli( (Long) value ); final Instant instant = Instant.ofEpochMilli( longValue );
return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() ).toLocalDate(); return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() ).toLocalDate();
} }
if (value instanceof Calendar) { if (value instanceof Calendar calendar) {
final Calendar calendar = (Calendar) value;
return LocalDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ).toLocalDate(); return LocalDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ).toLocalDate();
} }
if (value instanceof Date) { if (value instanceof Date date) {
if (value instanceof java.sql.Date) { if (value instanceof java.sql.Date sqlDate) {
return ((java.sql.Date) value).toLocalDate(); return sqlDate.toLocalDate();
} }
else { else {
return Instant.ofEpochMilli( ((Date) value).getTime() ).atZone( ZoneId.systemDefault() ).toLocalDate(); return Instant.ofEpochMilli( date.getTime() ).atZone( ZoneId.systemDefault() ).toLocalDate();
} }
} }

View File

@ -128,12 +128,11 @@ public class LocalDateTimeJavaType extends AbstractTemporalJavaType<LocalDateTim
return null; return null;
} }
if (value instanceof LocalDateTime) { if (value instanceof LocalDateTime localDateTime) {
return (LocalDateTime) value; return localDateTime;
} }
if (value instanceof Timestamp) { if (value instanceof Timestamp timestamp) {
final Timestamp ts = (Timestamp) value;
/* /*
* Workaround for HHH-13266 (JDK-8061577). * Workaround for HHH-13266 (JDK-8061577).
* We used to do LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ), * We used to do LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ),
@ -141,22 +140,20 @@ public class LocalDateTimeJavaType extends AbstractTemporalJavaType<LocalDateTim
* ts.toInstant() assumes the number of milliseconds since the epoch * ts.toInstant() assumes the number of milliseconds since the epoch
* means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900. * means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
*/ */
return ts.toLocalDateTime(); return timestamp.toLocalDateTime();
} }
if (value instanceof Long) { if (value instanceof Long longValue) {
final Instant instant = Instant.ofEpochMilli( (Long) value ); final Instant instant = Instant.ofEpochMilli( longValue );
return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() ); return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() );
} }
if (value instanceof Calendar) { if (value instanceof Calendar calendar) {
final Calendar calendar = (Calendar) value;
return LocalDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ); return LocalDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );
} }
if (value instanceof Date) { if (value instanceof Date timestamp) {
final Date ts = (Date) value; final Instant instant = timestamp.toInstant();
final Instant instant = ts.toInstant();
return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() ); return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() );
} }

View File

@ -132,12 +132,11 @@ public class LocalTimeJavaType extends AbstractTemporalJavaType<LocalTime> {
return null; return null;
} }
if (value instanceof LocalTime) { if (value instanceof LocalTime localTime) {
return (LocalTime) value; return localTime;
} }
if (value instanceof Time) { if (value instanceof Time time) {
final Time time = (Time) value;
final LocalTime localTime = time.toLocalTime(); final LocalTime localTime = time.toLocalTime();
long millis = time.getTime() % 1000; long millis = time.getTime() % 1000;
if ( millis == 0 ) { if ( millis == 0 ) {
@ -151,24 +150,21 @@ public class LocalTimeJavaType extends AbstractTemporalJavaType<LocalTime> {
return localTime.with( ChronoField.NANO_OF_SECOND, millis * 1_000_000L ); return localTime.with( ChronoField.NANO_OF_SECOND, millis * 1_000_000L );
} }
if (value instanceof Timestamp) { if (value instanceof Timestamp timestamp) {
final Timestamp ts = (Timestamp) value; return LocalDateTime.ofInstant( timestamp.toInstant(), ZoneId.systemDefault() ).toLocalTime();
return LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ).toLocalTime();
} }
if (value instanceof Long) { if (value instanceof Long longValue) {
final Instant instant = Instant.ofEpochMilli( (Long) value ); final Instant instant = Instant.ofEpochMilli( longValue );
return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() ).toLocalTime(); return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() ).toLocalTime();
} }
if (value instanceof Calendar) { if (value instanceof Calendar calendar) {
final Calendar calendar = (Calendar) value;
return LocalDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ).toLocalTime(); return LocalDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ).toLocalTime();
} }
if (value instanceof Date) { if (value instanceof Date timestamp ) {
final Date ts = (Date) value; final Instant instant = Instant.ofEpochMilli( timestamp.getTime() );
final Instant instant = Instant.ofEpochMilli( ts.getTime() );
return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() ).toLocalTime(); return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() ).toLocalTime();
} }

View File

@ -177,22 +177,19 @@ public class OffsetDateTimeJavaType extends AbstractTemporalJavaType<OffsetDateT
return null; return null;
} }
if (value instanceof OffsetDateTime) { if (value instanceof OffsetDateTime offsetDateTime) {
return (OffsetDateTime) value; return offsetDateTime;
} }
if (value instanceof ZonedDateTime) { if (value instanceof ZonedDateTime zonedDateTime) {
ZonedDateTime zonedDateTime = (ZonedDateTime) value;
return OffsetDateTime.of( zonedDateTime.toLocalDateTime(), zonedDateTime.getOffset() ); return OffsetDateTime.of( zonedDateTime.toLocalDateTime(), zonedDateTime.getOffset() );
} }
if (value instanceof Instant) { if (value instanceof Instant instant) {
Instant instant = (Instant) value;
return instant.atOffset( ZoneOffset.UTC ); return instant.atOffset( ZoneOffset.UTC );
} }
if (value instanceof Timestamp) { if (value instanceof Timestamp timestamp) {
final Timestamp ts = (Timestamp) value;
/* /*
* This works around two bugs: * This works around two bugs:
* - HHH-13266 (JDK-8061577): around and before 1900, * - HHH-13266 (JDK-8061577): around and before 1900,
@ -203,25 +200,23 @@ public class OffsetDateTimeJavaType extends AbstractTemporalJavaType<OffsetDateT
* (on DST end), so conversion must be done using the number of milliseconds since the epoch. * (on DST end), so conversion must be done using the number of milliseconds since the epoch.
* - around 1905, both methods are equally valid, so we don't really care which one is used. * - around 1905, both methods are equally valid, so we don't really care which one is used.
*/ */
if ( ts.getYear() < 5 ) { // Timestamp year 0 is 1900 if ( timestamp.getYear() < 5 ) { // Timestamp year 0 is 1900
return ts.toLocalDateTime().atZone( ZoneId.systemDefault() ).toOffsetDateTime(); return timestamp.toLocalDateTime().atZone( ZoneId.systemDefault() ).toOffsetDateTime();
} }
else { else {
return OffsetDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ); return OffsetDateTime.ofInstant( timestamp.toInstant(), ZoneId.systemDefault() );
} }
} }
if (value instanceof Date) { if (value instanceof Date date) {
final Date date = (Date) value;
return OffsetDateTime.ofInstant( date.toInstant(), ZoneId.systemDefault() ); return OffsetDateTime.ofInstant( date.toInstant(), ZoneId.systemDefault() );
} }
if (value instanceof Long) { if (value instanceof Long longValue) {
return OffsetDateTime.ofInstant( Instant.ofEpochMilli( (Long) value ), ZoneId.systemDefault() ); return OffsetDateTime.ofInstant( Instant.ofEpochMilli( longValue ), ZoneId.systemDefault() );
} }
if (value instanceof Calendar) { if (value instanceof Calendar calendar) {
final Calendar calendar = (Calendar) value;
return OffsetDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ); return OffsetDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );
} }

View File

@ -154,16 +154,16 @@ public class OffsetTimeJavaType extends AbstractTemporalJavaType<OffsetTime> {
// for java.time types, we assume that the JDBC timezone, if any, is ignored // for java.time types, we assume that the JDBC timezone, if any, is ignored
// (since PS.setObject() doesn't support passing a timezone) // (since PS.setObject() doesn't support passing a timezone)
if (value instanceof OffsetTime) { if (value instanceof OffsetTime offsetTime) {
return (OffsetTime) value; return offsetTime;
} }
if (value instanceof LocalTime) { if (value instanceof LocalTime localTime) {
return ((LocalTime) value).atOffset( getCurrentSystemOffset() ); return localTime.atOffset( getCurrentSystemOffset() );
} }
if ( value instanceof OffsetDateTime ) { if ( value instanceof OffsetDateTime offsetDateTime) {
return ( (OffsetDateTime) value ).toOffsetTime(); return offsetDateTime.toOffsetTime();
} }
/* /*
@ -184,8 +184,7 @@ public class OffsetTimeJavaType extends AbstractTemporalJavaType<OffsetTime> {
// for legacy types, we assume that the JDBC timezone is passed to JDBC // for legacy types, we assume that the JDBC timezone is passed to JDBC
// (since PS.setTime() and friends do accept a timezone passed as a Calendar) // (since PS.setTime() and friends do accept a timezone passed as a Calendar)
if (value instanceof Time) { if (value instanceof Time time) {
final Time time = (Time) value;
final OffsetTime offsetTime = time.toLocalTime() final OffsetTime offsetTime = time.toLocalTime()
.atOffset( getCurrentJdbcOffset( options) ) .atOffset( getCurrentJdbcOffset( options) )
.withOffsetSameInstant( getCurrentSystemOffset() ); .withOffsetSameInstant( getCurrentSystemOffset() );
@ -201,8 +200,7 @@ public class OffsetTimeJavaType extends AbstractTemporalJavaType<OffsetTime> {
return offsetTime.with( ChronoField.NANO_OF_SECOND, millis * 1_000_000L ); return offsetTime.with( ChronoField.NANO_OF_SECOND, millis * 1_000_000L );
} }
if (value instanceof Timestamp) { if (value instanceof Timestamp timestamp) {
final Timestamp ts = (Timestamp) value;
/* /*
* Workaround for HHH-13266 (JDK-8061577). * Workaround for HHH-13266 (JDK-8061577).
* Ideally we'd want to use OffsetDateTime.ofInstant( ts.toInstant(), ... ), * Ideally we'd want to use OffsetDateTime.ofInstant( ts.toInstant(), ... ),
@ -210,24 +208,21 @@ public class OffsetTimeJavaType extends AbstractTemporalJavaType<OffsetTime> {
* milliseconds since the epoch means the same thing in Timestamp and Instant, * milliseconds since the epoch means the same thing in Timestamp and Instant,
* but it doesn't, in particular before 1900. * but it doesn't, in particular before 1900.
*/ */
return ts.toLocalDateTime().toLocalTime().atOffset( getCurrentJdbcOffset(options) ) return timestamp.toLocalDateTime().toLocalTime().atOffset( getCurrentJdbcOffset(options) )
.withOffsetSameInstant( getCurrentSystemOffset() ); .withOffsetSameInstant( getCurrentSystemOffset() );
} }
if (value instanceof Date) { if (value instanceof Date date) {
final Date date = (Date) value;
return OffsetTime.ofInstant( date.toInstant(), getCurrentSystemOffset() ); return OffsetTime.ofInstant( date.toInstant(), getCurrentSystemOffset() );
} }
// for instants, we assume that the JDBC timezone, if any, is ignored // for instants, we assume that the JDBC timezone, if any, is ignored
if (value instanceof Long) { if (value instanceof Long millis) {
final long millis = (Long) value;
return OffsetTime.ofInstant( Instant.ofEpochMilli(millis), getCurrentSystemOffset() ); return OffsetTime.ofInstant( Instant.ofEpochMilli(millis), getCurrentSystemOffset() );
} }
if (value instanceof Calendar) { if (value instanceof Calendar calendar) {
final Calendar calendar = (Calendar) value;
return OffsetTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ); return OffsetTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );
} }

View File

@ -102,17 +102,17 @@ public class StringJavaType extends AbstractClassJavaType<String> {
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if (value instanceof String) { if (value instanceof String string) {
return (String) value; return string;
} }
if (value instanceof char[]) { if (value instanceof char[] chars) {
return new String( (char[]) value ); return new String( chars );
} }
if (value instanceof Reader) { if (value instanceof Reader reader) {
return DataHelper.extractString( (Reader) value ); return DataHelper.extractString( reader );
} }
if (value instanceof Clob) { if (value instanceof Clob clob) {
return DataHelper.extractString( (Clob) value ); return DataHelper.extractString( clob );
} }
if (value instanceof Integer) { if (value instanceof Integer) {
return value.toString(); return value.toString();

View File

@ -66,11 +66,11 @@ public class UrlJavaType extends AbstractClassJavaType<URL> {
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if (value instanceof URL) { if (value instanceof URL url) {
return (URL) value; return url;
} }
if (value instanceof CharSequence) { if (value instanceof CharSequence charSequence) {
return fromString( (CharSequence) value ); return fromString( charSequence );
} }
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );
} }

View File

@ -75,16 +75,16 @@ public class YearJavaType extends AbstractClassJavaType<Year> {
return null; return null;
} }
if ( value instanceof Year) { if ( value instanceof Year year) {
return (Year) value; return year;
} }
if ( value instanceof Number ) { if ( value instanceof Number number ) {
return Year.of( ( (Number) value ).intValue() ); return Year.of( number.intValue() );
} }
if ( value instanceof String ) { if ( value instanceof String string ) {
return fromString( (String) value ); return fromString( string );
} }
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );

View File

@ -64,11 +64,11 @@ public class ZoneIdJavaType extends AbstractClassJavaType<ZoneId> {
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if ( value instanceof ZoneId ) { if ( value instanceof ZoneId zoneId ) {
return (ZoneId) value; return zoneId;
} }
if ( value instanceof String ) { if ( value instanceof String string ) {
return fromString( (String) value ); return fromString( string );
} }
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );
} }

View File

@ -73,14 +73,14 @@ public class ZoneOffsetJavaType extends AbstractClassJavaType<ZoneOffset> {
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if ( value instanceof ZoneOffset ) { if ( value instanceof ZoneOffset zoneOffset ) {
return (ZoneOffset) value; return zoneOffset;
} }
if ( value instanceof CharSequence ) { if ( value instanceof CharSequence charSequence ) {
return fromString( (CharSequence) value ); return fromString( charSequence );
} }
if ( value instanceof Integer ) { if ( value instanceof Integer integer ) {
return ZoneOffset.ofTotalSeconds( (Integer) value ); return ZoneOffset.ofTotalSeconds( integer );
} }
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );
} }

View File

@ -144,22 +144,19 @@ public class ZonedDateTimeJavaType extends AbstractTemporalJavaType<ZonedDateTim
return null; return null;
} }
if (value instanceof ZonedDateTime) { if (value instanceof ZonedDateTime zonedDateTime) {
return (ZonedDateTime) value; return zonedDateTime;
} }
if (value instanceof OffsetDateTime) { if (value instanceof OffsetDateTime offsetDateTime) {
OffsetDateTime offsetDateTime = (OffsetDateTime) value;
return offsetDateTime.toZonedDateTime(); return offsetDateTime.toZonedDateTime();
} }
if (value instanceof Instant) { if (value instanceof Instant instant) {
Instant instant = (Instant) value;
return instant.atZone( ZoneOffset.UTC ); return instant.atZone( ZoneOffset.UTC );
} }
if (value instanceof Timestamp) { if (value instanceof Timestamp timestamp) {
final Timestamp ts = (Timestamp) value;
/* /*
* This works around two bugs: * This works around two bugs:
* - HHH-13266 (JDK-8061577): around and before 1900, * - HHH-13266 (JDK-8061577): around and before 1900,
@ -170,25 +167,23 @@ public class ZonedDateTimeJavaType extends AbstractTemporalJavaType<ZonedDateTim
* (on DST end), so conversion must be done using the number of milliseconds since the epoch. * (on DST end), so conversion must be done using the number of milliseconds since the epoch.
* - around 1905, both methods are equally valid, so we don't really care which one is used. * - around 1905, both methods are equally valid, so we don't really care which one is used.
*/ */
if ( ts.getYear() < 5 ) { // Timestamp year 0 is 1900 if ( timestamp.getYear() < 5 ) { // Timestamp year 0 is 1900
return ts.toLocalDateTime().atZone( ZoneId.systemDefault() ); return timestamp.toLocalDateTime().atZone( ZoneId.systemDefault() );
} }
else { else {
return ts.toInstant().atZone( ZoneId.systemDefault() ); return timestamp.toInstant().atZone( ZoneId.systemDefault() );
} }
} }
if (value instanceof Date) { if (value instanceof Date date) {
final Date date = (Date) value;
return ZonedDateTime.ofInstant( date.toInstant(), ZoneId.systemDefault() ); return ZonedDateTime.ofInstant( date.toInstant(), ZoneId.systemDefault() );
} }
if (value instanceof Long) { if (value instanceof Long longValue) {
return ZonedDateTime.ofInstant( Instant.ofEpochMilli( (Long) value ), ZoneId.systemDefault() ); return ZonedDateTime.ofInstant( Instant.ofEpochMilli( longValue ), ZoneId.systemDefault() );
} }
if (value instanceof Calendar) { if (value instanceof Calendar calendar) {
final Calendar calendar = (Calendar) value;
return ZonedDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ); return ZonedDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );
} }