Skip to content

Commit ee2fa41

Browse files
committed
minor aesthetic cleanups to the date/time JavaTypes
1 parent e991a42 commit ee2fa41

File tree

9 files changed

+181
-213
lines changed

9 files changed

+181
-213
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcDateJavaType.java

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
import java.sql.Types;
88
import java.time.LocalDate;
9-
import java.time.LocalTime;
109
import java.time.ZoneOffset;
1110
import java.time.format.DateTimeFormatter;
1211
import java.time.format.DateTimeFormatterBuilder;
1312
import java.time.format.DateTimeParseException;
13+
import java.time.temporal.TemporalAccessor;
1414
import java.util.Calendar;
1515
import java.util.Date;
1616
import java.util.GregorianCalendar;
@@ -114,7 +114,7 @@ public Date coerce(Object value, CoercionContext coercionContext) {
114114
return wrap( value, null );
115115
}
116116

117-
@SuppressWarnings("unchecked")
117+
@SuppressWarnings({"unchecked", "rawtypes"})
118118
@Override
119119
public Object unwrap(Date value, Class type, WrapperOptions options) {
120120
if ( value == null ) {
@@ -169,16 +169,17 @@ private java.sql.Date unwrapSqlDate(Date value) {
169169
final long dateEpoch = toDateEpoch( date.getTime() );
170170
return dateEpoch == date.getTime() ? date : new java.sql.Date( dateEpoch );
171171
}
172-
return new java.sql.Date( unwrapDateEpoch( value ) );
173-
172+
else {
173+
return new java.sql.Date( unwrapDateEpoch( value ) );
174+
}
174175
}
175176

176177
private static long unwrapDateEpoch(Date value) {
177178
return toDateEpoch( value.getTime() );
178179
}
179180

180181
private static long toDateEpoch(long value) {
181-
Calendar calendar = Calendar.getInstance();
182+
final var calendar = Calendar.getInstance();
182183
calendar.setTimeInMillis( value );
183184
calendar.set(Calendar.HOUR_OF_DAY, 0);
184185
calendar.clear(Calendar.MINUTE);
@@ -216,14 +217,15 @@ public Date wrap(Object value, WrapperOptions options) {
216217
throw unknownWrap( value.getClass() );
217218
}
218219

220+
private static TemporalAccessor fromDate(Date value) {
221+
return value instanceof java.sql.Date date
222+
? date.toLocalDate()
223+
: LocalDate.ofInstant( value.toInstant(), ZoneOffset.systemDefault() );
224+
}
225+
219226
@Override
220227
public String toString(Date value) {
221-
if ( value instanceof java.sql.Date ) {
222-
return LITERAL_FORMATTER.format( ( (java.sql.Date) value ).toLocalDate() );
223-
}
224-
else {
225-
return LITERAL_FORMATTER.format( LocalDate.ofInstant( value.toInstant(), ZoneOffset.systemDefault() ) );
226-
}
228+
return LITERAL_FORMATTER.format( fromDate( value ) );
227229
}
228230

229231
@Override
@@ -250,12 +252,7 @@ public Date fromEncodedString(CharSequence charSequence, int start, int end) {
250252

251253
@Override
252254
public void appendEncodedString(SqlAppender sb, Date value) {
253-
if ( value instanceof java.sql.Date ) {
254-
LITERAL_FORMATTER.formatTo( ( (java.sql.Date) value ).toLocalDate(), sb );
255-
}
256-
else {
257-
LITERAL_FORMATTER.formatTo( LocalTime.ofInstant( value.toInstant(), ZoneOffset.systemDefault() ), sb );
258-
}
255+
LITERAL_FORMATTER.formatTo( fromDate( value ), sb );
259256
}
260257

261258
@Override
@@ -274,12 +271,9 @@ public static class DateMutabilityPlan extends MutableMutabilityPlan<Date> {
274271

275272
@Override
276273
public Date deepCopyNotNull(Date value) {
277-
if ( value instanceof java.sql.Date ) {
278-
return value;
279-
}
280-
else {
281-
return new java.sql.Date( value.getTime() );
282-
}
274+
return value instanceof java.sql.Date
275+
? value
276+
: new java.sql.Date( value.getTime() );
283277
}
284278
}
285279
}

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimeJavaType.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,18 @@ public Date coerce(Object value, CoercionContext coercionContext) {
116116
return wrap( value, null );
117117
}
118118

119-
@SuppressWarnings("unchecked")
119+
@SuppressWarnings({"unchecked", "rawtypes"})
120120
@Override
121121
public Object unwrap(Date value, Class type, WrapperOptions options) {
122122
if ( value == null ) {
123123
return null;
124124
}
125125

126126
if ( LocalTime.class.isAssignableFrom( type ) ) {
127-
final Time time = value instanceof java.sql.Time
128-
? (java.sql.Time) value
129-
: new java.sql.Time( value.getTime() % 86_400_000 );
127+
final var time =
128+
value instanceof java.sql.Time
129+
? (java.sql.Time) value
130+
: new java.sql.Time( value.getTime() % 86_400_000 );
130131
final var localTime = time.toLocalTime();
131132
long millis = time.getTime() % 1000;
132133
if ( millis == 0 ) {
@@ -209,14 +210,15 @@ public Date wrap(Object value, WrapperOptions options) {
209210
throw unknownWrap( value.getClass() );
210211
}
211212

213+
private static LocalTime fromDate(Date value) {
214+
return value instanceof Time time
215+
? time.toLocalTime()
216+
: LocalTime.ofInstant( value.toInstant(), ZoneOffset.systemDefault() );
217+
}
218+
212219
@Override
213220
public String toString(Date value) {
214-
if ( value instanceof java.sql.Time time ) {
215-
return LITERAL_FORMATTER.format( time.toLocalTime() );
216-
}
217-
else {
218-
return LITERAL_FORMATTER.format( LocalTime.ofInstant( value.toInstant(), ZoneOffset.systemDefault() ) );
219-
}
221+
return LITERAL_FORMATTER.format( fromDate( value ) );
220222
}
221223

222224
@Override
@@ -246,12 +248,7 @@ public Date fromEncodedString(CharSequence charSequence, int start, int end) {
246248

247249
@Override
248250
public void appendEncodedString(SqlAppender sb, Date value) {
249-
if ( value instanceof java.sql.Time time ) {
250-
LITERAL_FORMATTER.formatTo( time.toLocalTime(), sb );
251-
}
252-
else {
253-
LITERAL_FORMATTER.formatTo( LocalTime.ofInstant( value.toInstant(), ZoneOffset.systemDefault() ), sb );
254-
}
251+
LITERAL_FORMATTER.formatTo( fromDate( value ), sb );
255252
}
256253

257254
@Override

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimestampJavaType.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import java.sql.Timestamp;
88
import java.sql.Types;
9-
import java.time.Instant;
109
import java.time.LocalDateTime;
1110
import java.time.ZoneId;
1211
import java.time.ZoneOffset;
@@ -120,7 +119,7 @@ public Date coerce(Object value, CoercionContext coercionContext) {
120119
return wrap( value, null );
121120
}
122121

123-
@SuppressWarnings({ "unchecked", "rawtypes" })
122+
@SuppressWarnings({"unchecked", "rawtypes"})
124123
@Override
125124
public Object unwrap(Date value, Class type, WrapperOptions options) {
126125
if ( value == null ) {
@@ -138,7 +137,7 @@ public Object unwrap(Date value, Class type, WrapperOptions options) {
138137
}
139138

140139
if ( LocalDateTime.class.isAssignableFrom( type ) ) {
141-
final Instant instant = value.toInstant();
140+
final var instant = value.toInstant();
142141
return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() );
143142
}
144143

@@ -230,15 +229,14 @@ public void appendEncodedString(SqlAppender sb, Date value) {
230229
public Date fromEncodedString(CharSequence charSequence, int start, int end) {
231230
try {
232231
final var temporalAccessor = ENCODED_FORMATTER.parse( subSequence( charSequence, start, end ) );
233-
final Timestamp timestamp;
234232
if ( temporalAccessor.isSupported( ChronoField.INSTANT_SECONDS ) ) {
235-
timestamp = new Timestamp( temporalAccessor.getLong( ChronoField.INSTANT_SECONDS ) * 1000L );
233+
final var timestamp = new Timestamp( temporalAccessor.getLong( ChronoField.INSTANT_SECONDS ) * 1000L );
236234
timestamp.setNanos( temporalAccessor.get( ChronoField.NANO_OF_SECOND ) );
235+
return timestamp;
237236
}
238237
else {
239-
timestamp = Timestamp.valueOf( LocalDateTime.from( temporalAccessor ) );
238+
return Timestamp.valueOf( LocalDateTime.from( temporalAccessor ) );
240239
}
241-
return timestamp;
242240
}
243241
catch ( DateTimeParseException pe) {
244242
throw new HibernateException( "could not parse timestamp string " + subSequence( charSequence, start, end ), pe );

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateJavaType.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,11 @@ public <X> X unwrap(LocalDate value, Class<X> type, WrapperOptions options) {
9494
final LocalDateTime localDateTime = value.atStartOfDay();
9595

9696
if ( Timestamp.class.isAssignableFrom( type ) ) {
97-
/*
98-
* Workaround for HHH-13266 (JDK-8061577).
99-
* We could have done Timestamp.from( localDateTime.atZone( ZoneId.systemDefault() ).toInstant() ),
100-
* but on top of being more complex than the line below, it won't always work.
101-
* Timestamp.from() assumes the number of milliseconds since the epoch
102-
* means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
103-
*/
97+
// Workaround for HHH-13266 (JDK-8061577).
98+
// We could have done Timestamp.from( localDateTime.atZone( ZoneId.systemDefault() ).toInstant() ),
99+
// but on top of being more complex than the line below, it won't always work.
100+
// Timestamp.from() assumes the number of milliseconds since the epoch means the
101+
// same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
104102
return (X) Timestamp.valueOf( localDateTime );
105103
}
106104

@@ -134,13 +132,11 @@ public <X> LocalDate wrap(X value, WrapperOptions options) {
134132
}
135133

136134
if (value instanceof Timestamp timestamp) {
137-
/*
138-
* Workaround for HHH-13266 (JDK-8061577).
139-
* We used to do LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ).toLocalDate(),
140-
* but on top of being more complex than the line below, it won't always work.
141-
* ts.toInstant() assumes the number of milliseconds since the epoch
142-
* means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
143-
*/
135+
// Workaround for HHH-13266 (JDK-8061577).
136+
// We used to do LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ).toLocalDate(),
137+
// but on top of being more complex than the line below, it won't always work.
138+
// ts.toInstant() assumes the number of milliseconds since the epoch means the
139+
// same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
144140
return timestamp.toLocalDateTime().toLocalDate();
145141
}
146142

@@ -154,12 +150,9 @@ public <X> LocalDate wrap(X value, WrapperOptions options) {
154150
}
155151

156152
if (value instanceof Date date) {
157-
if (value instanceof java.sql.Date sqlDate) {
158-
return sqlDate.toLocalDate();
159-
}
160-
else {
161-
return Instant.ofEpochMilli( date.getTime() ).atZone( ZoneId.systemDefault() ).toLocalDate();
162-
}
153+
return value instanceof java.sql.Date sqlDate
154+
? sqlDate.toLocalDate()
155+
: Instant.ofEpochMilli( date.getTime() ).atZone( ZoneId.systemDefault() ).toLocalDate();
163156
}
164157

165158
throw unknownWrap( value.getClass() );

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalDateTimeJavaType.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,11 @@ public <X> X unwrap(LocalDateTime value, Class<X> type, WrapperOptions options)
8989
}
9090

9191
if ( Timestamp.class.isAssignableFrom( type ) ) {
92-
/*
93-
* Workaround for HHH-13266 (JDK-8061577).
94-
* We used to do Timestamp.from( value.atZone( ZoneId.systemDefault() ).toInstant() ),
95-
* but on top of being more complex than the line below, it won't always work.
96-
* Timestamp.from() assumes the number of milliseconds since the epoch
97-
* means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
98-
*/
92+
// Workaround for HHH-13266 (JDK-8061577).
93+
// We used to do Timestamp.from( value.atZone( ZoneId.systemDefault() ).toInstant() ),
94+
// but on top of being more complex than the line below, it won't always work.
95+
// Timestamp.from() assumes the number of milliseconds since the epoch means the
96+
// same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
9997
return (X) Timestamp.valueOf( value );
10098
}
10199

@@ -137,13 +135,11 @@ public <X> LocalDateTime wrap(X value, WrapperOptions options) {
137135
}
138136

139137
if (value instanceof Timestamp timestamp) {
140-
/*
141-
* Workaround for HHH-13266 (JDK-8061577).
142-
* We used to do LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ),
143-
* but on top of being more complex than the line below, it won't always work.
144-
* ts.toInstant() assumes the number of milliseconds since the epoch
145-
* means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
146-
*/
138+
// Workaround for HHH-13266 (JDK-8061577).
139+
// We used to do LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ),
140+
// but on top of being more complex than the line below, it won't always work.
141+
// ts.toInstant() assumes the number of milliseconds since the epoch means the
142+
// same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
147143
return timestamp.toLocalDateTime();
148144
}
149145

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocalTimeJavaType.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222

2323
import org.hibernate.dialect.Dialect;
2424
import org.hibernate.type.SqlTypes;
25-
import org.hibernate.type.descriptor.DateTimeUtils;
2625
import org.hibernate.type.descriptor.WrapperOptions;
2726
import org.hibernate.type.descriptor.jdbc.JdbcType;
2827
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
2928
import org.hibernate.type.spi.TypeConfiguration;
3029

30+
import static org.hibernate.type.descriptor.DateTimeUtils.roundToPrecision;
31+
3132
/**
3233
* Java type descriptor for the {@link LocalTime} type.
3334
*
@@ -94,19 +95,20 @@ public <X> X unwrap(LocalTime value, Class<X> type, WrapperOptions options) {
9495

9596
if ( Time.class.isAssignableFrom( type ) ) {
9697
final var time = Time.valueOf( value );
97-
if ( value.getNano() == 0 ) {
98-
return (X) time;
99-
}
100-
// Preserve milliseconds, which java.sql.Time supports
101-
return (X) new Time( time.getTime() + DateTimeUtils.roundToPrecision( value.getNano(), 3 ) / 1000000 );
98+
final int nanos = value.getNano();
99+
return nanos == 0
100+
? (X) time
101+
// Preserve milliseconds, which java.sql.Time supports
102+
: (X) new Time( time.getTime() + roundToPrecision( nanos, 3 ) / 1000000 );
102103
}
103104

104105
// Oracle documentation says to set the Date to January 1, 1970 when convert from
105-
// a LocalTime to a Calendar. IMO the same should hold true for converting to all
106-
// the legacy Date/Time types...
107-
106+
// a LocalTime to a Calendar. IMO the same should hold true for converting to all
107+
// the legacy Date/Time types.
108108

109-
final var zonedDateTime = value.atDate( LocalDate.of( 1970, 1, 1 ) ).atZone( ZoneId.systemDefault() );
109+
final var zonedDateTime =
110+
value.atDate( LocalDate.of( 1970, 1, 1 ) )
111+
.atZone( ZoneId.systemDefault() );
110112

111113
if ( Calendar.class.isAssignableFrom( type ) ) {
112114
return (X) GregorianCalendar.from( zonedDateTime );
@@ -140,7 +142,7 @@ public <X> LocalTime wrap(X value, WrapperOptions options) {
140142
}
141143

142144
if (value instanceof Time time) {
143-
final LocalTime localTime = time.toLocalTime();
145+
final var localTime = time.toLocalTime();
144146
long millis = time.getTime() % 1000;
145147
if ( millis == 0 ) {
146148
return localTime;

0 commit comments

Comments
 (0)