Repository: googleapis/java-datastore
Pull request #1671 (2024-11-19 18:48:46)

feat: introduce `java.time` methods and variables
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).


Patch:
diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/models/ExecutionStats.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/models/ExecutionStats.java
index 52184a01..6738d84e 100644
--- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/models/ExecutionStats.java
+++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/models/ExecutionStats.java
@@ -15,25 +15,27 @@
  */
 package com.google.cloud.datastore.models;
 
+import static com.google.api.gax.util.TimeConversionUtils.toThreetenDuration;
+
 import com.google.api.core.BetaApi;
 import com.google.api.core.InternalApi;
+import com.google.api.core.ObsoleteApi;
 import com.google.cloud.Structs;
 import com.google.common.base.Objects;
 import java.util.Map;
-import org.threeten.bp.Duration;
 
 /** Model class for {@link com.google.datastore.v1.ExecutionStats} */
 @BetaApi
 public class ExecutionStats {
   private final long resultsReturned;
-  private final Duration executionDuration;
+  private final java.time.Duration executionDuration;
   private final long readOperations;
   private final Map<String, Object> debugStats;
 
   @InternalApi
   public ExecutionStats(com.google.datastore.v1.ExecutionStats proto) {
     this.resultsReturned = proto.getResultsReturned();
-    this.executionDuration = Duration.ofNanos(proto.getExecutionDuration().getNanos());
+    this.executionDuration = java.time.Duration.ofNanos(proto.getExecutionDuration().getNanos());
     this.readOperations = proto.getReadOperations();
     this.debugStats = Structs.asMap(proto.getDebugStats());
   }
@@ -51,8 +53,14 @@ public class ExecutionStats {
     return debugStats;
   }
 
+  /** This method is obsolete. Use {@link #getExecutionDurationJavaTime()} instead. */
+  @ObsoleteApi("Use getExecutionDurationJavaTime() instead")
+  public org.threeten.bp.Duration getExecutionDuration() {
+    return toThreetenDuration(getExecutionDurationJavaTime());
+  }
+
   /** Returns the total time to execute the query in the backend. */
-  public Duration getExecutionDuration() {
+  public java.time.Duration getExecutionDurationJavaTime() {
     return executionDuration;
   }
 


Test patch:
diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java
index 2723325e..32bf9611 100644
--- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java
+++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java
@@ -16,9 +16,11 @@
 
 package com.google.cloud.datastore.testing;
 
+import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeDuration;
 import static com.google.common.base.MoreObjects.firstNonNull;
 
 import com.google.api.core.InternalApi;
+import com.google.api.core.ObsoleteApi;
 import com.google.cloud.NoCredentials;
 import com.google.cloud.ServiceOptions;
 import com.google.cloud.datastore.DatastoreOptions;
@@ -38,7 +40,6 @@ import java.util.List;
 import java.util.UUID;
 import java.util.concurrent.TimeoutException;
 import java.util.logging.Logger;
-import org.threeten.bp.Duration;
 
 /**
  * Utility to start and stop local Google Cloud Datastore emulators.
@@ -307,6 +308,14 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {
     sendPostRequest("/reset");
   }
 
+  /** This method is obsolete. Use {@link #stopDuration(java.time.Duration)} instead */
+  @ObsoleteApi("Use stopDuration(java.time.Duration) instead")
+  @Override
+  public void stop(org.threeten.bp.Duration timeout)
+      throws IOException, InterruptedException, TimeoutException {
+    stopDuration(toJavaTimeDuration(timeout));
+  }
+
   /**
    * Stops the Datastore emulator.
    *
@@ -319,15 +328,16 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {
    *     this value high to ensure proper shutdown, like 5 seconds or more.
    */
   @Override
-  public void stop(Duration timeout) throws IOException, InterruptedException, TimeoutException {
+  public void stopDuration(java.time.Duration timeout)
+      throws IOException, InterruptedException, TimeoutException {
     sendPostRequest("/shutdown");
-    waitForProcess(timeout);
+    waitForProcessDuration(timeout);
     deleteRecursively(gcdPath);
   }
 
   /**
-   * Stops the Datastore emulator. The same as {@link #stop(Duration)} but with timeout duration of
-   * 20 seconds.
+   * Stops the Datastore emulator. The same as {@link #stopDuration(java.time.Duration)} but with
+   * timeout duration of 20 seconds.
    *
    * <p>It is important to stop the emulator. Since the emulator runs in its own process, not
    * stopping it might cause it to become orphan.
@@ -335,7 +345,7 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {
    * <p>It is not required to call {@link #reset()} before {@code stop()}.
    */
   public void stop() throws IOException, InterruptedException, TimeoutException {
-    stop(Duration.ofSeconds(20));
+    stopDuration(java.time.Duration.ofSeconds(20));
   }
 
   static void deleteRecursively(Path path) throws IOException {
diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java
index d88e1d1e..459278f2 100644
--- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java
+++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java
@@ -68,6 +68,7 @@ import com.google.datastore.v1.RunQueryResponse;
 import com.google.datastore.v1.TransactionOptions;
 import com.google.protobuf.ByteString;
 import java.io.IOException;
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -87,7 +88,6 @@ import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
-import org.threeten.bp.Duration;
 
 @RunWith(JUnit4.class)
 public class DatastoreTest {
@@ -193,7 +193,7 @@ public class DatastoreTest {
 
   @AfterClass
   public static void afterClass() throws IOException, InterruptedException, TimeoutException {
-    helper.stop(Duration.ofMinutes(1));
+    helper.stopDuration(Duration.ofMinutes(1));
   }
 
   @Test
diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITDatastoreTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITDatastoreTest.java
index c012d28c..bf0c20dc 100644
--- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITDatastoreTest.java
+++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITDatastoreTest.java
@@ -81,6 +81,7 @@ import com.google.common.collect.Range;
 import com.google.common.truth.Truth;
 import com.google.datastore.v1.TransactionOptions;
 import com.google.datastore.v1.TransactionOptions.ReadOnly;
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -103,7 +104,6 @@ import org.junit.Test;
 import org.junit.rules.Timeout;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
-import org.threeten.bp.Duration;
 
 @RunWith(Parameterized.class)
 public class ITDatastoreTest {
@@ -674,7 +674,7 @@ public class ITDatastoreTest {
     Truth.assertThat(debugStats.get("index_entries_scanned"))
         .isEqualTo(expectedIndexEntriesScanned);
 
-    Duration executionDuration = executionStats.getExecutionDuration();
+    Duration executionDuration = executionStats.getExecutionDurationJavaTime();
     Truth.assertThat(executionDuration).isIn(Range.greaterThan(Duration.ofMillis(0)));
 
     long readOperations = executionStats.getReadOperations();
diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/models/ExecutionStatsTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/models/ExecutionStatsTest.java
index 4706e3b8..a0e778ce 100644
--- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/models/ExecutionStatsTest.java
+++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/models/ExecutionStatsTest.java
@@ -41,8 +41,8 @@ public class ExecutionStatsTest {
   @Test
   public void testModel() {
     Truth.assertThat(executionStats.getDebugStats()).isEqualTo(Structs.asMap(struct));
-    Truth.assertThat(executionStats.getExecutionDuration())
-        .isEqualTo(org.threeten.bp.Duration.ofNanos(duration.getNanos()));
+    Truth.assertThat(executionStats.getExecutionDurationJavaTime())
+        .isEqualTo(java.time.Duration.ofNanos(duration.getNanos()));
     Truth.assertThat(executionStats.getReadOperations()).isEqualTo(2);
     Truth.assertThat(executionStats.getResultsReturned()).isEqualTo(3);
   }
diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java
index 3ebd3ef4..dfa2255a 100644
--- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java
+++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java
@@ -32,6 +32,7 @@ import com.google.cloud.datastore.Key;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.time.Duration;
 import java.util.concurrent.TimeoutException;
 import org.junit.After;
 import org.junit.Assert;
@@ -39,7 +40,6 @@ import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
-import org.threeten.bp.Duration;
 
 @RunWith(JUnit4.class)
 public class ITLocalDatastoreHelperTest {
@@ -178,7 +178,7 @@ public class ITLocalDatastoreHelperTest {
       assertNotNull(datastore.get(key));
       helper.reset();
       assertNull(datastore.get(key));
-      helper.stop(Duration.ofMinutes(1));
+      helper.stopDuration(Duration.ofMinutes(1));
       datastore.get(key);
       Assert.fail();
     } catch (DatastoreException ex) {
@@ -198,7 +198,7 @@ public class ITLocalDatastoreHelperTest {
       assertNotNull(datastore.get(key));
       helper.reset();
       assertNull(datastore.get(key));
-      helper.stop(Duration.ofMinutes(1));
+      helper.stopDuration(Duration.ofMinutes(1));
       datastore.get(key);
       Assert.fail();
     } catch (DatastoreException ex) {
diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java
index 6167cedc..a4b389a1 100644
--- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java
+++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java
@@ -27,9 +27,9 @@ import com.google.cloud.datastore.QueryResults;
 import com.google.cloud.datastore.StructuredQuery;
 import com.google.cloud.http.HttpTransportOptions;
 import io.opentelemetry.sdk.OpenTelemetrySdk;
+import java.time.Duration;
 import java.util.UUID;
 import javax.annotation.Nullable;
-import org.threeten.bp.Duration;
 
 /**
  * Utility to create a remote datastore configuration for testing. Datastore options can be obtained
@@ -110,13 +110,13 @@ public class RemoteDatastoreHelper {
   private static RetrySettings retrySettings() {
     return RetrySettings.newBuilder()
         .setMaxAttempts(10)
-        .setMaxRetryDelay(Duration.ofMillis(30000L))
-        .setTotalTimeout(Duration.ofMillis(120000L))
-        .setInitialRetryDelay(Duration.ofMillis(250L))
+        .setMaxRetryDelayDuration(Duration.ofMillis(30000L))
+        .setTotalTimeoutDuration(Duration.ofMillis(120000L))
+        .setInitialRetryDelayDuration(Duration.ofMillis(250L))
         .setRetryDelayMultiplier(1.0)
-        .setInitialRpcTimeout(Duration.ofMillis(120000L))
+        .setInitialRpcTimeoutDuration(Duration.ofMillis(120000L))
         .setRpcTimeoutMultiplier(1.0)
-        .setMaxRpcTimeout(Duration.ofMillis(120000L))
+        .setMaxRpcTimeoutDuration(Duration.ofMillis(120000L))
         .build();
   }
 }
diff --git a/samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java b/samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java
index 045b16a9..33aa63ab 100644
--- a/samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java
+++ b/samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java
@@ -77,7 +77,6 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
-import org.threeten.bp.Duration;
 
 /** Contains Cloud Datastore snippets demonstrating concepts for documentation. */
 @RunWith(JUnit4.class)
@@ -147,7 +146,7 @@ public class ConceptsTest {
    */
   @AfterClass
   public static void afterClass() throws IOException, InterruptedException, TimeoutException {
-    HELPER.stop(Duration.ofMinutes(1));
+    HELPER.stopDuration(java.time.Duration.ofMinutes(1));
   }
 
   private void assertValidKey(Key taskKey) {