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 ) {
return null;
}
if ( value instanceof BigDecimal ) {
return (BigDecimal) value;
if ( value instanceof BigDecimal bigDecimal ) {
return bigDecimal;
}
if ( value instanceof BigInteger ) {
return new BigDecimal( (BigInteger) value );
if ( value instanceof BigInteger bigInteger ) {
return new BigDecimal( bigInteger );
}
if ( value instanceof Number ) {
return BigDecimal.valueOf( ( (Number) value ).doubleValue() );
if ( value instanceof Number number ) {
return BigDecimal.valueOf( number.doubleValue() );
}
if ( value instanceof String ) {
return new BigDecimal( (String) value );
if ( value instanceof String string ) {
return new BigDecimal( string );
}
throw unknownWrap( value.getClass() );
}
@ -125,17 +125,17 @@ public class BigDecimalJavaType extends AbstractClassJavaType<BigDecimal> {
return null;
}
if ( value instanceof BigDecimal ) {
return (BigDecimal) value;
if ( value instanceof BigDecimal bigDecimal ) {
return bigDecimal;
}
if ( value instanceof Number ) {
return BigDecimal.valueOf( ( (Number) value ).doubleValue() );
if ( value instanceof Number number ) {
return BigDecimal.valueOf( number.doubleValue() );
}
if ( value instanceof String ) {
if ( value instanceof String string ) {
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 ) {
return null;
}
if ( value instanceof BigInteger ) {
return (BigInteger) value;
if ( value instanceof BigInteger bigInteger ) {
return bigInteger;
}
if ( value instanceof BigDecimal ) {
return ( (BigDecimal) value ).toBigIntegerExact();
if ( value instanceof BigDecimal bigDecimal ) {
return bigDecimal.toBigIntegerExact();
}
if ( value instanceof Number ) {
return BigInteger.valueOf( ( (Number) value ).longValue() );
if ( value instanceof Number number ) {
return BigInteger.valueOf( number.longValue() );
}
if ( value instanceof String ) {
return new BigInteger( (String) value );
if ( value instanceof String string ) {
return new BigInteger( string );
}
throw unknownWrap( value.getClass() );
}
@ -127,41 +127,41 @@ public class BigIntegerJavaType extends AbstractClassJavaType<BigInteger> {
return null;
}
if ( value instanceof BigInteger ) {
return (BigInteger) value;
if ( value instanceof BigInteger bigInteger ) {
return bigInteger;
}
if ( value instanceof Byte ) {
return BigInteger.valueOf( ( (Byte) value ) );
if ( value instanceof Byte byteValue ) {
return BigInteger.valueOf( byteValue );
}
if ( value instanceof Short ) {
return BigInteger.valueOf( ( (Short) value ) );
if ( value instanceof Short shortValue ) {
return BigInteger.valueOf( shortValue );
}
if ( value instanceof Integer ) {
return BigInteger.valueOf( ( (Integer) value ) );
if ( value instanceof Integer integerValue ) {
return BigInteger.valueOf( integerValue );
}
if ( value instanceof Long ) {
return BigInteger.valueOf( ( (Long) value ) );
if ( value instanceof Long longValue ) {
return BigInteger.valueOf( longValue );
}
if ( value instanceof Double ) {
return CoercionHelper.toBigInteger( (Double) value );
if ( value instanceof Double doubleValue ) {
return CoercionHelper.toBigInteger( doubleValue );
}
if ( value instanceof Float ) {
return CoercionHelper.toBigInteger( (Float) value );
if ( value instanceof Float floatValue ) {
return CoercionHelper.toBigInteger( floatValue );
}
if ( value instanceof BigDecimal ) {
return CoercionHelper.toBigInteger( (BigDecimal) value );
if ( value instanceof BigDecimal bigDecimal ) {
return CoercionHelper.toBigInteger( bigDecimal );
}
if ( value instanceof String ) {
if ( value instanceof String string ) {
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 {
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
return (X) ( (BlobImplementer) value ).getUnderlyingStream();
return (X) blobImplementer.getUnderlyingStream();
}
else {
// otherwise we need to build a BinaryStream...
@ -120,9 +120,9 @@ public class BlobJavaType extends AbstractClassJavaType<Blob> {
}
}
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
return (X) ( (BlobImplementer) value ).getUnderlyingStream().getBytes();
return (X) blobImplementer.getUnderlyingStream().getBytes();
}
else {
// 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 {
if ( value instanceof WrappedBlob ) {
value = ( (WrappedBlob) value ).getWrappedBlob();
if ( value instanceof WrappedBlob wrappedBlob ) {
value = wrappedBlob.getWrappedBlob();
}
if ( options.getDialect().useConnectionToCreateLob() ) {
if ( value.length() == 0 ) {

View File

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

View File

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

View File

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

View File

@ -83,9 +83,9 @@ public class ClobJavaType extends AbstractClassJavaType<Clob> {
try {
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
return (X) ( (ClobImplementer) value ).getUnderlyingStream();
return (X) clobImplementer.getUnderlyingStream();
}
else {
// otherwise we need to build a CharacterStream...
@ -93,9 +93,9 @@ public class ClobJavaType extends AbstractClassJavaType<Clob> {
}
}
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
return (X) ( (ClobImplementer) value ).getUnderlyingStream().asString();
return (X) clobImplementer.getUnderlyingStream().asString();
}
else {
// otherwise extract the bytes from the stream manually
@ -106,9 +106,9 @@ public class ClobJavaType extends AbstractClassJavaType<Clob> {
return (X) getOrCreateClob( value, options );
}
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.
return (X) ( (ClobImplementer) value ).getUnderlyingStream().asString();
return (X) clobImplementer.getUnderlyingStream().asString();
}
else {
// 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 {
if ( value instanceof WrappedClob ) {
value = ( (WrappedClob) value ).getWrappedClob();
if ( value instanceof WrappedClob wrappedClob ) {
value = wrappedClob.getWrappedClob();
}
if ( options.getDialect().useConnectionToCreateLob() ) {
if ( value.length() == 0 ) {

View File

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

View File

@ -112,12 +112,11 @@ public class DurationJavaType extends AbstractClassJavaType<Duration> {
return null;
}
if (value instanceof Duration) {
return (Duration) value;
if (value instanceof Duration duration) {
return duration;
}
if (value instanceof BigDecimal) {
final BigDecimal decimal = (BigDecimal) value;
if ( value instanceof BigDecimal decimal ) {
final BigDecimal[] secondsAndNanos = decimal.divideAndRemainder( BILLION );
return Duration.ofSeconds(
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)
return Duration.ofNanos( ( (Double) value ).longValue() );
return Duration.ofNanos( doubleValue.longValue() );
}
if (value instanceof Long) {
return Duration.ofNanos( (Long) value );
if (value instanceof Long longValue) {
return Duration.ofNanos( longValue );
}
if (value instanceof String) {
return Duration.parse( (String) value );
if (value instanceof String string) {
return Duration.parse( string );
}
throw unknownWrap( value.getClass() );

View File

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

View File

@ -146,16 +146,15 @@ public class InstantJavaType extends AbstractTemporalJavaType<Instant>
return null;
}
if ( value instanceof Instant ) {
return (Instant) value;
if ( value instanceof Instant instant ) {
return instant;
}
if ( value instanceof OffsetDateTime ) {
return ( (OffsetDateTime) value ).toInstant();
if ( value instanceof OffsetDateTime offsetDateTime ) {
return offsetDateTime.toInstant();
}
if ( value instanceof Timestamp ) {
final Timestamp ts = (Timestamp) value;
if ( value instanceof Timestamp timestamp ) {
/*
* This works around two bugs:
* - 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.
* - 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
return ts.toLocalDateTime().atZone( ZoneId.systemDefault() ).toInstant();
if ( timestamp.getYear() < 5 ) { // Timestamp year 0 is 1900
return timestamp.toLocalDateTime().atZone( ZoneId.systemDefault() ).toInstant();
}
else {
return ts.toInstant();
return timestamp.toInstant();
}
}
if ( value instanceof Long ) {
return Instant.ofEpochMilli( (Long) value );
if ( value instanceof Long longValue ) {
return Instant.ofEpochMilli( longValue );
}
if ( value instanceof Calendar ) {
final Calendar calendar = (Calendar) value;
if ( value instanceof Calendar calendar ) {
return ZonedDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ).toInstant();
}

View File

@ -126,12 +126,11 @@ public class LocalDateJavaType extends AbstractTemporalJavaType<LocalDate> {
return null;
}
if (value instanceof LocalDate) {
return (LocalDate) value;
if (value instanceof LocalDate localDate) {
return localDate;
}
if (value instanceof Timestamp) {
final Timestamp ts = (Timestamp) value;
if (value instanceof Timestamp timestamp) {
/*
* Workaround for HHH-13266 (JDK-8061577).
* 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
* 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) {
final Instant instant = Instant.ofEpochMilli( (Long) value );
if (value instanceof Long longValue) {
final Instant instant = Instant.ofEpochMilli( longValue );
return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() ).toLocalDate();
}
if (value instanceof Calendar) {
final Calendar calendar = (Calendar) value;
if (value instanceof Calendar calendar) {
return LocalDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ).toLocalDate();
}
if (value instanceof Date) {
if (value instanceof java.sql.Date) {
return ((java.sql.Date) value).toLocalDate();
if (value instanceof Date date) {
if (value instanceof java.sql.Date sqlDate) {
return sqlDate.toLocalDate();
}
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;
}
if (value instanceof LocalDateTime) {
return (LocalDateTime) value;
if (value instanceof LocalDateTime localDateTime) {
return localDateTime;
}
if (value instanceof Timestamp) {
final Timestamp ts = (Timestamp) value;
if (value instanceof Timestamp timestamp) {
/*
* Workaround for HHH-13266 (JDK-8061577).
* 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
* 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) {
final Instant instant = Instant.ofEpochMilli( (Long) value );
if (value instanceof Long longValue) {
final Instant instant = Instant.ofEpochMilli( longValue );
return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() );
}
if (value instanceof Calendar) {
final Calendar calendar = (Calendar) value;
if (value instanceof Calendar calendar) {
return LocalDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );
}
if (value instanceof Date) {
final Date ts = (Date) value;
final Instant instant = ts.toInstant();
if (value instanceof Date timestamp) {
final Instant instant = timestamp.toInstant();
return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() );
}

View File

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

View File

@ -177,22 +177,19 @@ public class OffsetDateTimeJavaType extends AbstractTemporalJavaType<OffsetDateT
return null;
}
if (value instanceof OffsetDateTime) {
return (OffsetDateTime) value;
if (value instanceof OffsetDateTime offsetDateTime) {
return offsetDateTime;
}
if (value instanceof ZonedDateTime) {
ZonedDateTime zonedDateTime = (ZonedDateTime) value;
if (value instanceof ZonedDateTime zonedDateTime) {
return OffsetDateTime.of( zonedDateTime.toLocalDateTime(), zonedDateTime.getOffset() );
}
if (value instanceof Instant) {
Instant instant = (Instant) value;
if (value instanceof Instant instant) {
return instant.atOffset( ZoneOffset.UTC );
}
if (value instanceof Timestamp) {
final Timestamp ts = (Timestamp) value;
if (value instanceof Timestamp timestamp) {
/*
* This works around two bugs:
* - 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.
* - 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
return ts.toLocalDateTime().atZone( ZoneId.systemDefault() ).toOffsetDateTime();
if ( timestamp.getYear() < 5 ) { // Timestamp year 0 is 1900
return timestamp.toLocalDateTime().atZone( ZoneId.systemDefault() ).toOffsetDateTime();
}
else {
return OffsetDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() );
return OffsetDateTime.ofInstant( timestamp.toInstant(), ZoneId.systemDefault() );
}
}
if (value instanceof Date) {
final Date date = (Date) value;
if (value instanceof Date date) {
return OffsetDateTime.ofInstant( date.toInstant(), ZoneId.systemDefault() );
}
if (value instanceof Long) {
return OffsetDateTime.ofInstant( Instant.ofEpochMilli( (Long) value ), ZoneId.systemDefault() );
if (value instanceof Long longValue) {
return OffsetDateTime.ofInstant( Instant.ofEpochMilli( longValue ), ZoneId.systemDefault() );
}
if (value instanceof Calendar) {
final Calendar calendar = (Calendar) value;
if (value instanceof Calendar calendar) {
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
// (since PS.setObject() doesn't support passing a timezone)
if (value instanceof OffsetTime) {
return (OffsetTime) value;
if (value instanceof OffsetTime offsetTime) {
return offsetTime;
}
if (value instanceof LocalTime) {
return ((LocalTime) value).atOffset( getCurrentSystemOffset() );
if (value instanceof LocalTime localTime) {
return localTime.atOffset( getCurrentSystemOffset() );
}
if ( value instanceof OffsetDateTime ) {
return ( (OffsetDateTime) value ).toOffsetTime();
if ( value instanceof OffsetDateTime offsetDateTime) {
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
// (since PS.setTime() and friends do accept a timezone passed as a Calendar)
if (value instanceof Time) {
final Time time = (Time) value;
if (value instanceof Time time) {
final OffsetTime offsetTime = time.toLocalTime()
.atOffset( getCurrentJdbcOffset( options) )
.withOffsetSameInstant( getCurrentSystemOffset() );
@ -201,8 +200,7 @@ public class OffsetTimeJavaType extends AbstractTemporalJavaType<OffsetTime> {
return offsetTime.with( ChronoField.NANO_OF_SECOND, millis * 1_000_000L );
}
if (value instanceof Timestamp) {
final Timestamp ts = (Timestamp) value;
if (value instanceof Timestamp timestamp) {
/*
* Workaround for HHH-13266 (JDK-8061577).
* 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,
* but it doesn't, in particular before 1900.
*/
return ts.toLocalDateTime().toLocalTime().atOffset( getCurrentJdbcOffset(options) )
return timestamp.toLocalDateTime().toLocalTime().atOffset( getCurrentJdbcOffset(options) )
.withOffsetSameInstant( getCurrentSystemOffset() );
}
if (value instanceof Date) {
final Date date = (Date) value;
if (value instanceof Date date) {
return OffsetTime.ofInstant( date.toInstant(), getCurrentSystemOffset() );
}
// for instants, we assume that the JDBC timezone, if any, is ignored
if (value instanceof Long) {
final long millis = (Long) value;
if (value instanceof Long millis) {
return OffsetTime.ofInstant( Instant.ofEpochMilli(millis), getCurrentSystemOffset() );
}
if (value instanceof Calendar) {
final Calendar calendar = (Calendar) value;
if (value instanceof Calendar calendar) {
return OffsetTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -144,22 +144,19 @@ public class ZonedDateTimeJavaType extends AbstractTemporalJavaType<ZonedDateTim
return null;
}
if (value instanceof ZonedDateTime) {
return (ZonedDateTime) value;
if (value instanceof ZonedDateTime zonedDateTime) {
return zonedDateTime;
}
if (value instanceof OffsetDateTime) {
OffsetDateTime offsetDateTime = (OffsetDateTime) value;
if (value instanceof OffsetDateTime offsetDateTime) {
return offsetDateTime.toZonedDateTime();
}
if (value instanceof Instant) {
Instant instant = (Instant) value;
if (value instanceof Instant instant) {
return instant.atZone( ZoneOffset.UTC );
}
if (value instanceof Timestamp) {
final Timestamp ts = (Timestamp) value;
if (value instanceof Timestamp timestamp) {
/*
* This works around two bugs:
* - 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.
* - 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
return ts.toLocalDateTime().atZone( ZoneId.systemDefault() );
if ( timestamp.getYear() < 5 ) { // Timestamp year 0 is 1900
return timestamp.toLocalDateTime().atZone( ZoneId.systemDefault() );
}
else {
return ts.toInstant().atZone( ZoneId.systemDefault() );
return timestamp.toInstant().atZone( ZoneId.systemDefault() );
}
}
if (value instanceof Date) {
final Date date = (Date) value;
if (value instanceof Date date) {
return ZonedDateTime.ofInstant( date.toInstant(), ZoneId.systemDefault() );
}
if (value instanceof Long) {
return ZonedDateTime.ofInstant( Instant.ofEpochMilli( (Long) value ), ZoneId.systemDefault() );
if (value instanceof Long longValue) {
return ZonedDateTime.ofInstant( Instant.ofEpochMilli( longValue ), ZoneId.systemDefault() );
}
if (value instanceof Calendar) {
final Calendar calendar = (Calendar) value;
if (value instanceof Calendar calendar) {
return ZonedDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );
}