diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcDateJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcDateJavaType.java index a10b208106..f49b7def0d 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcDateJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcDateJavaType.java @@ -19,7 +19,6 @@ import java.util.Date; import java.util.GregorianCalendar; import org.hibernate.HibernateException; -import org.hibernate.internal.util.CharSequenceHelper; import org.hibernate.sql.ast.spi.SqlAppender; import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.jdbc.JdbcType; @@ -28,6 +27,8 @@ import org.hibernate.type.spi.TypeConfiguration; import jakarta.persistence.TemporalType; +import static org.hibernate.internal.util.CharSequenceHelper.subSequence; + /** * Descriptor for {@link java.sql.Date} handling. * @@ -39,15 +40,13 @@ import jakarta.persistence.TemporalType; public class JdbcDateJavaType extends AbstractTemporalJavaType { public static final JdbcDateJavaType INSTANCE = new JdbcDateJavaType(); - public static final String DATE_FORMAT = "dd MMMM yyyy"; - /** * Intended for use in reading HQL literals and writing SQL literals * - * @see #DATE_FORMAT + * @see DateTimeFormatter#ISO_LOCAL_DATE */ - @SuppressWarnings("unused") public static final DateTimeFormatter LITERAL_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE; + private static final DateTimeFormatter ENCODED_FORMATTER = new DateTimeFormatterBuilder() .append( DateTimeFormatter.ISO_DATE ) .optionalStart() @@ -68,7 +67,7 @@ public class JdbcDateJavaType extends AbstractTemporalJavaType { public boolean isInstance(Object value) { // this check holds true for java.sql.Date as well return value instanceof Date - && !( value instanceof java.sql.Time ); + && !( value instanceof java.sql.Time ); } @Override @@ -91,8 +90,8 @@ public class JdbcDateJavaType extends AbstractTemporalJavaType { calendar2.setTime( another ); return calendar1.get( Calendar.MONTH ) == calendar2.get( Calendar.MONTH ) - && calendar1.get( Calendar.DAY_OF_MONTH ) == calendar2.get( Calendar.DAY_OF_MONTH ) - && calendar1.get( Calendar.YEAR ) == calendar2.get( Calendar.YEAR ); + && calendar1.get( Calendar.DAY_OF_MONTH ) == calendar2.get( Calendar.DAY_OF_MONTH ) + && calendar1.get( Calendar.YEAR ) == calendar2.get( Calendar.YEAR ); } @Override @@ -156,16 +155,15 @@ public class JdbcDateJavaType extends AbstractTemporalJavaType { } private LocalDate unwrapLocalDate(Date value) { - return value instanceof java.sql.Date - ? ( (java.sql.Date) value ).toLocalDate() + return value instanceof java.sql.Date date + ? date.toLocalDate() : new java.sql.Date( unwrapDateEpoch( value ) ).toLocalDate(); } private java.sql.Date unwrapSqlDate(Date value) { - if ( value instanceof java.sql.Date ) { - final java.sql.Date sqlDate = (java.sql.Date) value; - final long dateEpoch = toDateEpoch( sqlDate.getTime() ); - return dateEpoch == sqlDate.getTime() ? sqlDate : new java.sql.Date( dateEpoch ); + if ( value instanceof java.sql.Date date ) { + final long dateEpoch = toDateEpoch( date.getTime() ); + return dateEpoch == date.getTime() ? date : new java.sql.Date( dateEpoch ); } return new java.sql.Date( unwrapDateEpoch( value ) ); @@ -238,13 +236,7 @@ public class JdbcDateJavaType extends AbstractTemporalJavaType { @Override public Date fromEncodedString(CharSequence charSequence, int start, int end) { try { - final TemporalAccessor accessor = ENCODED_FORMATTER.parse( - CharSequenceHelper.subSequence( - charSequence, - start, - end - ) - ); + final TemporalAccessor accessor = ENCODED_FORMATTER.parse( subSequence( charSequence, start, end ) ); return java.sql.Date.valueOf( accessor.query( LocalDate::from ) ); } catch ( DateTimeParseException pe) { diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimeJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimeJavaType.java index bf9c4b9fa1..73beca06ab 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimeJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimeJavaType.java @@ -21,7 +21,6 @@ import java.util.GregorianCalendar; import org.hibernate.HibernateException; import org.hibernate.dialect.Dialect; -import org.hibernate.internal.util.CharSequenceHelper; import org.hibernate.sql.ast.spi.SqlAppender; import org.hibernate.type.descriptor.DateTimeUtils; import org.hibernate.type.descriptor.WrapperOptions; @@ -31,6 +30,8 @@ import org.hibernate.type.spi.TypeConfiguration; import jakarta.persistence.TemporalType; +import static org.hibernate.internal.util.CharSequenceHelper.subSequence; + /** * Descriptor for {@link Time} handling. * @@ -42,19 +43,8 @@ import jakarta.persistence.TemporalType; public class JdbcTimeJavaType extends AbstractTemporalJavaType { public static final JdbcTimeJavaType INSTANCE = new JdbcTimeJavaType(); - public static final String TIME_FORMAT = "HH:mm:ss.SSS"; - public static final DateTimeFormatter LITERAL_FORMATTER = DateTimeFormatter.ISO_LOCAL_TIME; - /** - * Alias for {@link DateTimeFormatter#ISO_LOCAL_TIME}. - * - * Intended for use with logging - * - * @see #LITERAL_FORMATTER - */ - @SuppressWarnings("unused") - public static final DateTimeFormatter LOGGABLE_FORMATTER = DateTimeFormatter.ISO_LOCAL_TIME; private static final DateTimeFormatter ENCODED_FORMATTER = new DateTimeFormatterBuilder() .optionalStart() .append( DateTimeFormatter.ISO_DATE ) @@ -63,6 +53,7 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType { .append( DateTimeFormatter.ISO_LOCAL_TIME ) .toFormatter(); + public JdbcTimeJavaType() { super( Time.class, TimeMutabilityPlan.INSTANCE ); } @@ -76,7 +67,7 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType { public boolean isInstance(Object value) { // this check holds true for java.sql.Time as well return value instanceof Date - && !( value instanceof java.sql.Date ); + && !( value instanceof java.sql.Date ); } @Override @@ -111,9 +102,9 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType { calendar2.setTime( another ); return calendar1.get( Calendar.HOUR_OF_DAY ) == calendar2.get( Calendar.HOUR_OF_DAY ) - && calendar1.get( Calendar.MINUTE ) == calendar2.get( Calendar.MINUTE ) - && calendar1.get( Calendar.SECOND ) == calendar2.get( Calendar.SECOND ) - && calendar1.get( Calendar.MILLISECOND ) == calendar2.get( Calendar.MILLISECOND ); + && calendar1.get( Calendar.MINUTE ) == calendar2.get( Calendar.MINUTE ) + && calendar1.get( Calendar.SECOND ) == calendar2.get( Calendar.SECOND ) + && calendar1.get( Calendar.MILLISECOND ) == calendar2.get( Calendar.MILLISECOND ); } @Override @@ -217,8 +208,8 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType { @Override public String toString(Date value) { - if ( value instanceof java.sql.Time ) { - return LITERAL_FORMATTER.format( ( (java.sql.Time) value ).toLocalTime() ); + if ( value instanceof java.sql.Time time ) { + return LITERAL_FORMATTER.format( time.toLocalTime() ); } else { return LITERAL_FORMATTER.format( LocalTime.ofInstant( value.toInstant(), ZoneOffset.systemDefault() ) ); @@ -242,13 +233,7 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType { @Override public Date fromEncodedString(CharSequence charSequence, int start, int end) { try { - final TemporalAccessor accessor = ENCODED_FORMATTER.parse( - CharSequenceHelper.subSequence( - charSequence, - start, - end - ) - ); + final TemporalAccessor accessor = ENCODED_FORMATTER.parse( subSequence( charSequence, start, end ) ); return java.sql.Time.valueOf( accessor.query( LocalTime::from ) ); } catch ( DateTimeParseException pe) { @@ -258,8 +243,8 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType { @Override public void appendEncodedString(SqlAppender sb, Date value) { - if ( value instanceof java.sql.Time ) { - LITERAL_FORMATTER.formatTo( ( (java.sql.Time) value ).toLocalTime(), sb ); + if ( value instanceof java.sql.Time time ) { + LITERAL_FORMATTER.formatTo( time.toLocalTime(), sb ); } else { LITERAL_FORMATTER.formatTo( LocalTime.ofInstant( value.toInstant(), ZoneOffset.systemDefault() ), sb ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimestampJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimestampJavaType.java index fbee5d86bd..7a7d3a5ce7 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimestampJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimestampJavaType.java @@ -25,7 +25,6 @@ import jakarta.persistence.TemporalType; import org.hibernate.HibernateException; import org.hibernate.dialect.Dialect; import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.internal.util.CharSequenceHelper; import org.hibernate.sql.ast.spi.SqlAppender; import org.hibernate.type.descriptor.DateTimeUtils; import org.hibernate.type.descriptor.WrapperOptions; @@ -33,6 +32,8 @@ import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; import org.hibernate.type.spi.TypeConfiguration; +import static org.hibernate.internal.util.CharSequenceHelper.subSequence; + /** * Descriptor for {@link Timestamp} handling. * @@ -51,7 +52,6 @@ public class JdbcTimestampJavaType extends AbstractTemporalJavaType implem * * @see #TIMESTAMP_FORMAT */ - @SuppressWarnings("unused") public static final DateTimeFormatter LITERAL_FORMATTER = DateTimeFormatter.ofPattern( TIMESTAMP_FORMAT ) .withZone( ZoneId.from( ZoneOffset.UTC ) ); @@ -231,13 +231,7 @@ public class JdbcTimestampJavaType extends AbstractTemporalJavaType implem @Override public Date fromEncodedString(CharSequence charSequence, int start, int end) { try { - final TemporalAccessor accessor = DateTimeUtils.DATE_TIME.parse( - CharSequenceHelper.subSequence( - charSequence, - start, - end - ) - ); + final TemporalAccessor accessor = DateTimeUtils.DATE_TIME.parse( subSequence( charSequence, start, end ) ); final Timestamp timestamp; if ( accessor.isSupported( ChronoField.INSTANT_SECONDS ) ) { timestamp = new Timestamp( accessor.getLong( ChronoField.INSTANT_SECONDS ) * 1000L ); @@ -297,11 +291,10 @@ public class JdbcTimestampJavaType extends AbstractTemporalJavaType implem public static final TimestampMutabilityPlan INSTANCE = new TimestampMutabilityPlan(); @Override public Date deepCopyNotNull(Date value) { - if ( value instanceof Timestamp ) { + if ( value instanceof Timestamp timestamp ) { // make sure to get the nanos - final Timestamp orig = (Timestamp) value; - final Timestamp copy = new Timestamp( orig.getTime() ); - copy.setNanos( orig.getNanos() ); + final Timestamp copy = new Timestamp( timestamp.getTime() ); + copy.setNanos( timestamp.getNanos() ); return copy; } else {