Skip to content

Commit 8dd66d5

Browse files
feat: introduce java.time methods and variables (#2780)
This PR introduces `java.time` alternatives to existing `org.threeten.bp.*` methods, as well as switching internal variables (if any) to `java.time` The main constraint is to keep the changes backwards compatible, so for each existing threeten method "`method1(org.threeten.bp.Duration)`" we will add an alternative with a _Duration_ (or _Timestamp_ when applicable) suffix: "`method1Duration(java.time.Duration)`". For most cases, the implementation will be held in the `java.time` method and the old threeten method will just delegate the call to it. However, for the case of abstract classes, the implementation will be kept in the threeten method to avoid breaking changes (i.e. users that already overloaded the method in their user code).
1 parent 7703ab2 commit 8dd66d5

40 files changed

+473
-274
lines changed

google-cloud-bigquerystorage/clirr-ignored-differences.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,21 @@
212212
<className>com/google/cloud/bigquery/storage/v1/StreamWriter</className>
213213
<method>void setMissingValueInterpretationMap(java.util.Map)</method>
214214
</difference>
215+
<difference>
216+
<!--This class is marked as @InternalApi "public for technical reasons"-->
217+
<differenceType>6004</differenceType>
218+
<className>com/google/cloud/bigquery/storage/*/stub/readrows/ApiResultRetryAlgorithm</className>
219+
<field>DEADLINE_SLEEP_DURATION</field>
220+
<from>org.threeten.bp.Duration</from>
221+
<to>java.time.Duration</to>
222+
</difference>
223+
<difference>
224+
<!--The retryDelay field is used by ApiResultRetryAlgorithm, which is marked as @InternalApi-->
225+
<differenceType>6004</differenceType>
226+
<className>com/google/cloud/bigquery/storage/util/Errors$IsRetryableStatusResult</className>
227+
<field>retryDelay</field>
228+
<from>org.threeten.bp.Duration</from>
229+
<to>java.time.Duration</to>
230+
</difference>
215231
</differences>
216232

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/util/Errors.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import io.grpc.Metadata;
2020
import io.grpc.Status;
2121
import io.grpc.protobuf.ProtoUtils;
22-
import org.threeten.bp.Duration;
22+
import java.time.Duration;
2323

2424
/** Static utility methods for working with Errors returned from the service. */
2525
public class Errors {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigquery.storage.util;
18+
19+
import com.google.api.core.InternalApi;
20+
21+
/**
22+
* Convenience methods for conversions between {@link java.time} and {@link org.threeten.bp}
23+
* objects. This will be kept until <a
24+
* href="https://github.com/googleapis/sdk-platform-java/issues/3412">this issue</a> is solved.
25+
*/
26+
@InternalApi("https://github.com/googleapis/sdk-platform-java/issues/3412")
27+
public class TimeConversionUtils {
28+
public static java.time.LocalDateTime toJavaTimeLocalDateTime(
29+
org.threeten.bp.LocalDateTime result) {
30+
return java.time.LocalDateTime.of(
31+
result.getYear(),
32+
java.time.Month.of(result.getMonth().getValue()),
33+
result.getDayOfMonth(),
34+
result.getHour(),
35+
result.getMinute(),
36+
result.getSecond(),
37+
result.getNano());
38+
}
39+
40+
public static org.threeten.bp.LocalDateTime toThreetenLocalDateTime(
41+
java.time.LocalDateTime result) {
42+
return org.threeten.bp.LocalDateTime.of(
43+
result.getYear(),
44+
org.threeten.bp.Month.of(result.getMonth().getValue()),
45+
result.getDayOfMonth(),
46+
result.getHour(),
47+
result.getMinute(),
48+
result.getSecond(),
49+
result.getNano());
50+
}
51+
52+
public static java.time.LocalTime toJavaTimeLocalTime(org.threeten.bp.LocalTime result) {
53+
return java.time.LocalTime.of(
54+
result.getHour(), result.getMinute(), result.getSecond(), result.getNano());
55+
}
56+
57+
public static org.threeten.bp.LocalTime toThreetenLocalTime(java.time.LocalTime result) {
58+
return org.threeten.bp.LocalTime.of(
59+
result.getHour(), result.getMinute(), result.getSecond(), result.getNano());
60+
}
61+
}

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/CivilTimeEncoder.java

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
*/
1616
package com.google.cloud.bigquery.storage.v1;
1717

18+
import static com.google.cloud.bigquery.storage.util.TimeConversionUtils.toJavaTimeLocalDateTime;
19+
import static com.google.cloud.bigquery.storage.util.TimeConversionUtils.toJavaTimeLocalTime;
20+
import static com.google.cloud.bigquery.storage.util.TimeConversionUtils.toThreetenLocalDateTime;
21+
import static com.google.cloud.bigquery.storage.util.TimeConversionUtils.toThreetenLocalTime;
1822
import static com.google.common.base.Preconditions.checkArgument;
1923

20-
import org.threeten.bp.DateTimeException;
21-
import org.threeten.bp.LocalDateTime;
22-
import org.threeten.bp.LocalTime;
23-
import org.threeten.bp.temporal.ChronoUnit;
24+
import com.google.api.core.ObsoleteApi;
25+
import java.time.DateTimeException;
26+
import java.time.temporal.ChronoUnit;
2427

2528
/**
2629
* Ported from ZetaSQL CivilTimeEncoder Original code can be found at:
@@ -89,7 +92,7 @@ public final class CivilTimeEncoder {
8992
* @see #decodePacked32TimeSeconds(int)
9093
*/
9194
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
92-
private static int encodePacked32TimeSeconds(LocalTime time) {
95+
private static int encodePacked32TimeSeconds(java.time.LocalTime time) {
9396
checkValidTimeSeconds(time);
9497
int bitFieldTimeSeconds = 0x0;
9598
bitFieldTimeSeconds |= time.getHour() << HOUR_SHIFT;
@@ -112,19 +115,29 @@ private static int encodePacked32TimeSeconds(LocalTime time) {
112115
* @see #encodePacked32TimeSeconds(LocalTime)
113116
*/
114117
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
115-
private static LocalTime decodePacked32TimeSeconds(int bitFieldTimeSeconds) {
118+
private static java.time.LocalTime decodePacked32TimeSeconds(int bitFieldTimeSeconds) {
116119
checkValidBitField(bitFieldTimeSeconds, TIME_SECONDS_MASK);
117120
int hourOfDay = getFieldFromBitField(bitFieldTimeSeconds, HOUR_MASK, HOUR_SHIFT);
118121
int minuteOfHour = getFieldFromBitField(bitFieldTimeSeconds, MINUTE_MASK, MINUTE_SHIFT);
119122
int secondOfMinute = getFieldFromBitField(bitFieldTimeSeconds, SECOND_MASK, SECOND_SHIFT);
120123
// LocalTime validates the input parameters.
121124
try {
122-
return LocalTime.of(hourOfDay, minuteOfHour, secondOfMinute);
125+
return java.time.LocalTime.of(hourOfDay, minuteOfHour, secondOfMinute);
123126
} catch (DateTimeException e) {
124127
throw new IllegalArgumentException(e.getMessage(), e);
125128
}
126129
}
127130

131+
/**
132+
* This method is obsolete. Use {@link #encodePacked64TimeMicrosLocalTime(java.time.LocalTime)}
133+
* instead.
134+
*/
135+
@ObsoleteApi("Use encodePacked64TimeMicrosLocalTime(java.time.LocalTime) instead")
136+
@SuppressWarnings("GoodTime")
137+
public static long encodePacked64TimeMicros(org.threeten.bp.LocalTime time) {
138+
return encodePacked64TimeMicrosLocalTime(toJavaTimeLocalTime(time));
139+
}
140+
128141
/**
129142
* Encodes {@code time} as a 8-byte integer with microseconds precision.
130143
*
@@ -140,13 +153,21 @@ private static LocalTime decodePacked32TimeSeconds(int bitFieldTimeSeconds) {
140153
* @see #encodePacked64TimeMicros(LocalTime)
141154
*/
142155
@SuppressWarnings("GoodTime")
143-
public static long encodePacked64TimeMicros(LocalTime time) {
156+
public static long encodePacked64TimeMicrosLocalTime(java.time.LocalTime time) {
144157
checkValidTimeMicros(time);
145158
return (((long) encodePacked32TimeSeconds(time)) << MICRO_LENGTH) | (time.getNano() / 1_000L);
146159
}
147160

161+
/** This method is obsolete. Use {@link #decodePacked64TimeMicrosLocalTime(long)} instead. */
162+
@ObsoleteApi("Use decodePacked64TimeMicrosLocalTime(long) instead")
163+
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
164+
public static org.threeten.bp.LocalTime decodePacked64TimeMicros(long bitFieldTimeMicros) {
165+
return toThreetenLocalTime(decodePacked64TimeMicrosLocalTime(bitFieldTimeMicros));
166+
}
167+
148168
/**
149-
* Decodes {@code bitFieldTimeMicros} as a {@link LocalTime} with microseconds precision.
169+
* Decodes {@code bitFieldTimeMicros} as a {@link java.time.LocalTime} with microseconds
170+
* precision.
150171
*
151172
* <p>Encoding is as the following:
152173
*
@@ -159,13 +180,13 @@ public static long encodePacked64TimeMicros(LocalTime time) {
159180
* @see #encodePacked64TimeMicros(LocalTime)
160181
*/
161182
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
162-
public static LocalTime decodePacked64TimeMicros(long bitFieldTimeMicros) {
183+
public static java.time.LocalTime decodePacked64TimeMicrosLocalTime(long bitFieldTimeMicros) {
163184
checkValidBitField(bitFieldTimeMicros, TIME_MICROS_MASK);
164185
int bitFieldTimeSeconds = (int) (bitFieldTimeMicros >> MICRO_LENGTH);
165-
LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);
186+
java.time.LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);
166187
int microOfSecond = getFieldFromBitField(bitFieldTimeMicros, MICRO_MASK, MICRO_SHIFT);
167188
checkValidMicroOfSecond(microOfSecond);
168-
LocalTime time = timeSeconds.withNano(microOfSecond * 1000);
189+
java.time.LocalTime time = timeSeconds.withNano(microOfSecond * 1000);
169190
checkValidTimeMicros(time);
170191
return time;
171192
}
@@ -184,7 +205,7 @@ public static LocalTime decodePacked64TimeMicros(long bitFieldTimeMicros) {
184205
* @see #decodePacked64DatetimeSeconds(long)
185206
*/
186207
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
187-
private static long encodePacked64DatetimeSeconds(LocalDateTime dateTime) {
208+
private static long encodePacked64DatetimeSeconds(java.time.LocalDateTime dateTime) {
188209
checkValidDateTimeSeconds(dateTime);
189210
long bitFieldDatetimeSeconds = 0x0L;
190211
bitFieldDatetimeSeconds |= (long) dateTime.getYear() << YEAR_SHIFT;
@@ -208,16 +229,17 @@ private static long encodePacked64DatetimeSeconds(LocalDateTime dateTime) {
208229
* @see #encodePacked64DatetimeSeconds(LocalDateTime)
209230
*/
210231
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
211-
private static LocalDateTime decodePacked64DatetimeSeconds(long bitFieldDatetimeSeconds) {
232+
private static java.time.LocalDateTime decodePacked64DatetimeSeconds(
233+
long bitFieldDatetimeSeconds) {
212234
checkValidBitField(bitFieldDatetimeSeconds, DATETIME_SECONDS_MASK);
213235
int bitFieldTimeSeconds = (int) (bitFieldDatetimeSeconds & TIME_SECONDS_MASK);
214-
LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);
236+
java.time.LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);
215237
int year = getFieldFromBitField(bitFieldDatetimeSeconds, YEAR_MASK, YEAR_SHIFT);
216238
int monthOfYear = getFieldFromBitField(bitFieldDatetimeSeconds, MONTH_MASK, MONTH_SHIFT);
217239
int dayOfMonth = getFieldFromBitField(bitFieldDatetimeSeconds, DAY_MASK, DAY_SHIFT);
218240
try {
219-
LocalDateTime dateTime =
220-
LocalDateTime.of(
241+
java.time.LocalDateTime dateTime =
242+
java.time.LocalDateTime.of(
221243
year,
222244
monthOfYear,
223245
dayOfMonth,
@@ -231,6 +253,16 @@ private static LocalDateTime decodePacked64DatetimeSeconds(long bitFieldDatetime
231253
}
232254
}
233255

256+
/**
257+
* This method is obsolete. Use {@link
258+
* #encodePacked64DatetimeMicrosLocalDateTime(java.time.LocalDateTime)} instead.
259+
*/
260+
@ObsoleteApi("Use encodePacked64DatetimeMicrosLocalDateTime(java.time.LocalDateTime) instead")
261+
@SuppressWarnings({"GoodTime-ApiWithNumericTimeUnit", "JavaLocalDateTimeGetNano"})
262+
public static long encodePacked64DatetimeMicros(org.threeten.bp.LocalDateTime dateTime) {
263+
return encodePacked64DatetimeMicrosLocalDateTime(toJavaTimeLocalDateTime(dateTime));
264+
}
265+
234266
/**
235267
* Encodes {@code dateTime} as a 8-byte integer with microseconds precision.
236268
*
@@ -245,14 +277,26 @@ private static LocalDateTime decodePacked64DatetimeSeconds(long bitFieldDatetime
245277
* @see #decodePacked64DatetimeMicros(long)
246278
*/
247279
@SuppressWarnings({"GoodTime-ApiWithNumericTimeUnit", "JavaLocalDateTimeGetNano"})
248-
public static long encodePacked64DatetimeMicros(LocalDateTime dateTime) {
280+
public static long encodePacked64DatetimeMicrosLocalDateTime(java.time.LocalDateTime dateTime) {
249281
checkValidDateTimeMicros(dateTime);
250282
return (encodePacked64DatetimeSeconds(dateTime) << MICRO_LENGTH)
251283
| (dateTime.getNano() / 1_000L);
252284
}
253285

254286
/**
255-
* Decodes {@code bitFieldDatetimeMicros} as a {@link LocalDateTime} with microseconds precision.
287+
* This method is obsolete. Use {@link #decodePacked64DatetimeMicrosLocalDateTime(long)} instead.
288+
*/
289+
@ObsoleteApi("Use decodePacked64DatetimeMicrosLocalDateTime(long) instead")
290+
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
291+
public static org.threeten.bp.LocalDateTime decodePacked64DatetimeMicros(
292+
long bitFieldDatetimeMicros) {
293+
return toThreetenLocalDateTime(
294+
decodePacked64DatetimeMicrosLocalDateTime(bitFieldDatetimeMicros));
295+
}
296+
297+
/**
298+
* Decodes {@code bitFieldDatetimeMicros} as a {@link java.time.LocalDateTime} with microseconds
299+
* precision.
256300
*
257301
* <p>Encoding is as the following:
258302
*
@@ -265,13 +309,15 @@ public static long encodePacked64DatetimeMicros(LocalDateTime dateTime) {
265309
* @see #encodePacked64DatetimeMicros(LocalDateTime)
266310
*/
267311
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
268-
public static LocalDateTime decodePacked64DatetimeMicros(long bitFieldDatetimeMicros) {
312+
public static java.time.LocalDateTime decodePacked64DatetimeMicrosLocalDateTime(
313+
long bitFieldDatetimeMicros) {
269314
checkValidBitField(bitFieldDatetimeMicros, DATETIME_MICROS_MASK);
270315
long bitFieldDatetimeSeconds = bitFieldDatetimeMicros >> MICRO_LENGTH;
271-
LocalDateTime dateTimeSeconds = decodePacked64DatetimeSeconds(bitFieldDatetimeSeconds);
316+
java.time.LocalDateTime dateTimeSeconds =
317+
decodePacked64DatetimeSeconds(bitFieldDatetimeSeconds);
272318
int microOfSecond = getFieldFromBitField(bitFieldDatetimeMicros, MICRO_MASK, MICRO_SHIFT);
273319
checkValidMicroOfSecond(microOfSecond);
274-
LocalDateTime dateTime = dateTimeSeconds.withNano(microOfSecond * 1_000);
320+
java.time.LocalDateTime dateTime = dateTimeSeconds.withNano(microOfSecond * 1_000);
275321
checkValidDateTimeMicros(dateTime);
276322
return dateTime;
277323
}
@@ -280,25 +326,25 @@ private static int getFieldFromBitField(long bitField, long mask, int shift) {
280326
return (int) ((bitField & mask) >> shift);
281327
}
282328

283-
private static void checkValidTimeSeconds(LocalTime time) {
329+
private static void checkValidTimeSeconds(java.time.LocalTime time) {
284330
checkArgument(time.getHour() >= 0 && time.getHour() <= 23);
285331
checkArgument(time.getMinute() >= 0 && time.getMinute() <= 59);
286332
checkArgument(time.getSecond() >= 0 && time.getSecond() <= 59);
287333
}
288334

289-
private static void checkValidDateTimeSeconds(LocalDateTime dateTime) {
335+
private static void checkValidDateTimeSeconds(java.time.LocalDateTime dateTime) {
290336
checkArgument(dateTime.getYear() >= 1 && dateTime.getYear() <= 9999);
291337
checkArgument(dateTime.getMonthValue() >= 1 && dateTime.getMonthValue() <= 12);
292338
checkArgument(dateTime.getDayOfMonth() >= 1 && dateTime.getDayOfMonth() <= 31);
293339
checkValidTimeSeconds(dateTime.toLocalTime());
294340
}
295341

296-
private static void checkValidTimeMicros(LocalTime time) {
342+
private static void checkValidTimeMicros(java.time.LocalTime time) {
297343
checkValidTimeSeconds(time);
298344
checkArgument(time.equals(time.truncatedTo(ChronoUnit.MICROS)));
299345
}
300346

301-
private static void checkValidDateTimeMicros(LocalDateTime dateTime) {
347+
private static void checkValidDateTimeMicros(java.time.LocalDateTime dateTime) {
302348
checkValidDateTimeSeconds(dateTime);
303349
checkArgument(dateTime.equals(dateTime.truncatedTo(ChronoUnit.MICROS)));
304350
}

0 commit comments

Comments
 (0)