Skip to content

Commit 5a78a80

Browse files
feat: introduce java.time methods and variables (#1671)
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 593c50c commit 5a78a80

File tree

8 files changed

+44
-27
lines changed

8 files changed

+44
-27
lines changed

google-cloud-datastore/src/main/java/com/google/cloud/datastore/models/ExecutionStats.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@
1515
*/
1616
package com.google.cloud.datastore.models;
1717

18+
import static com.google.api.gax.util.TimeConversionUtils.toThreetenDuration;
19+
1820
import com.google.api.core.BetaApi;
1921
import com.google.api.core.InternalApi;
22+
import com.google.api.core.ObsoleteApi;
2023
import com.google.cloud.Structs;
2124
import com.google.common.base.Objects;
2225
import java.util.Map;
23-
import org.threeten.bp.Duration;
2426

2527
/** Model class for {@link com.google.datastore.v1.ExecutionStats} */
2628
@BetaApi
2729
public class ExecutionStats {
2830
private final long resultsReturned;
29-
private final Duration executionDuration;
31+
private final java.time.Duration executionDuration;
3032
private final long readOperations;
3133
private final Map<String, Object> debugStats;
3234

3335
@InternalApi
3436
public ExecutionStats(com.google.datastore.v1.ExecutionStats proto) {
3537
this.resultsReturned = proto.getResultsReturned();
36-
this.executionDuration = Duration.ofNanos(proto.getExecutionDuration().getNanos());
38+
this.executionDuration = java.time.Duration.ofNanos(proto.getExecutionDuration().getNanos());
3739
this.readOperations = proto.getReadOperations();
3840
this.debugStats = Structs.asMap(proto.getDebugStats());
3941
}
@@ -51,8 +53,14 @@ public Map<String, Object> getDebugStats() {
5153
return debugStats;
5254
}
5355

56+
/** This method is obsolete. Use {@link #getExecutionDurationJavaTime()} instead. */
57+
@ObsoleteApi("Use getExecutionDurationJavaTime() instead")
58+
public org.threeten.bp.Duration getExecutionDuration() {
59+
return toThreetenDuration(getExecutionDurationJavaTime());
60+
}
61+
5462
/** Returns the total time to execute the query in the backend. */
55-
public Duration getExecutionDuration() {
63+
public java.time.Duration getExecutionDurationJavaTime() {
5664
return executionDuration;
5765
}
5866

google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package com.google.cloud.datastore.testing;
1818

19+
import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeDuration;
1920
import static com.google.common.base.MoreObjects.firstNonNull;
2021

2122
import com.google.api.core.InternalApi;
23+
import com.google.api.core.ObsoleteApi;
2224
import com.google.cloud.NoCredentials;
2325
import com.google.cloud.ServiceOptions;
2426
import com.google.cloud.datastore.DatastoreOptions;
@@ -38,7 +40,6 @@
3840
import java.util.UUID;
3941
import java.util.concurrent.TimeoutException;
4042
import java.util.logging.Logger;
41-
import org.threeten.bp.Duration;
4243

4344
/**
4445
* Utility to start and stop local Google Cloud Datastore emulators.
@@ -307,6 +308,14 @@ public void reset() throws IOException {
307308
sendPostRequest("/reset");
308309
}
309310

311+
/** This method is obsolete. Use {@link #stopDuration(java.time.Duration)} instead */
312+
@ObsoleteApi("Use stopDuration(java.time.Duration) instead")
313+
@Override
314+
public void stop(org.threeten.bp.Duration timeout)
315+
throws IOException, InterruptedException, TimeoutException {
316+
stopDuration(toJavaTimeDuration(timeout));
317+
}
318+
310319
/**
311320
* Stops the Datastore emulator.
312321
*
@@ -319,23 +328,24 @@ public void reset() throws IOException {
319328
* this value high to ensure proper shutdown, like 5 seconds or more.
320329
*/
321330
@Override
322-
public void stop(Duration timeout) throws IOException, InterruptedException, TimeoutException {
331+
public void stopDuration(java.time.Duration timeout)
332+
throws IOException, InterruptedException, TimeoutException {
323333
sendPostRequest("/shutdown");
324-
waitForProcess(timeout);
334+
waitForProcessDuration(timeout);
325335
deleteRecursively(gcdPath);
326336
}
327337

328338
/**
329-
* Stops the Datastore emulator. The same as {@link #stop(Duration)} but with timeout duration of
330-
* 20 seconds.
339+
* Stops the Datastore emulator. The same as {@link #stopDuration(java.time.Duration)} but with
340+
* timeout duration of 20 seconds.
331341
*
332342
* <p>It is important to stop the emulator. Since the emulator runs in its own process, not
333343
* stopping it might cause it to become orphan.
334344
*
335345
* <p>It is not required to call {@link #reset()} before {@code stop()}.
336346
*/
337347
public void stop() throws IOException, InterruptedException, TimeoutException {
338-
stop(Duration.ofSeconds(20));
348+
stopDuration(java.time.Duration.ofSeconds(20));
339349
}
340350

341351
static void deleteRecursively(Path path) throws IOException {

google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import com.google.datastore.v1.TransactionOptions;
6969
import com.google.protobuf.ByteString;
7070
import java.io.IOException;
71+
import java.time.Duration;
7172
import java.util.ArrayList;
7273
import java.util.Arrays;
7374
import java.util.Collections;
@@ -87,7 +88,6 @@
8788
import org.junit.Test;
8889
import org.junit.runner.RunWith;
8990
import org.junit.runners.JUnit4;
90-
import org.threeten.bp.Duration;
9191

9292
@RunWith(JUnit4.class)
9393
public class DatastoreTest {
@@ -193,7 +193,7 @@ public void setUp() {
193193

194194
@AfterClass
195195
public static void afterClass() throws IOException, InterruptedException, TimeoutException {
196-
helper.stop(Duration.ofMinutes(1));
196+
helper.stopDuration(Duration.ofMinutes(1));
197197
}
198198

199199
@Test

google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITDatastoreTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import com.google.common.truth.Truth;
8282
import com.google.datastore.v1.TransactionOptions;
8383
import com.google.datastore.v1.TransactionOptions.ReadOnly;
84+
import java.time.Duration;
8485
import java.util.ArrayList;
8586
import java.util.Arrays;
8687
import java.util.Collections;
@@ -103,7 +104,6 @@
103104
import org.junit.rules.Timeout;
104105
import org.junit.runner.RunWith;
105106
import org.junit.runners.Parameterized;
106-
import org.threeten.bp.Duration;
107107

108108
@RunWith(Parameterized.class)
109109
public class ITDatastoreTest {
@@ -674,7 +674,7 @@ private void assertExecutionStats(
674674
Truth.assertThat(debugStats.get("index_entries_scanned"))
675675
.isEqualTo(expectedIndexEntriesScanned);
676676

677-
Duration executionDuration = executionStats.getExecutionDuration();
677+
Duration executionDuration = executionStats.getExecutionDurationJavaTime();
678678
Truth.assertThat(executionDuration).isIn(Range.greaterThan(Duration.ofMillis(0)));
679679

680680
long readOperations = executionStats.getReadOperations();

google-cloud-datastore/src/test/java/com/google/cloud/datastore/models/ExecutionStatsTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public class ExecutionStatsTest {
4141
@Test
4242
public void testModel() {
4343
Truth.assertThat(executionStats.getDebugStats()).isEqualTo(Structs.asMap(struct));
44-
Truth.assertThat(executionStats.getExecutionDuration())
45-
.isEqualTo(org.threeten.bp.Duration.ofNanos(duration.getNanos()));
44+
Truth.assertThat(executionStats.getExecutionDurationJavaTime())
45+
.isEqualTo(java.time.Duration.ofNanos(duration.getNanos()));
4646
Truth.assertThat(executionStats.getReadOperations()).isEqualTo(2);
4747
Truth.assertThat(executionStats.getResultsReturned()).isEqualTo(3);
4848
}

google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
import java.io.IOException;
3333
import java.nio.file.Files;
3434
import java.nio.file.Path;
35+
import java.time.Duration;
3536
import java.util.concurrent.TimeoutException;
3637
import org.junit.After;
3738
import org.junit.Assert;
3839
import org.junit.Before;
3940
import org.junit.Test;
4041
import org.junit.runner.RunWith;
4142
import org.junit.runners.JUnit4;
42-
import org.threeten.bp.Duration;
4343

4444
@RunWith(JUnit4.class)
4545
public class ITLocalDatastoreHelperTest {
@@ -178,7 +178,7 @@ public void testStartStopReset() throws IOException, InterruptedException, Timeo
178178
assertNotNull(datastore.get(key));
179179
helper.reset();
180180
assertNull(datastore.get(key));
181-
helper.stop(Duration.ofMinutes(1));
181+
helper.stopDuration(Duration.ofMinutes(1));
182182
datastore.get(key);
183183
Assert.fail();
184184
} catch (DatastoreException ex) {
@@ -198,7 +198,7 @@ public void testStartStopResetWithBuilder()
198198
assertNotNull(datastore.get(key));
199199
helper.reset();
200200
assertNull(datastore.get(key));
201-
helper.stop(Duration.ofMinutes(1));
201+
helper.stopDuration(Duration.ofMinutes(1));
202202
datastore.get(key);
203203
Assert.fail();
204204
} catch (DatastoreException ex) {

google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import com.google.cloud.datastore.StructuredQuery;
2828
import com.google.cloud.http.HttpTransportOptions;
2929
import io.opentelemetry.sdk.OpenTelemetrySdk;
30+
import java.time.Duration;
3031
import java.util.UUID;
3132
import javax.annotation.Nullable;
32-
import org.threeten.bp.Duration;
3333

3434
/**
3535
* Utility to create a remote datastore configuration for testing. Datastore options can be obtained
@@ -110,13 +110,13 @@ public static RemoteDatastoreHelper create(String databaseId) {
110110
private static RetrySettings retrySettings() {
111111
return RetrySettings.newBuilder()
112112
.setMaxAttempts(10)
113-
.setMaxRetryDelay(Duration.ofMillis(30000L))
114-
.setTotalTimeout(Duration.ofMillis(120000L))
115-
.setInitialRetryDelay(Duration.ofMillis(250L))
113+
.setMaxRetryDelayDuration(Duration.ofMillis(30000L))
114+
.setTotalTimeoutDuration(Duration.ofMillis(120000L))
115+
.setInitialRetryDelayDuration(Duration.ofMillis(250L))
116116
.setRetryDelayMultiplier(1.0)
117-
.setInitialRpcTimeout(Duration.ofMillis(120000L))
117+
.setInitialRpcTimeoutDuration(Duration.ofMillis(120000L))
118118
.setRpcTimeoutMultiplier(1.0)
119-
.setMaxRpcTimeout(Duration.ofMillis(120000L))
119+
.setMaxRpcTimeoutDuration(Duration.ofMillis(120000L))
120120
.build();
121121
}
122122
}

samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
import org.junit.rules.ExpectedException;
7878
import org.junit.runner.RunWith;
7979
import org.junit.runners.JUnit4;
80-
import org.threeten.bp.Duration;
8180

8281
/** Contains Cloud Datastore snippets demonstrating concepts for documentation. */
8382
@RunWith(JUnit4.class)
@@ -147,7 +146,7 @@ public void tearDown() throws Exception {
147146
*/
148147
@AfterClass
149148
public static void afterClass() throws IOException, InterruptedException, TimeoutException {
150-
HELPER.stop(Duration.ofMinutes(1));
149+
HELPER.stopDuration(java.time.Duration.ofMinutes(1));
151150
}
152151

153152
private void assertValidKey(Key taskKey) {

0 commit comments

Comments
 (0)