Skip to content

Commit 7feaaa8

Browse files
committed
feat: introduce java.time methods and variables
1 parent 5f1b30a commit 7feaaa8

38 files changed

+423
-262
lines changed

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 {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
public class TimeConversionUtils {
20+
public static java.time.LocalDateTime toJavaTimeLocalDateTime(
21+
org.threeten.bp.LocalDateTime result) {
22+
return java.time.LocalDateTime.of(
23+
result.getYear(),
24+
java.time.Month.of(result.getMonth().getValue()),
25+
result.getDayOfMonth(),
26+
result.getHour(),
27+
result.getMinute(),
28+
result.getSecond(),
29+
result.getNano());
30+
}
31+
32+
public static org.threeten.bp.LocalDateTime toThreetenLocalDateTime(
33+
java.time.LocalDateTime result) {
34+
return org.threeten.bp.LocalDateTime.of(
35+
result.getYear(),
36+
org.threeten.bp.Month.of(result.getMonth().getValue()),
37+
result.getDayOfMonth(),
38+
result.getHour(),
39+
result.getMinute(),
40+
result.getSecond(),
41+
result.getNano());
42+
}
43+
44+
public static java.time.LocalTime toJavaTimeLocalTime(org.threeten.bp.LocalTime result) {
45+
return java.time.LocalTime.of(
46+
result.getHour(), result.getMinute(), result.getSecond(), result.getNano());
47+
}
48+
49+
public static org.threeten.bp.LocalTime toThreetenLocalTime(java.time.LocalTime result) {
50+
return org.threeten.bp.LocalTime.of(
51+
result.getHour(), result.getMinute(), result.getSecond(), result.getNano());
52+
}
53+
}

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

Lines changed: 68 additions & 24 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,11 +153,18 @@ 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
/**
149169
* Decodes {@code bitFieldTimeMicros} as a {@link LocalTime} with microseconds precision.
150170
*
@@ -159,13 +179,13 @@ public static long encodePacked64TimeMicros(LocalTime time) {
159179
* @see #encodePacked64TimeMicros(LocalTime)
160180
*/
161181
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
162-
public static LocalTime decodePacked64TimeMicros(long bitFieldTimeMicros) {
182+
public static java.time.LocalTime decodePacked64TimeMicrosLocalTime(long bitFieldTimeMicros) {
163183
checkValidBitField(bitFieldTimeMicros, TIME_MICROS_MASK);
164184
int bitFieldTimeSeconds = (int) (bitFieldTimeMicros >> MICRO_LENGTH);
165-
LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);
185+
java.time.LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);
166186
int microOfSecond = getFieldFromBitField(bitFieldTimeMicros, MICRO_MASK, MICRO_SHIFT);
167187
checkValidMicroOfSecond(microOfSecond);
168-
LocalTime time = timeSeconds.withNano(microOfSecond * 1000);
188+
java.time.LocalTime time = timeSeconds.withNano(microOfSecond * 1000);
169189
checkValidTimeMicros(time);
170190
return time;
171191
}
@@ -184,7 +204,7 @@ public static LocalTime decodePacked64TimeMicros(long bitFieldTimeMicros) {
184204
* @see #decodePacked64DatetimeSeconds(long)
185205
*/
186206
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
187-
private static long encodePacked64DatetimeSeconds(LocalDateTime dateTime) {
207+
private static long encodePacked64DatetimeSeconds(java.time.LocalDateTime dateTime) {
188208
checkValidDateTimeSeconds(dateTime);
189209
long bitFieldDatetimeSeconds = 0x0L;
190210
bitFieldDatetimeSeconds |= (long) dateTime.getYear() << YEAR_SHIFT;
@@ -208,16 +228,17 @@ private static long encodePacked64DatetimeSeconds(LocalDateTime dateTime) {
208228
* @see #encodePacked64DatetimeSeconds(LocalDateTime)
209229
*/
210230
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
211-
private static LocalDateTime decodePacked64DatetimeSeconds(long bitFieldDatetimeSeconds) {
231+
private static java.time.LocalDateTime decodePacked64DatetimeSeconds(
232+
long bitFieldDatetimeSeconds) {
212233
checkValidBitField(bitFieldDatetimeSeconds, DATETIME_SECONDS_MASK);
213234
int bitFieldTimeSeconds = (int) (bitFieldDatetimeSeconds & TIME_SECONDS_MASK);
214-
LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);
235+
java.time.LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);
215236
int year = getFieldFromBitField(bitFieldDatetimeSeconds, YEAR_MASK, YEAR_SHIFT);
216237
int monthOfYear = getFieldFromBitField(bitFieldDatetimeSeconds, MONTH_MASK, MONTH_SHIFT);
217238
int dayOfMonth = getFieldFromBitField(bitFieldDatetimeSeconds, DAY_MASK, DAY_SHIFT);
218239
try {
219-
LocalDateTime dateTime =
220-
LocalDateTime.of(
240+
java.time.LocalDateTime dateTime =
241+
java.time.LocalDateTime.of(
221242
year,
222243
monthOfYear,
223244
dayOfMonth,
@@ -231,6 +252,16 @@ private static LocalDateTime decodePacked64DatetimeSeconds(long bitFieldDatetime
231252
}
232253
}
233254

255+
/**
256+
* This method is obsolete. Use {@link
257+
* #encodePacked64DatetimeMicrosLocalDateTime(java.time.LocalDateTime)} instead.
258+
*/
259+
@ObsoleteApi("Use encodePacked64DatetimeMicrosLocalDateTime(java.time.LocalDateTime) instead")
260+
@SuppressWarnings({"GoodTime-ApiWithNumericTimeUnit", "JavaLocalDateTimeGetNano"})
261+
public static long encodePacked64DatetimeMicros(org.threeten.bp.LocalDateTime dateTime) {
262+
return encodePacked64DatetimeMicrosLocalDateTime(toJavaTimeLocalDateTime(dateTime));
263+
}
264+
234265
/**
235266
* Encodes {@code dateTime} as a 8-byte integer with microseconds precision.
236267
*
@@ -245,12 +276,23 @@ private static LocalDateTime decodePacked64DatetimeSeconds(long bitFieldDatetime
245276
* @see #decodePacked64DatetimeMicros(long)
246277
*/
247278
@SuppressWarnings({"GoodTime-ApiWithNumericTimeUnit", "JavaLocalDateTimeGetNano"})
248-
public static long encodePacked64DatetimeMicros(LocalDateTime dateTime) {
279+
public static long encodePacked64DatetimeMicrosLocalDateTime(java.time.LocalDateTime dateTime) {
249280
checkValidDateTimeMicros(dateTime);
250281
return (encodePacked64DatetimeSeconds(dateTime) << MICRO_LENGTH)
251282
| (dateTime.getNano() / 1_000L);
252283
}
253284

285+
/**
286+
* This method is obsolete. Use {@link #decodePacked64DatetimeMicrosLocalDateTime(long)} instead.
287+
*/
288+
@ObsoleteApi("Use decodePacked64DatetimeMicrosLocalDateTime(long) instead")
289+
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
290+
public static org.threeten.bp.LocalDateTime decodePacked64DatetimeMicros(
291+
long bitFieldDatetimeMicros) {
292+
return toThreetenLocalDateTime(
293+
decodePacked64DatetimeMicrosLocalDateTime(bitFieldDatetimeMicros));
294+
}
295+
254296
/**
255297
* Decodes {@code bitFieldDatetimeMicros} as a {@link LocalDateTime} with microseconds precision.
256298
*
@@ -265,13 +307,15 @@ public static long encodePacked64DatetimeMicros(LocalDateTime dateTime) {
265307
* @see #encodePacked64DatetimeMicros(LocalDateTime)
266308
*/
267309
@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")
268-
public static LocalDateTime decodePacked64DatetimeMicros(long bitFieldDatetimeMicros) {
310+
public static java.time.LocalDateTime decodePacked64DatetimeMicrosLocalDateTime(
311+
long bitFieldDatetimeMicros) {
269312
checkValidBitField(bitFieldDatetimeMicros, DATETIME_MICROS_MASK);
270313
long bitFieldDatetimeSeconds = bitFieldDatetimeMicros >> MICRO_LENGTH;
271-
LocalDateTime dateTimeSeconds = decodePacked64DatetimeSeconds(bitFieldDatetimeSeconds);
314+
java.time.LocalDateTime dateTimeSeconds =
315+
decodePacked64DatetimeSeconds(bitFieldDatetimeSeconds);
272316
int microOfSecond = getFieldFromBitField(bitFieldDatetimeMicros, MICRO_MASK, MICRO_SHIFT);
273317
checkValidMicroOfSecond(microOfSecond);
274-
LocalDateTime dateTime = dateTimeSeconds.withNano(microOfSecond * 1_000);
318+
java.time.LocalDateTime dateTime = dateTimeSeconds.withNano(microOfSecond * 1_000);
275319
checkValidDateTimeMicros(dateTime);
276320
return dateTime;
277321
}
@@ -280,25 +324,25 @@ private static int getFieldFromBitField(long bitField, long mask, int shift) {
280324
return (int) ((bitField & mask) >> shift);
281325
}
282326

283-
private static void checkValidTimeSeconds(LocalTime time) {
327+
private static void checkValidTimeSeconds(java.time.LocalTime time) {
284328
checkArgument(time.getHour() >= 0 && time.getHour() <= 23);
285329
checkArgument(time.getMinute() >= 0 && time.getMinute() <= 59);
286330
checkArgument(time.getSecond() >= 0 && time.getSecond() <= 59);
287331
}
288332

289-
private static void checkValidDateTimeSeconds(LocalDateTime dateTime) {
333+
private static void checkValidDateTimeSeconds(java.time.LocalDateTime dateTime) {
290334
checkArgument(dateTime.getYear() >= 1 && dateTime.getYear() <= 9999);
291335
checkArgument(dateTime.getMonthValue() >= 1 && dateTime.getMonthValue() <= 12);
292336
checkArgument(dateTime.getDayOfMonth() >= 1 && dateTime.getDayOfMonth() <= 31);
293337
checkValidTimeSeconds(dateTime.toLocalTime());
294338
}
295339

296-
private static void checkValidTimeMicros(LocalTime time) {
340+
private static void checkValidTimeMicros(java.time.LocalTime time) {
297341
checkValidTimeSeconds(time);
298342
checkArgument(time.equals(time.truncatedTo(ChronoUnit.MICROS)));
299343
}
300344

301-
private static void checkValidDateTimeMicros(LocalDateTime dateTime) {
345+
private static void checkValidDateTimeMicros(java.time.LocalDateTime dateTime) {
302346
checkValidDateTimeSeconds(dateTime);
303347
checkArgument(dateTime.equals(dateTime.truncatedTo(ChronoUnit.MICROS)));
304348
}

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@
3030
import java.math.BigDecimal;
3131
import java.math.RoundingMode;
3232
import java.time.LocalDate;
33+
import java.time.LocalDateTime;
34+
import java.time.LocalTime;
35+
import java.time.ZoneOffset;
36+
import java.time.format.DateTimeFormatter;
37+
import java.time.format.DateTimeFormatterBuilder;
38+
import java.time.format.TextStyle;
39+
import java.time.temporal.ChronoField;
40+
import java.time.temporal.TemporalAccessor;
3341
import java.util.ArrayList;
3442
import java.util.HashMap;
3543
import java.util.List;
3644
import java.util.Map;
3745
import org.json.JSONArray;
3846
import org.json.JSONException;
3947
import org.json.JSONObject;
40-
import org.threeten.bp.LocalDateTime;
41-
import org.threeten.bp.LocalTime;
42-
import org.threeten.bp.ZoneOffset;
43-
import org.threeten.bp.format.DateTimeFormatter;
44-
import org.threeten.bp.format.DateTimeFormatterBuilder;
45-
import org.threeten.bp.format.TextStyle;
46-
import org.threeten.bp.temporal.ChronoField;
47-
import org.threeten.bp.temporal.TemporalAccessor;
4848

4949
/**
5050
* Converts JSON data to Protobuf messages given the Protobuf descriptor and BigQuery table schema.
@@ -604,7 +604,7 @@ private void fillField(
604604
if (val instanceof String) {
605605
protoMsg.setField(
606606
fieldDescriptor,
607-
CivilTimeEncoder.encodePacked64DatetimeMicros(
607+
CivilTimeEncoder.encodePacked64DatetimeMicrosLocalDateTime(
608608
LocalDateTime.parse((String) val, DATETIME_FORMATTER)));
609609
return;
610610
} else if (val instanceof Long) {
@@ -615,7 +615,8 @@ private void fillField(
615615
if (val instanceof String) {
616616
protoMsg.setField(
617617
fieldDescriptor,
618-
CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.parse((String) val)));
618+
CivilTimeEncoder.encodePacked64TimeMicrosLocalTime(
619+
LocalTime.parse((String) val)));
619620
return;
620621
} else if (val instanceof Long) {
621622
protoMsg.setField(fieldDescriptor, val);
@@ -872,7 +873,7 @@ private void fillRepeatedField(
872873
if (val instanceof String) {
873874
protoMsg.addRepeatedField(
874875
fieldDescriptor,
875-
CivilTimeEncoder.encodePacked64DatetimeMicros(
876+
CivilTimeEncoder.encodePacked64DatetimeMicrosLocalDateTime(
876877
LocalDateTime.parse((String) val, DATETIME_FORMATTER)));
877878
} else if (val instanceof Long) {
878879
protoMsg.addRepeatedField(fieldDescriptor, val);
@@ -883,7 +884,8 @@ private void fillRepeatedField(
883884
if (val instanceof String) {
884885
protoMsg.addRepeatedField(
885886
fieldDescriptor,
886-
CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.parse((String) val)));
887+
CivilTimeEncoder.encodePacked64TimeMicrosLocalTime(
888+
LocalTime.parse((String) val)));
887889
} else if (val instanceof Long) {
888890
protoMsg.addRepeatedField(fieldDescriptor, val);
889891
} else {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ static BigQueryWriteSettings getBigQueryWriteSettings(Builder builder) throws IO
372372
new BigQueryWriteSettings.Builder()
373373
.setTransportChannelProvider(
374374
BigQueryWriteSettings.defaultGrpcTransportProviderBuilder()
375-
.setKeepAliveTime(org.threeten.bp.Duration.ofMinutes(1))
376-
.setKeepAliveTimeout(org.threeten.bp.Duration.ofMinutes(1))
375+
.setKeepAliveTimeDuration(java.time.Duration.ofMinutes(1))
376+
.setKeepAliveTimeoutDuration(java.time.Duration.ofMinutes(1))
377377
.setKeepAliveWithoutCalls(true)
378378
.setChannelsPerCpu(2)
379379
.build())

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/stub/readrows/ApiResultRetryAlgorithm.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import com.google.cloud.bigquery.storage.v1.BigQueryReadSettings;
2525
import io.grpc.Metadata;
2626
import io.grpc.Status;
27-
import org.threeten.bp.Duration;
27+
import java.time.Duration;
2828

2929
/** For internal use, public for technical reasons. */
3030
@InternalApi
@@ -56,17 +56,17 @@ public TimedAttemptSettings createNextAttempt(
5656
Duration retryDelay = result.retryDelay;
5757
Duration randomizedRetryDelay = result.retryDelay;
5858
if (retryDelay == null) {
59-
retryDelay = prevSettings.getRetryDelay();
59+
retryDelay = prevSettings.getRetryDelayDuration();
6060
randomizedRetryDelay = DEADLINE_SLEEP_DURATION;
6161
}
6262
if (retryAttemptListener != null) {
6363
retryAttemptListener.onRetryAttempt(status, metadata);
6464
}
6565
return TimedAttemptSettings.newBuilder()
6666
.setGlobalSettings(prevSettings.getGlobalSettings())
67-
.setRetryDelay(retryDelay)
67+
.setRetryDelayDuration(retryDelay)
6868
.setRpcTimeout(prevSettings.getRpcTimeout())
69-
.setRandomizedRetryDelay(randomizedRetryDelay)
69+
.setRandomizedRetryDelayDuration(randomizedRetryDelay)
7070
.setAttemptCount(prevSettings.getAttemptCount() + 1)
7171
.setFirstAttemptStartTimeNanos(prevSettings.getFirstAttemptStartTimeNanos())
7272
.build();

0 commit comments

Comments
 (0)