Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce java.time to java-core #3330

Merged
merged 22 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions java-core/google-cloud-core/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@
<className>com/google/cloud/ReadChannel</className>
<method>long limit()</method>
</difference>
<!-- These methods were switched to java.time and are internally used
in testing -->
<difference>
<differenceType>7005</differenceType>
<className>com/google/cloud/testing/*</className>
<method>* *(org.threeten.bp.Duration)</method>
<to>*</to>
</difference>
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
</differences>
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package com.google.cloud;

import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeDuration;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.core.BetaApi;
import com.google.api.core.ObsoleteApi;
import com.google.api.gax.retrying.RetrySettings;
import java.io.Serializable;
import org.threeten.bp.Duration;

/**
* This class represents an options wrapper around the {@link RetrySettings} class and is an
Expand Down Expand Up @@ -51,13 +52,25 @@ private RetryOption(OptionType type, Object value) {
this.value = checkNotNull(value);
}

/** See {@link RetrySettings#getTotalTimeout()}. */
public static RetryOption totalTimeout(Duration totalTimeout) {
/** This method is obsolete. Use {@link #totalTimeoutDuration(java.time.Duration)} instead */
@ObsoleteApi("Use totalTimeouDuration() instead")
public static RetryOption totalTimeout(org.threeten.bp.Duration totalTimeout) {
return totalTimeoutDuration(toJavaTimeDuration(totalTimeout));
}

/** See {@link RetrySettings#getTotalTimeoutDuration()}. */
public static RetryOption totalTimeoutDuration(java.time.Duration totalTimeout) {
return new RetryOption(OptionType.TOTAL_TIMEOUT, totalTimeout);
}

/** See {@link RetrySettings#getInitialRetryDelay()}. */
public static RetryOption initialRetryDelay(Duration initialRetryDelay) {
/** This method is obsolete. Use {@link #initialRetryDelayDuration(java.time.Duration)} instead */
@ObsoleteApi("Use initialRetryDelayDuration() instead")
public static RetryOption initialRetryDelay(org.threeten.bp.Duration initialRetryDelay) {
return initialRetryDelayDuration(toJavaTimeDuration(initialRetryDelay));
}

/** See {@link RetrySettings#getInitialRetryDelayDuration()}. */
public static RetryOption initialRetryDelayDuration(java.time.Duration initialRetryDelay) {
return new RetryOption(OptionType.INITIAL_RETRY_DELAY, initialRetryDelay);
}

Expand All @@ -66,8 +79,14 @@ public static RetryOption retryDelayMultiplier(double retryDelayMultiplier) {
return new RetryOption(OptionType.RETRY_DELAY_MULTIPLIER, retryDelayMultiplier);
}

/** See {@link RetrySettings#getMaxRetryDelay()}. */
public static RetryOption maxRetryDelay(Duration maxRetryDelay) {
/** This method is obsolete. Use {@link #maxRetryDelayDuration(java.time.Duration)} instead */
@ObsoleteApi("Use maxRetryDelayDuration() instead")
public static RetryOption maxRetryDelay(org.threeten.bp.Duration maxRetryDelay) {
return maxRetryDelayDuration(toJavaTimeDuration(maxRetryDelay));
}

/** See {@link RetrySettings#getMaxRetryDelayDuration()}. */
public static RetryOption maxRetryDelayDuration(java.time.Duration maxRetryDelay) {
return new RetryOption(OptionType.MAX_RETRY_DELAY, maxRetryDelay);
}

Expand Down Expand Up @@ -124,16 +143,16 @@ public static RetrySettings mergeToSettings(RetrySettings settings, RetryOption.
for (RetryOption option : options) {
switch (option.type) {
case TOTAL_TIMEOUT:
builder.setTotalTimeout((Duration) option.value);
builder.setTotalTimeoutDuration((java.time.Duration) option.value);
break;
case INITIAL_RETRY_DELAY:
builder.setInitialRetryDelay((Duration) option.value);
builder.setInitialRetryDelayDuration((java.time.Duration) option.value);
break;
case RETRY_DELAY_MULTIPLIER:
builder.setRetryDelayMultiplier((Double) option.value);
break;
case MAX_RETRY_DELAY:
builder.setMaxRetryDelay((Duration) option.value);
builder.setMaxRetryDelayDuration((java.time.Duration) option.value);
break;
case MAX_ATTEMPTS:
builder.setMaxAttempts((Integer) option.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@
import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.threeten.bp.Duration;

/**
* Abstract class representing service options.
Expand Down Expand Up @@ -787,13 +787,13 @@ public static RetrySettings getNoRetrySettings() {
private static RetrySettings.Builder getDefaultRetrySettingsBuilder() {
return RetrySettings.newBuilder()
.setMaxAttempts(6)
.setInitialRetryDelay(Duration.ofMillis(1000L))
.setMaxRetryDelay(Duration.ofMillis(32_000L))
.setInitialRetryDelayDuration(Duration.ofMillis(1000L))
.setMaxRetryDelayDuration(Duration.ofMillis(32_000L))
.setRetryDelayMultiplier(2.0)
.setTotalTimeout(Duration.ofMillis(50_000L))
.setInitialRpcTimeout(Duration.ofMillis(50_000L))
.setTotalTimeoutDuration(Duration.ofMillis(50_000L))
.setInitialRpcTimeoutDuration(Duration.ofMillis(50_000L))
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ofMillis(50_000L));
.setMaxRpcTimeoutDuration(Duration.ofMillis(50_000L));
}

protected abstract Set<String> getScopes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@

import com.google.protobuf.util.Timestamps;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.threeten.bp.Instant;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.ZoneOffset;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.format.DateTimeFormatterBuilder;
import org.threeten.bp.format.DateTimeParseException;
import org.threeten.bp.temporal.TemporalAccessor;

/**
* Represents a timestamp with nanosecond precision. Timestamps cover the range [0001-01-01,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
Expand All @@ -56,7 +57,6 @@
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.threeten.bp.Duration;

/** Utility class to start and stop a local service which is used by unit testing. */
@InternalApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,30 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import com.google.api.gax.retrying.RetrySettings;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.threeten.bp.Duration;

class RetryOptionTest {
private static final long TOTAL_TIMEOUT_MILLIS = 420l;
private static final long INITIAL_RETRY_DELAY_MILLIS = 42l;
private static final long MAX_RETRY_DELAY_MILLIS = 100l;

private static final RetryOption TOTAL_TIMEOUT =
RetryOption.totalTimeout(Duration.ofMillis(420L));
RetryOption.totalTimeoutDuration(Duration.ofMillis(TOTAL_TIMEOUT_MILLIS));
private static final RetryOption INITIAL_RETRY_DELAY =
RetryOption.initialRetryDelay(Duration.ofMillis(42L));
RetryOption.initialRetryDelayDuration(Duration.ofMillis(INITIAL_RETRY_DELAY_MILLIS));
private static final RetryOption RETRY_DELAY_MULTIPLIER = RetryOption.retryDelayMultiplier(1.5);
private static final RetryOption MAX_RETRY_DELAY =
RetryOption.maxRetryDelay(Duration.ofMillis(100));
RetryOption.maxRetryDelayDuration(Duration.ofMillis(MAX_RETRY_DELAY_MILLIS));
private static final RetryOption MAX_ATTEMPTS = RetryOption.maxAttempts(100);
private static final RetryOption JITTERED = RetryOption.jittered(false);

private static final RetrySettings retrySettings =
RetrySettings.newBuilder()
.setTotalTimeout(Duration.ofMillis(420L))
.setInitialRetryDelay(Duration.ofMillis(42L))
.setTotalTimeoutDuration(Duration.ofMillis(420L))
.setInitialRetryDelayDuration(Duration.ofMillis(42L))
.setRetryDelayMultiplier(1.5)
.setMaxRetryDelay(Duration.ofMillis(100))
.setMaxRetryDelayDuration(Duration.ofMillis(100))
.setMaxAttempts(100)
.setJittered(false)
.build();
Expand All @@ -61,10 +64,10 @@ void testEqualsAndHashCode() {
assertNotEquals(MAX_ATTEMPTS, MAX_RETRY_DELAY);
assertNotEquals(JITTERED, MAX_ATTEMPTS);

RetryOption totalTimeout = RetryOption.totalTimeout(Duration.ofMillis(420L));
RetryOption initialRetryDelay = RetryOption.initialRetryDelay(Duration.ofMillis(42L));
RetryOption totalTimeout = RetryOption.totalTimeoutDuration(Duration.ofMillis(420L));
RetryOption initialRetryDelay = RetryOption.initialRetryDelayDuration(Duration.ofMillis(42L));
RetryOption retryDelayMultiplier = RetryOption.retryDelayMultiplier(1.5);
RetryOption maxRetryDelay = RetryOption.maxRetryDelay(Duration.ofMillis(100));
RetryOption maxRetryDelay = RetryOption.maxRetryDelayDuration(Duration.ofMillis(100));
RetryOption maxAttempts = RetryOption.maxAttempts(100);
RetryOption jittered = RetryOption.jittered(false);

Expand Down Expand Up @@ -101,17 +104,17 @@ void testMergeToSettings() {
assertEquals(retrySettings, mergedRetrySettings);

defRetrySettings =
defRetrySettings.toBuilder().setTotalTimeout(Duration.ofMillis(420L)).build();
defRetrySettings.toBuilder().setTotalTimeoutDuration(Duration.ofMillis(420L)).build();
mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, TOTAL_TIMEOUT);
assertEquals(defRetrySettings, mergedRetrySettings);

defRetrySettings =
defRetrySettings.toBuilder().setMaxRetryDelay(Duration.ofMillis(100)).build();
defRetrySettings.toBuilder().setMaxRetryDelayDuration(Duration.ofMillis(100)).build();
mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, MAX_RETRY_DELAY);
assertEquals(defRetrySettings, mergedRetrySettings);

defRetrySettings =
defRetrySettings.toBuilder().setInitialRetryDelay(Duration.ofMillis(42L)).build();
defRetrySettings.toBuilder().setInitialRetryDelayDuration(Duration.ofMillis(42L)).build();
mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, INITIAL_RETRY_DELAY);
assertEquals(defRetrySettings, mergedRetrySettings);

Expand All @@ -127,4 +130,20 @@ void testMergeToSettings() {
mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, JITTERED);
assertEquals(defRetrySettings, mergedRetrySettings);
}

@Test
public void threetenMethods_producesEquivalentJavaTimeRetryOptions() {

final RetryOption totalTimeoutThreeten =
RetryOption.totalTimeout(org.threeten.bp.Duration.ofMillis(TOTAL_TIMEOUT_MILLIS));
final RetryOption initialRetryDelayThreeten =
RetryOption.initialRetryDelay(
org.threeten.bp.Duration.ofMillis(INITIAL_RETRY_DELAY_MILLIS));
final RetryOption maxRetryDelayThreeten =
RetryOption.maxRetryDelay(org.threeten.bp.Duration.ofMillis(MAX_RETRY_DELAY_MILLIS));

assertEquals(TOTAL_TIMEOUT, totalTimeoutThreeten);
assertEquals(INITIAL_RETRY_DELAY, initialRetryDelayThreeten);
assertEquals(MAX_RETRY_DELAY, maxRetryDelayThreeten);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.Serializable;
import org.threeten.bp.Duration;
import java.time.Duration;

public class SerializationTest extends BaseSerializationTest {

Expand All @@ -37,7 +37,7 @@ public class SerializationTest extends BaseSerializationTest {
private static final Role SOME_ROLE = Role.viewer();
private static final Policy SOME_IAM_POLICY = Policy.newBuilder().build();
private static final RetryOption CHECKING_PERIOD =
RetryOption.initialRetryDelay(Duration.ofSeconds(42));
RetryOption.initialRetryDelayDuration(Duration.ofSeconds(42));
private static final LabelDescriptor LABEL_DESCRIPTOR =
new LabelDescriptor("project_id", ValueType.STRING, "The project id");
private static final MonitoredResourceDescriptor MONITORED_RESOURCE_DESCRIPTOR =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.time.Duration;
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;
import org.easymock.EasyMock;
import org.junit.jupiter.api.Test;
import org.threeten.bp.Duration;

class BaseEmulatorHelperTest {

Expand Down
Loading