From 26988dd536d90b92510db8f0da0e6e94008b98c5 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Wed, 30 Oct 2024 10:28:00 +0100 Subject: [PATCH] start using flow typing in the JavaTypes - but definitely not done with this squash Signed-off-by: Gavin King --- .../descriptor/java/BigDecimalJavaType.java | 28 +++++----- .../descriptor/java/BigIntegerJavaType.java | 52 +++++++++---------- .../type/descriptor/java/BlobJavaType.java | 12 ++--- .../type/descriptor/java/BooleanJavaType.java | 16 +++--- .../descriptor/java/CharacterJavaType.java | 9 ++-- .../type/descriptor/java/ClassJavaType.java | 15 +++--- .../type/descriptor/java/ClobJavaType.java | 16 +++--- .../descriptor/java/CurrencyJavaType.java | 8 +-- .../descriptor/java/DurationJavaType.java | 19 ++++--- .../type/descriptor/java/EnumJavaType.java | 24 ++++----- .../type/descriptor/java/InstantJavaType.java | 24 ++++----- .../descriptor/java/LocalDateJavaType.java | 24 ++++----- .../java/LocalDateTimeJavaType.java | 21 ++++---- .../descriptor/java/LocalTimeJavaType.java | 24 ++++----- .../java/OffsetDateTimeJavaType.java | 29 +++++------ .../descriptor/java/OffsetTimeJavaType.java | 29 +++++------ .../type/descriptor/java/StringJavaType.java | 16 +++--- .../type/descriptor/java/UrlJavaType.java | 8 +-- .../type/descriptor/java/YearJavaType.java | 12 ++--- .../type/descriptor/java/ZoneIdJavaType.java | 8 +-- .../descriptor/java/ZoneOffsetJavaType.java | 12 ++--- .../java/ZonedDateTimeJavaType.java | 29 +++++------ 22 files changed, 204 insertions(+), 231 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BigDecimalJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BigDecimalJavaType.java index be0e5a11bc..2cd9b6fc91 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BigDecimalJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BigDecimalJavaType.java @@ -82,17 +82,17 @@ public class BigDecimalJavaType extends AbstractClassJavaType { 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 { 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 ) ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BigIntegerJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BigIntegerJavaType.java index 0354ceed18..2441f69f7a 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BigIntegerJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BigIntegerJavaType.java @@ -80,17 +80,17 @@ public class BigIntegerJavaType extends AbstractClassJavaType { 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 { 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 ) ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BlobJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BlobJavaType.java index c5b3717e04..db45a657d1 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BlobJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BlobJavaType.java @@ -110,9 +110,9 @@ public class BlobJavaType extends AbstractClassJavaType { 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 { } } 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 { } 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 ) { diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanJavaType.java index ebfa121219..770903bd95 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanJavaType.java @@ -108,18 +108,18 @@ public class BooleanJavaType extends AbstractClassJavaType 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() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java index 331ec4947c..d91e7de4b1 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java @@ -64,8 +64,8 @@ public class CharacterJavaType extends AbstractClassJavaType 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 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() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClassJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClassJavaType.java index 408505aa43..9cfa5d5144 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClassJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClassJavaType.java @@ -13,11 +13,12 @@ import org.hibernate.type.descriptor.WrapperOptions; * * @author Steve Ebersole */ -public class ClassJavaType extends AbstractClassJavaType { +public class ClassJavaType extends AbstractClassJavaType> { 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 { 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 { } @SuppressWarnings("unchecked") - public X unwrap(Class value, Class type, WrapperOptions options) { + public X unwrap(Class value, Class type, WrapperOptions options) { if ( value == null ) { return null; } @@ -56,12 +57,12 @@ public class ClassJavaType extends AbstractClassJavaType { throw unknownUnwrap( type ); } - public Class wrap(X value, WrapperOptions options) { + public 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 ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java index f9d2609592..b4b8687403 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java @@ -83,9 +83,9 @@ public class ClobJavaType extends AbstractClassJavaType { 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 { } } 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 { 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 { } 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 ) { diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CurrencyJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CurrencyJavaType.java index 82b6e9aee7..025e043338 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CurrencyJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CurrencyJavaType.java @@ -56,11 +56,11 @@ public class CurrencyJavaType extends AbstractClassJavaType { 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() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DurationJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DurationJavaType.java index 41e0c7aa99..c43ecec681 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DurationJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DurationJavaType.java @@ -112,12 +112,11 @@ public class DurationJavaType extends AbstractClassJavaType { 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 { ); } - 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() ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/EnumJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/EnumJavaType.java index 22ef049eb9..c202fa816f 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/EnumJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/EnumJavaType.java @@ -124,23 +124,23 @@ public class EnumJavaType> extends AbstractClassJavaType { 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; diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaType.java index bc24254179..4d5e80b0bb 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaType.java @@ -146,16 +146,15 @@ public class InstantJavaType extends AbstractTemporalJavaType 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 * (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(); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateJavaType.java index 7b36a45177..34765bfdc2 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateJavaType.java @@ -126,12 +126,11 @@ public class LocalDateJavaType extends AbstractTemporalJavaType { 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 { * 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(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateTimeJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateTimeJavaType.java index 3ca0cf3aaf..a1e2bb1a3f 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateTimeJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateTimeJavaType.java @@ -128,12 +128,11 @@ public class LocalDateTimeJavaType extends AbstractTemporalJavaType { 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 { 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(); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/OffsetDateTimeJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/OffsetDateTimeJavaType.java index 0ca2d75b8e..bfb5748d2a 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/OffsetDateTimeJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/OffsetDateTimeJavaType.java @@ -177,22 +177,19 @@ public class OffsetDateTimeJavaType extends AbstractTemporalJavaType { // 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 { // 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 { 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 { * 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() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/StringJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/StringJavaType.java index 9cc631b623..68e3e21e7e 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/StringJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/StringJavaType.java @@ -102,17 +102,17 @@ public class StringJavaType extends AbstractClassJavaType { 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(); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/UrlJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/UrlJavaType.java index 6173b1895a..51212067ff 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/UrlJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/UrlJavaType.java @@ -66,11 +66,11 @@ public class UrlJavaType extends AbstractClassJavaType { 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() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/YearJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/YearJavaType.java index 43950ee332..242890caf1 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/YearJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/YearJavaType.java @@ -75,16 +75,16 @@ public class YearJavaType extends AbstractClassJavaType { 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() ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZoneIdJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZoneIdJavaType.java index 5e9296a406..5b106f6e15 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZoneIdJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZoneIdJavaType.java @@ -64,11 +64,11 @@ public class ZoneIdJavaType extends AbstractClassJavaType { 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() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZoneOffsetJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZoneOffsetJavaType.java index 058aa6f20d..04bbbf9e78 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZoneOffsetJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZoneOffsetJavaType.java @@ -73,14 +73,14 @@ public class ZoneOffsetJavaType extends AbstractClassJavaType { 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() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZonedDateTimeJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZonedDateTimeJavaType.java index de3b732c2c..35bae1aeff 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZonedDateTimeJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ZonedDateTimeJavaType.java @@ -144,22 +144,19 @@ public class ZonedDateTimeJavaType extends AbstractTemporalJavaType