Skip to content

Commit 31fb15f

Browse files
feat: introduce java.time methods and variables (#3586)
* feat: introduce `java.time` methods and variables * remove threeten dependency
1 parent c0b874a commit 31fb15f

File tree

11 files changed

+59
-58
lines changed

11 files changed

+59
-58
lines changed

google-cloud-bigquery/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@
8585
<groupId>com.google.api</groupId>
8686
<artifactId>gax</artifactId>
8787
</dependency>
88-
<dependency>
89-
<groupId>org.threeten</groupId>
90-
<artifactId>threetenbp</artifactId>
91-
</dependency>
9288
<dependency>
9389
<groupId>com.google.code.gson</groupId>
9490
<artifactId>gson</artifactId>

google-cloud-bigquery/src/benchmark/java/com/google/cloud/bigquery/benchmark/Benchmark.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import com.google.cloud.bigquery.QueryJobConfiguration;
2424
import com.google.cloud.bigquery.TableResult;
2525
import java.io.FileInputStream;
26+
import java.time.Clock;
27+
import java.time.Duration;
28+
import java.time.Instant;
2629
import java.util.List;
27-
import org.threeten.bp.Clock;
28-
import org.threeten.bp.Duration;
29-
import org.threeten.bp.Instant;
3030

3131
public class Benchmark {
3232

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060
import java.util.regex.Matcher;
6161
import java.util.regex.Pattern;
6262
import org.checkerframework.checker.nullness.qual.NonNull;
63-
import org.threeten.bp.Instant;
64-
import org.threeten.bp.temporal.ChronoUnit;
6563

6664
final class BigQueryImpl extends BaseService<BigQueryOptions> implements BigQuery {
6765

@@ -450,7 +448,9 @@ public com.google.api.services.bigquery.model.Job call() {
450448
long jobCreationTime = job.getStatistics().getCreationTime();
451449
long jobMinStaleTime = System.currentTimeMillis();
452450
long jobMaxStaleTime =
453-
Instant.ofEpochMilli(jobMinStaleTime).minus(1, ChronoUnit.DAYS).toEpochMilli();
451+
java.time.Instant.ofEpochMilli(jobMinStaleTime)
452+
.minus(1, java.time.temporal.ChronoUnit.DAYS)
453+
.toEpochMilli();
454454

455455
// Only return the job if it has been created in the past 24 hours.
456456
// This is assuming any job older than 24 hours is a valid duplicate JobID

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryRetryAlgorithm.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
import com.google.api.gax.retrying.TimedRetryAlgorithmWithContext;
2828
import com.google.gson.JsonObject;
2929
import com.google.gson.JsonParser;
30+
import java.time.Duration;
3031
import java.util.Iterator;
3132
import java.util.UUID;
3233
import java.util.concurrent.CancellationException;
3334
import java.util.logging.Level;
3435
import java.util.logging.Logger;
3536
import java.util.regex.Pattern;
36-
import org.threeten.bp.Duration;
3737

3838
public class BigQueryRetryAlgorithm<ResponseT> extends RetryAlgorithm<ResponseT> {
3939
private final BigQueryRetryConfig bigQueryRetryConfig;
@@ -67,7 +67,7 @@ public boolean shouldRetry(
6767
// Log retry info
6868
int attemptCount = nextAttemptSettings == null ? 0 : nextAttemptSettings.getAttemptCount();
6969
Duration retryDelay =
70-
nextAttemptSettings == null ? Duration.ZERO : nextAttemptSettings.getRetryDelay();
70+
nextAttemptSettings == null ? Duration.ZERO : nextAttemptSettings.getRetryDelayDuration();
7171
String errorMessage = previousThrowable != null ? previousThrowable.getMessage() : "";
7272

7373
// Implementing shouldRetryBasedOnBigQueryRetryConfig so that we can retry exceptions based on

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
import com.google.common.collect.ImmutableList;
3232
import java.io.IOException;
3333
import java.io.ObjectInputStream;
34+
import java.time.Duration;
3435
import java.util.ArrayList;
3536
import java.util.Arrays;
3637
import java.util.List;
3738
import java.util.Objects;
3839
import java.util.concurrent.Callable;
3940
import java.util.concurrent.ExecutionException;
40-
import org.threeten.bp.Duration;
4141

4242
/**
4343
* A Google BigQuery Job.
@@ -52,20 +52,20 @@ public class Job extends JobInfo {
5252

5353
private static final RetrySettings DEFAULT_JOB_WAIT_SETTINGS =
5454
RetrySettings.newBuilder()
55-
.setTotalTimeout(Duration.ofHours(12L))
56-
.setInitialRetryDelay(Duration.ofSeconds(1L))
55+
.setTotalTimeoutDuration(Duration.ofHours(12L))
56+
.setInitialRetryDelayDuration(Duration.ofSeconds(1L))
5757
.setRetryDelayMultiplier(2.0)
5858
.setJittered(true)
59-
.setMaxRetryDelay(Duration.ofMinutes(1L))
59+
.setMaxRetryDelayDuration(Duration.ofMinutes(1L))
6060
.build();
6161

6262
static final RetrySettings DEFAULT_QUERY_JOB_WAIT_SETTINGS =
6363
RetrySettings.newBuilder()
64-
.setTotalTimeout(Duration.ofHours(12L))
65-
.setInitialRetryDelay(Duration.ofSeconds(3L))
64+
.setTotalTimeoutDuration(Duration.ofHours(12L))
65+
.setInitialRetryDelayDuration(Duration.ofSeconds(3L))
6666
.setRetryDelayMultiplier(1.0)
6767
.setJittered(true)
68-
.setMaxRetryDelay(Duration.ofSeconds(3L))
68+
.setMaxRetryDelayDuration(Duration.ofSeconds(3L))
6969
.build();
7070

7171
static final QueryResultsOption[] DEFAULT_QUERY_WAIT_OPTIONS = {

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
package com.google.cloud.bigquery;
1818

19-
import static org.threeten.bp.temporal.ChronoField.HOUR_OF_DAY;
20-
import static org.threeten.bp.temporal.ChronoField.MINUTE_OF_HOUR;
21-
import static org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND;
22-
import static org.threeten.bp.temporal.ChronoField.SECOND_OF_MINUTE;
19+
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
20+
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
21+
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
22+
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
2323

24+
import com.google.api.core.ObsoleteApi;
2425
import com.google.api.services.bigquery.model.QueryParameterType;
2526
import com.google.api.services.bigquery.model.RangeValue;
2627
import com.google.auto.value.AutoValue;
@@ -33,17 +34,17 @@
3334
import com.google.gson.JsonObject;
3435
import java.io.Serializable;
3536
import java.math.BigDecimal;
37+
import java.time.Instant;
38+
import java.time.ZoneOffset;
39+
import java.time.format.DateTimeFormatter;
40+
import java.time.format.DateTimeFormatterBuilder;
41+
import java.time.format.DateTimeParseException;
3642
import java.util.ArrayList;
3743
import java.util.Date;
3844
import java.util.HashMap;
3945
import java.util.List;
4046
import java.util.Map;
4147
import javax.annotation.Nullable;
42-
import org.threeten.bp.Instant;
43-
import org.threeten.bp.ZoneOffset;
44-
import org.threeten.bp.format.DateTimeFormatter;
45-
import org.threeten.bp.format.DateTimeFormatterBuilder;
46-
import org.threeten.bp.format.DateTimeParseException;
4748
import org.threeten.extra.PeriodDuration;
4849

4950
/**
@@ -345,7 +346,11 @@ public static QueryParameterValue interval(String value) {
345346
return of(value, StandardSQLTypeName.INTERVAL);
346347
}
347348

348-
/** Creates a {@code QueryParameterValue} object with a type of INTERVAL. */
349+
/**
350+
* Creates a {@code QueryParameterValue} object with a type of INTERVAL. This method is obsolete.
351+
* Use {@link #interval(String)} instead.
352+
*/
353+
@ObsoleteApi("Use interval(String) instead")
349354
public static QueryParameterValue interval(PeriodDuration value) {
350355
return of(value, StandardSQLTypeName.INTERVAL);
351356
}

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
import com.google.cloud.http.HttpTransportOptions;
2525
import java.io.IOException;
2626
import java.io.InputStream;
27+
import java.time.Duration;
2728
import java.util.UUID;
2829
import java.util.logging.Level;
2930
import java.util.logging.Logger;
30-
import org.threeten.bp.Duration;
3131

3232
/**
3333
* Utility to create a remote BigQuery configuration for testing. BigQuery options can be obtained
@@ -144,13 +144,13 @@ private static RetrySettings retrySettings() {
144144
long totalTimeOut = 120000L;
145145
return RetrySettings.newBuilder()
146146
.setMaxAttempts(maxAttempts)
147-
.setMaxRetryDelay(Duration.ofMillis(maxRetryDelay))
148-
.setTotalTimeout(Duration.ofMillis(totalTimeOut))
149-
.setInitialRetryDelay(Duration.ofMillis(initialRetryDelay))
147+
.setMaxRetryDelayDuration(Duration.ofMillis(maxRetryDelay))
148+
.setTotalTimeoutDuration(Duration.ofMillis(totalTimeOut))
149+
.setInitialRetryDelayDuration(Duration.ofMillis(initialRetryDelay))
150150
.setRetryDelayMultiplier(retryDelayMultiplier)
151-
.setInitialRpcTimeout(Duration.ofMillis(totalTimeOut))
151+
.setInitialRpcTimeoutDuration(Duration.ofMillis(totalTimeOut))
152152
.setRpcTimeoutMultiplier(retryDelayMultiplier)
153-
.setMaxRpcTimeout(Duration.ofMillis(totalTimeOut))
153+
.setMaxRpcTimeoutDuration(Duration.ofMillis(totalTimeOut))
154154
.build();
155155
}
156156

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@
4141
import com.google.cloud.bigquery.JobStatistics.QueryStatistics;
4242
import com.google.cloud.bigquery.JobStatus.State;
4343
import com.google.common.collect.ImmutableList;
44+
import java.time.Duration;
4445
import org.junit.Assert;
4546
import org.junit.Before;
4647
import org.junit.Rule;
4748
import org.junit.Test;
4849
import org.junit.runner.RunWith;
4950
import org.mockito.junit.MockitoJUnitRunner;
5051
import org.mockito.junit.MockitoRule;
51-
import org.threeten.bp.Duration;
5252

5353
@RunWith(MockitoJUnitRunner.class)
5454
public class JobTest {
@@ -83,8 +83,8 @@ public class JobTest {
8383

8484
private static final RetryOption[] TEST_RETRY_OPTIONS =
8585
new RetryOption[] {
86-
RetryOption.totalTimeout(Duration.ofSeconds(3)),
87-
RetryOption.initialRetryDelay(Duration.ofMillis(1L)),
86+
RetryOption.totalTimeoutDuration(Duration.ofSeconds(3)),
87+
RetryOption.initialRetryDelayDuration(Duration.ofMillis(1L)),
8888
RetryOption.jittered(false),
8989
RetryOption.retryDelayMultiplier(1.0)
9090
};
@@ -402,7 +402,8 @@ public void testWaitForWithTimeout() throws InterruptedException {
402402
when(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).thenReturn(runningJob);
403403
when(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).thenReturn(runningJob);
404404
try {
405-
job.waitFor(concat(TEST_RETRY_OPTIONS, RetryOption.totalTimeout(Duration.ofMillis(3))));
405+
job.waitFor(
406+
concat(TEST_RETRY_OPTIONS, RetryOption.totalTimeoutDuration(Duration.ofMillis(3))));
406407
Assert.fail();
407408
} catch (BigQueryException expected) {
408409
Assert.assertNotNull(expected.getMessage());

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryParameterValueTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,28 @@
1717
package com.google.cloud.bigquery;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20-
import static org.threeten.bp.temporal.ChronoField.HOUR_OF_DAY;
21-
import static org.threeten.bp.temporal.ChronoField.MINUTE_OF_HOUR;
22-
import static org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND;
23-
import static org.threeten.bp.temporal.ChronoField.SECOND_OF_MINUTE;
20+
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
21+
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
22+
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
23+
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
2424

2525
import com.google.api.services.bigquery.model.QueryParameterType;
2626
import com.google.common.collect.ImmutableMap;
2727
import com.google.gson.JsonObject;
2828
import java.math.BigDecimal;
2929
import java.text.ParseException;
30+
import java.time.Instant;
3031
import java.time.Period;
32+
import java.time.ZoneOffset;
33+
import java.time.format.DateTimeFormatter;
34+
import java.time.format.DateTimeFormatterBuilder;
3135
import java.util.ArrayList;
3236
import java.util.Date;
3337
import java.util.HashMap;
3438
import java.util.List;
3539
import java.util.Map;
3640
import org.junit.Assert;
3741
import org.junit.Test;
38-
import org.threeten.bp.Instant;
39-
import org.threeten.bp.ZoneOffset;
40-
import org.threeten.bp.format.DateTimeFormatter;
41-
import org.threeten.bp.format.DateTimeFormatterBuilder;
42-
import org.threeten.bp.jdk8.Jdk8Methods;
4342
import org.threeten.extra.PeriodDuration;
4443

4544
public class QueryParameterValueTest {
@@ -338,8 +337,8 @@ public void testTimestampFromLong() {
338337
public void testTimestampWithFormatter() {
339338
long timestampInMicroseconds = 1571068536842L * 1000 + 123;
340339
long microseconds = 1_000_000;
341-
long secs = Jdk8Methods.floorDiv(timestampInMicroseconds, microseconds);
342-
int nano = (int) Jdk8Methods.floorMod(timestampInMicroseconds, microseconds) * 1000;
340+
long secs = Math.floorDiv(timestampInMicroseconds, microseconds);
341+
int nano = (int) Math.floorMod(timestampInMicroseconds, microseconds) * 1000;
343342
Instant instant = Instant.ofEpochSecond(secs, nano);
344343
String expected = TIMESTAMPFORMATTER.format(instant);
345344
assertThat(expected)

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
import java.sql.ResultSet;
170170
import java.sql.SQLException;
171171
import java.sql.Time;
172+
import java.time.Duration;
172173
import java.time.Instant;
173174
import java.time.LocalTime;
174175
import java.time.Period;
@@ -195,7 +196,6 @@
195196
import org.junit.Rule;
196197
import org.junit.Test;
197198
import org.junit.rules.Timeout;
198-
import org.threeten.bp.Duration;
199199
import org.threeten.extra.PeriodDuration;
200200

201201
public class ITBigQueryTest {
@@ -5383,7 +5383,7 @@ public void testCreateAndGetJob() throws InterruptedException, TimeoutException
53835383
assertEquals(createdJob.getSelfLink(), remoteJob.getSelfLink());
53845384
assertEquals(createdJob.getUserEmail(), remoteJob.getUserEmail());
53855385

5386-
Job completedJob = remoteJob.waitFor(RetryOption.totalTimeout(Duration.ofMinutes(1)));
5386+
Job completedJob = remoteJob.waitFor(RetryOption.totalTimeoutDuration(Duration.ofMinutes(1)));
53875387
assertNotNull(completedJob);
53885388
assertNull(completedJob.getStatus().getError());
53895389
assertTrue(createdTable.delete());
@@ -5451,8 +5451,8 @@ public void testCreateAndGetJobWithSelectedFields()
54515451
assertNull(remoteJob.getUserEmail());
54525452
Job completedJob =
54535453
remoteJob.waitFor(
5454-
RetryOption.initialRetryDelay(Duration.ofSeconds(1)),
5455-
RetryOption.totalTimeout(Duration.ofMinutes(1)));
5454+
RetryOption.initialRetryDelayDuration(Duration.ofSeconds(1)),
5455+
RetryOption.totalTimeoutDuration(Duration.ofMinutes(1)));
54565456
assertNotNull(completedJob);
54575457
assertTrue(createdTable.delete());
54585458
assertNull(completedJob.getStatus().getError());

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelperTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import com.google.cloud.http.HttpTransportOptions;
2626
import java.io.ByteArrayInputStream;
2727
import java.io.InputStream;
28+
import java.time.Duration;
2829
import java.util.concurrent.ExecutionException;
2930
import org.junit.Test;
3031
import org.junit.runner.RunWith;
3132
import org.mockito.Mockito;
3233
import org.mockito.junit.MockitoJUnitRunner;
33-
import org.threeten.bp.Duration;
3434

3535
@RunWith(MockitoJUnitRunner.class)
3636
public class RemoteBigQueryHelperTest {
@@ -83,8 +83,8 @@ public void testCreateFromStream() {
8383
assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getConnectTimeout());
8484
assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getReadTimeout());
8585
assertEquals(10, options.getRetrySettings().getMaxAttempts());
86-
assertEquals(Duration.ofMillis(30000), options.getRetrySettings().getMaxRetryDelay());
87-
assertEquals(Duration.ofMillis(120000), options.getRetrySettings().getTotalTimeout());
88-
assertEquals(Duration.ofMillis(250), options.getRetrySettings().getInitialRetryDelay());
86+
assertEquals(Duration.ofMillis(30000), options.getRetrySettings().getMaxRetryDelayDuration());
87+
assertEquals(Duration.ofMillis(120000), options.getRetrySettings().getTotalTimeoutDuration());
88+
assertEquals(Duration.ofMillis(250), options.getRetrySettings().getInitialRetryDelayDuration());
8989
}
9090
}

0 commit comments

Comments
 (0)