Skip to content

Commit 194d239

Browse files
committed
Rename TimeSource to Clock and move it to ServiceOptions
1 parent 64121d8 commit 194d239

File tree

4 files changed

+67
-63
lines changed

4 files changed

+67
-63
lines changed

gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public abstract class ServiceOptions<
6262
private final ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory;
6363
private final int connectTimeout;
6464
private final int readTimeout;
65+
private final Clock clock;
6566

6667
public interface HttpTransportFactory extends Serializable {
6768
HttpTransport create();
@@ -91,7 +92,40 @@ public HttpTransport create() {
9192
}
9293
}
9394

95+
/**
96+
* A class providing access to the current time in milliseconds. This class is mainly used for
97+
* testing and will be replaced by Java8's {@code java.time.Clock}.
98+
*
99+
* Implementations should implement {@code Serializable} wherever possible and must document
100+
* whether or not they do support serialization.
101+
*/
102+
public static abstract class Clock {
103+
104+
private static ServiceOptions.Clock DEFAULT_TIME_SOURCE = new DefaultClock();
105+
106+
/**
107+
* Returns current time in milliseconds according to this clock.
108+
*/
109+
public abstract long millis();
110+
111+
/**
112+
* Returns the default clock. Default clock uses {@link System#currentTimeMillis()} to get time
113+
* in milliseconds.
114+
*/
115+
public static ServiceOptions.Clock defaultClock() {
116+
return DEFAULT_TIME_SOURCE;
117+
}
118+
119+
private static class DefaultClock extends ServiceOptions.Clock implements Serializable {
94120

121+
private static final long serialVersionUID = -5077300394286703864L;
122+
123+
@Override
124+
public long millis() {
125+
return System.currentTimeMillis();
126+
}
127+
}
128+
}
95129

96130
protected abstract static class Builder<
97131
ServiceRpcT,
@@ -106,6 +140,7 @@ protected abstract static class Builder<
106140
private ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory;
107141
private int connectTimeout = -1;
108142
private int readTimeout = -1;
143+
private Clock clock;
109144

110145
protected Builder() {}
111146

@@ -125,6 +160,18 @@ protected B self() {
125160
return (B) this;
126161
}
127162

163+
/**
164+
* Sets the service's clock. The clock is mainly used for testing purpose. {@link Clock} will be
165+
* replaced by Java8's {@code java.time.Clock}.
166+
*
167+
* @param clock the clock to set
168+
* @return the builder.
169+
*/
170+
public B clock(Clock clock) {
171+
this.clock = clock;
172+
return self();
173+
}
174+
128175
/**
129176
* Sets project id.
130177
*
@@ -221,6 +268,7 @@ protected ServiceOptions(Builder<ServiceRpcT, OptionsT, ?> builder) {
221268
serviceRpcFactory = builder.serviceRpcFactory;
222269
connectTimeout = builder.connectTimeout;
223270
readTimeout = builder.readTimeout;
271+
clock = firstNonNull(builder.clock, Clock.defaultClock());
224272
}
225273

226274
private static AuthCredentials defaultAuthCredentials() {
@@ -419,9 +467,17 @@ public int readTimeout() {
419467
return readTimeout;
420468
}
421469

470+
/**
471+
* Returns the service's clock. Default time source uses {@link System#currentTimeMillis()} to
472+
* get current time.
473+
*/
474+
public Clock clock() {
475+
return clock;
476+
}
477+
422478
protected int baseHashCode() {
423479
return Objects.hash(projectId, host, httpTransportFactory, authCredentials, retryParams,
424-
serviceRpcFactory);
480+
serviceRpcFactory, connectTimeout, readTimeout, clock);
425481
}
426482

427483
protected boolean baseEquals(ServiceOptions<?, ?> other) {
@@ -430,7 +486,10 @@ protected boolean baseEquals(ServiceOptions<?, ?> other) {
430486
&& Objects.equals(httpTransportFactory, other.httpTransportFactory)
431487
&& Objects.equals(authCredentials, other.authCredentials)
432488
&& Objects.equals(retryParams, other.retryParams)
433-
&& Objects.equals(serviceRpcFactory, other.serviceRpcFactory);
489+
&& Objects.equals(serviceRpcFactory, other.serviceRpcFactory)
490+
&& Objects.equals(connectTimeout, other.connectTimeout)
491+
&& Objects.equals(readTimeout, other.readTimeout)
492+
&& Objects.equals(clock, clock);
434493
}
435494

436495
public abstract Builder<ServiceRpcT, OptionsT, ?> toBuilder();

gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ public BlobWriteChannel writer(BlobInfo blobInfo, BlobTargetOption... options) {
524524
@Override
525525
public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOption... options) {
526526
long expiration = TimeUnit.SECONDS.convert(
527-
options().timeSource().millis() + unit.toMillis(duration), TimeUnit.MILLISECONDS);
527+
options().clock().millis() + unit.toMillis(duration), TimeUnit.MILLISECONDS);
528528
EnumMap<SignUrlOption.Option, Object> optionMap = Maps.newEnumMap(SignUrlOption.Option.class);
529529
for (SignUrlOption option : options) {
530530
optionMap.put(option.option(), option.value());

gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.gcloud.spi.StorageRpc;
2424
import com.google.gcloud.spi.StorageRpcFactory;
2525

26-
import java.io.Serializable;
2726
import java.util.Objects;
2827
import java.util.Set;
2928

@@ -35,47 +34,12 @@ public class StorageOptions extends ServiceOptions<StorageRpc, StorageOptions> {
3534
private static final String DEFAULT_PATH_DELIMITER = "/";
3635

3736
private final String pathDelimiter;
38-
private final TimeSource timeSource;
3937
private transient StorageRpc storageRpc;
4038

41-
/**
42-
* A class providing access to the current time in milliseconds.
43-
*
44-
* Implementations should implement {@code Serializable} wherever possible and must document
45-
* whether or not they do support serialization.
46-
*/
47-
public static abstract class TimeSource {
48-
49-
private static TimeSource DEFAULT_TIME_SOURCE = new DefaultTimeSource();
50-
51-
/**
52-
* Returns current time in milliseconds according to this time source.
53-
*/
54-
public abstract long millis();
55-
56-
/**
57-
* Returns the default time source.
58-
*/
59-
public static TimeSource defaultTimeSource() {
60-
return DEFAULT_TIME_SOURCE;
61-
}
62-
63-
private static class DefaultTimeSource extends TimeSource implements Serializable {
64-
65-
private static final long serialVersionUID = -5077300394286703864L;
66-
67-
@Override
68-
public long millis() {
69-
return System.currentTimeMillis();
70-
}
71-
}
72-
}
73-
7439
public static class Builder extends
7540
ServiceOptions.Builder<StorageRpc, StorageOptions, Builder> {
7641

7742
private String pathDelimiter;
78-
private TimeSource timeSource;
7943

8044
private Builder() {}
8145

@@ -85,6 +49,7 @@ private Builder(StorageOptions options) {
8549

8650
/**
8751
* Sets the path delimiter for the storage service.
52+
*
8853
* @param pathDelimiter the path delimiter to set
8954
* @return the builder.
9055
*/
@@ -93,17 +58,6 @@ public Builder pathDelimiter(String pathDelimiter) {
9358
return this;
9459
}
9560

96-
/**
97-
* Sets the time source for the storage service. The time source is used by `signUrl` to compute
98-
* URL's expiry time. If no time source is set by default `System.getTimeMillis()` is used.
99-
* @param source the time source to set
100-
* @return the builder.
101-
*/
102-
public Builder timeSource(TimeSource source) {
103-
this.timeSource = source;
104-
return this;
105-
}
106-
10761
@Override
10862
public StorageOptions build() {
10963
return new StorageOptions(this);
@@ -113,8 +67,6 @@ public StorageOptions build() {
11367
private StorageOptions(Builder builder) {
11468
super(builder);
11569
pathDelimiter = MoreObjects.firstNonNull(builder.pathDelimiter, DEFAULT_PATH_DELIMITER);
116-
timeSource = MoreObjects.firstNonNull(builder.timeSource, TimeSource.defaultTimeSource());
117-
// todo: consider providing read-timeout
11870
}
11971

12072
@Override
@@ -144,14 +96,6 @@ public String pathDelimiter() {
14496
return pathDelimiter;
14597
}
14698

147-
/**
148-
* Returns the storage service's time source. Default time source uses `System.getTimeMillis()` to
149-
* get current time.
150-
*/
151-
public TimeSource timeSource() {
152-
return timeSource;
153-
}
154-
15599
@Override
156100
public Builder toBuilder() {
157101
return new Builder(this);

gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.common.io.BaseEncoding;
3434
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
3535
import com.google.gcloud.RetryParams;
36+
import com.google.gcloud.ServiceOptions;
3637
import com.google.gcloud.spi.StorageRpc;
3738
import com.google.gcloud.spi.StorageRpc.Tuple;
3839

@@ -172,7 +173,7 @@ public class StorageImplTest {
172173
+ "EkPPhszldvQTY486uPxyD/D7HdfnGW/Nbw5JUhfvecAdudDEhNAQ3PNabyDMI+TpiHy4NTWOrgdcWrzj6VXcdc"
173174
+ "+uuABnPwRCdcyJ1xl2kOrPksRnp1auNGMLOe4IpEBjGY7baX9UG8+A45MbG0aHmkR59Op/aR9XowIDAQAB";
174175

175-
private static final StorageOptions.TimeSource TIME_SOURCE = new StorageOptions.TimeSource() {
176+
private static final ServiceOptions.Clock TIME_SOURCE = new ServiceOptions.Clock() {
176177
@Override
177178
public long millis() {
178179
return 42000L;
@@ -804,7 +805,7 @@ public void testSignUrl() throws NoSuchAlgorithmException, InvalidKeyException,
804805
EasyMock.createMock(ServiceAccountAuthCredentials.class);
805806
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
806807
EasyMock.expect(optionsMock.authCredentials()).andReturn(credentialsMock).times(2);
807-
EasyMock.expect(optionsMock.timeSource()).andReturn(TIME_SOURCE);
808+
EasyMock.expect(optionsMock.clock()).andReturn(TIME_SOURCE);
808809
EasyMock.expect(credentialsMock.privateKey()).andReturn(privateKey);
809810
EasyMock.expect(credentialsMock.account()).andReturn(account);
810811
EasyMock.replay(optionsMock, storageRpcMock, credentialsMock);
@@ -839,7 +840,7 @@ public void testSignUrlWithOptions() throws NoSuchAlgorithmException, InvalidKey
839840
EasyMock.createMock(ServiceAccountAuthCredentials.class);
840841
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
841842
EasyMock.expect(optionsMock.authCredentials()).andReturn(credentialsMock).times(2);
842-
EasyMock.expect(optionsMock.timeSource()).andReturn(TIME_SOURCE);
843+
EasyMock.expect(optionsMock.clock()).andReturn(TIME_SOURCE);
843844
EasyMock.expect(credentialsMock.privateKey()).andReturn(privateKey);
844845
EasyMock.expect(credentialsMock.account()).andReturn(account);
845846
EasyMock.replay(optionsMock, storageRpcMock, credentialsMock);

0 commit comments

Comments
 (0)