Skip to content

Commit 9d48528

Browse files
committed
Refactor Compute immutable resources' identities and classes
- Use Pattern and Matcher to parse URLs into identities - Make REGEX private in identity classes - Remove non-necessary javadoc - Change timestamps' type from String to Long - Change id's type from Long to String
1 parent 0465926 commit 9d48528

26 files changed

+353
-422
lines changed

gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java

+33-28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.gcloud.compute;
1818

19+
import com.google.api.client.util.DateTime;
1920
import com.google.common.base.Function;
2021
import com.google.common.base.MoreObjects;
2122

@@ -24,14 +25,16 @@
2425

2526
/**
2627
* The deprecation status associated to a Google Compute Engine resource.
28+
*
29+
* @param <T> The Google Compute Engine resource to which the deprecation status refers to.
2730
*/
2831
public final class DeprecationStatus<T extends ResourceId> implements Serializable {
2932

3033
private static final long serialVersionUID = -2695077634793679794L;
3134

32-
private final String deleted;
33-
private final String deprecated;
34-
private final String obsolete;
35+
private final Long deleted;
36+
private final Long deprecated;
37+
private final Long obsolete;
3538
private final T replacement;
3639
private final Status status;
3740

@@ -58,8 +61,7 @@ public enum Status {
5861
DELETED
5962
}
6063

61-
DeprecationStatus(String deleted, String deprecated, String obsolete, T replacement,
62-
Status status) {
64+
DeprecationStatus(Long deleted, Long deprecated, Long obsolete, T replacement, Status status) {
6365
this.deleted = deleted;
6466
this.deprecated = deprecated;
6567
this.obsolete = obsolete;
@@ -68,32 +70,26 @@ public enum Status {
6870
}
6971

7072
/**
71-
* Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource
72-
* will be changed to {@link Status#DELETED}.
73-
*
74-
* @see <a href="https://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
73+
* Returns the timestamp on or after which the deprecation state of this resource will be changed
74+
* to {@link Status#DELETED}. In milliseconds since epoch.
7575
*/
76-
public String deleted() {
76+
public Long deleted() {
7777
return deleted;
7878
}
7979

8080
/**
81-
* Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource
82-
* will be changed to {@link Status#DEPRECATED}.
83-
*
84-
* @see <a href="https://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
81+
* Returns the timestamp on or after which the deprecation state of this resource will be changed
82+
* to {@link Status#DEPRECATED}. In milliseconds since epoch.
8583
*/
86-
public String deprecated() {
84+
public Long deprecated() {
8785
return deprecated;
8886
}
8987

9088
/**
91-
* Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource
92-
* will be changed to {@link Status#OBSOLETE}.
93-
*
94-
* @see <a href="https://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
89+
* Returns the timestamp on or after which the deprecation state of this resource will be changed
90+
* to {@link Status#OBSOLETE}. In milliseconds since epoch.
9591
*/
96-
public String obsolete() {
92+
public Long obsolete() {
9793
return obsolete;
9894
}
9995

@@ -137,11 +133,17 @@ public boolean equals(Object obj) {
137133
com.google.api.services.compute.model.DeprecationStatus toPb() {
138134
com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb =
139135
new com.google.api.services.compute.model.DeprecationStatus();
140-
deprecationStatusPb.setDeleted(deleted);
141-
deprecationStatusPb.setDeprecated(deprecated);
142-
deprecationStatusPb.setObsolete(obsolete);
136+
if (deleted != null) {
137+
deprecationStatusPb.setDeleted(new DateTime(deleted).toStringRfc3339());
138+
}
139+
if (deprecated != null) {
140+
deprecationStatusPb.setDeprecated(new DateTime(deprecated).toStringRfc3339());
141+
}
142+
if (obsolete != null) {
143+
deprecationStatusPb.setObsolete(new DateTime(obsolete).toStringRfc3339());
144+
}
143145
if (replacement != null) {
144-
deprecationStatusPb.setReplacement(replacement.toUrl());
146+
deprecationStatusPb.setReplacement(replacement.selfLink());
145147
}
146148
if (status() != null) {
147149
deprecationStatusPb.setState(status.name());
@@ -152,10 +154,13 @@ com.google.api.services.compute.model.DeprecationStatus toPb() {
152154
static <T extends ResourceId> DeprecationStatus<T> fromPb(
153155
com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb,
154156
Function<String, T> fromUrl) {
155-
return new DeprecationStatus<T>(
156-
deprecationStatusPb.getDeleted(),
157-
deprecationStatusPb.getDeprecated(),
158-
deprecationStatusPb.getObsolete(),
157+
return new DeprecationStatus<>(
158+
deprecationStatusPb.getDeleted() != null
159+
? DateTime.parseRfc3339(deprecationStatusPb.getDeleted()).getValue() : null,
160+
deprecationStatusPb.getDeprecated() != null
161+
? DateTime.parseRfc3339(deprecationStatusPb.getDeprecated()).getValue() : null,
162+
deprecationStatusPb.getObsolete() != null
163+
? DateTime.parseRfc3339(deprecationStatusPb.getObsolete()).getValue() : null,
159164
deprecationStatusPb.getReplacement() != null
160165
? fromUrl.apply(deprecationStatusPb.getReplacement()) : null,
161166
deprecationStatusPb.getState() != null

gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java

+25-34
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.gcloud.compute;
1818

19+
import com.google.api.client.util.DateTime;
1920
import com.google.common.base.Function;
2021
import com.google.common.base.MoreObjects;
2122

@@ -48,34 +49,32 @@ public com.google.api.services.compute.model.DiskType apply(DiskType diskType) {
4849

4950
private static final long serialVersionUID = -944042261695072026L;
5051

51-
private final BigInteger id;
52+
private final String id;
5253
private final DiskTypeId diskTypeId;
53-
private final String creationTimestamp;
54+
private final Long creationTimestamp;
5455
private final String description;
5556
private final String validDiskSize;
56-
private final String selfLink;
5757
private final Long defaultDiskSizeGb;
5858
private final DeprecationStatus<DiskTypeId> deprecationStatus;
5959

6060
static final class Builder {
6161

62-
private BigInteger id;
62+
private String id;
6363
private DiskTypeId diskTypeId;
64-
private String creationTimestamp;
64+
private Long creationTimestamp;
6565
private String description;
6666
private String validDiskSize;
67-
private String selfLink;
6867
private Long defaultDiskSizeGb;
6968
private DeprecationStatus<DiskTypeId> deprecationStatus;
7069

7170
private Builder() {}
7271

73-
Builder id(BigInteger id) {
72+
Builder id(String id) {
7473
this.id = id;
7574
return this;
7675
}
7776

78-
Builder creationTimestamp(String creationTimestamp) {
77+
Builder creationTimestamp(Long creationTimestamp) {
7978
this.creationTimestamp = creationTimestamp;
8079
return this;
8180
}
@@ -95,11 +94,6 @@ Builder validDiskSize(String validDiskSize) {
9594
return this;
9695
}
9796

98-
Builder selfLink(String selfLink) {
99-
this.selfLink = selfLink;
100-
return this;
101-
}
102-
10397
Builder defaultDiskSizeGb(Long defaultDiskSizeGb) {
10498
this.defaultDiskSizeGb = defaultDiskSizeGb;
10599
return this;
@@ -121,17 +115,14 @@ private DiskType(Builder builder) {
121115
this.diskTypeId = builder.diskTypeId;
122116
this.description = builder.description;
123117
this.validDiskSize = builder.validDiskSize;
124-
this.selfLink = builder.selfLink;
125118
this.defaultDiskSizeGb = builder.defaultDiskSizeGb;
126119
this.deprecationStatus = builder.deprecationStatus;
127120
}
128121

129122
/**
130-
* Returns the creation timestamp in RFC3339 text format.
131-
*
132-
* @see <a href="https://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
123+
* Returns the creation timestamp in milliseconds since epoch.
133124
*/
134-
public String creationTimestamp() {
125+
public Long creationTimestamp() {
135126
return creationTimestamp;
136127
}
137128

@@ -145,7 +136,7 @@ public DiskTypeId diskTypeId() {
145136
/**
146137
* Returns an unique identifier for the disk type; defined by the service.
147138
*/
148-
public BigInteger id() {
139+
public String id() {
149140
return id;
150141
}
151142

@@ -163,13 +154,6 @@ public String validDiskSize() {
163154
return validDiskSize;
164155
}
165156

166-
/**
167-
* Returns a service-defined URL for the disk type.
168-
*/
169-
public String selfLink() {
170-
return selfLink;
171-
}
172-
173157
/**
174158
* Returns the service-defined default disk size in GB.
175159
*/
@@ -193,7 +177,6 @@ public String toString() {
193177
.add("creationTimestamp", creationTimestamp)
194178
.add("description", description)
195179
.add("validDiskSize", validDiskSize)
196-
.add("selfLink", selfLink)
197180
.add("defaultDiskSizeGb", defaultDiskSizeGb)
198181
.add("deprecationStatus", deprecationStatus)
199182
.toString();
@@ -212,13 +195,17 @@ public boolean equals(Object obj) {
212195
com.google.api.services.compute.model.DiskType toPb() {
213196
com.google.api.services.compute.model.DiskType diskTypePb =
214197
new com.google.api.services.compute.model.DiskType();
215-
diskTypePb.setId(id);
216-
diskTypePb.setCreationTimestamp(creationTimestamp);
198+
if (id != null) {
199+
diskTypePb.setId(new BigInteger(id));
200+
}
201+
if (creationTimestamp != null) {
202+
diskTypePb.setCreationTimestamp(new DateTime(creationTimestamp).toStringRfc3339());
203+
}
217204
diskTypePb.setDescription(description);
218205
diskTypePb.setValidDiskSize(validDiskSize);
219-
diskTypePb.setSelfLink(selfLink);
206+
diskTypePb.setSelfLink(diskTypeId.selfLink());
220207
diskTypePb.setDefaultDiskSizeGb(defaultDiskSizeGb);
221-
diskTypePb.setZone(diskTypeId.zoneId().toUrl());
208+
diskTypePb.setZone(diskTypeId.zoneId().selfLink());
222209
if (deprecationStatus != null) {
223210
diskTypePb.setDeprecated(deprecationStatus.toPb());
224211
}
@@ -231,12 +218,16 @@ static Builder builder() {
231218

232219
static DiskType fromPb(com.google.api.services.compute.model.DiskType diskTypePb) {
233220
Builder builder = builder();
234-
builder.id(diskTypePb.getId());
235-
builder.creationTimestamp(diskTypePb.getCreationTimestamp());
221+
if (diskTypePb.getId() != null) {
222+
builder.id(diskTypePb.getId().toString());
223+
}
224+
if (diskTypePb.getCreationTimestamp() != null) {
225+
builder.creationTimestamp(
226+
DateTime.parseRfc3339(diskTypePb.getCreationTimestamp()).getValue());
227+
}
236228
builder.diskTypeId(DiskTypeId.fromUrl(diskTypePb.getSelfLink()));
237229
builder.description(diskTypePb.getDescription());
238230
builder.validDiskSize(diskTypePb.getValidDiskSize());
239-
builder.selfLink(diskTypePb.getSelfLink());
240231
builder.defaultDiskSizeGb(diskTypePb.getDefaultDiskSizeGb());
241232
if (diskTypePb.getDeprecated() != null) {
242233
builder.deprecationStatus(

gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java

+16-22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.google.common.base.MoreObjects;
2323

2424
import java.util.Objects;
25+
import java.util.regex.Matcher;
26+
import java.util.regex.Pattern;
2527

2628
/**
2729
* Identity for a Google Compute Engine disk type.
@@ -37,11 +39,12 @@ public DiskTypeId apply(String pb) {
3739
static final Function<DiskTypeId, String> TO_URL_FUNCTION = new Function<DiskTypeId, String>() {
3840
@Override
3941
public String apply(DiskTypeId diskTypeId) {
40-
return diskTypeId.toUrl();
42+
return diskTypeId.selfLink();
4143
}
4244
};
4345

44-
static final String REGEX = ZoneResourceId.REGEX + "diskTypes/[^/]+";
46+
private static final String REGEX = ZoneResourceId.REGEX + "diskTypes/([^/]+)";
47+
private static final Pattern PATTERN = Pattern.compile(REGEX);
4548
private static final long serialVersionUID = 7337881474103686219L;
4649

4750
private final String diskType;
@@ -52,21 +55,15 @@ private DiskTypeId(String project, String zone, String diskType) {
5255
}
5356

5457
/**
55-
* Returns the name of the disk type resource. The name must be 1-63 characters long, and comply
56-
* with RFC1035. Specifically, the name must be 1-63 characters long and match the regular
57-
* expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a
58-
* lowercase letter, and all following characters must be a dash, lowercase letter, or digit,
59-
* except the last character, which cannot be a dash.
60-
*
61-
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>
58+
* Returns the name of the disk type.
6259
*/
6360
public String diskType() {
6461
return diskType;
6562
}
6663

6764
@Override
68-
public String toUrl() {
69-
return super.toUrl() + "/diskTypes/" + diskType;
65+
public String selfLink() {
66+
return super.selfLink() + "/diskTypes/" + diskType;
7067
}
7168

7269
@Override
@@ -76,12 +73,14 @@ MoreObjects.ToStringHelper toStringHelper() {
7673

7774
@Override
7875
public int hashCode() {
79-
return Objects.hash(super.hashCode(), diskType);
76+
return Objects.hash(super.baseHashCode(), diskType);
8077
}
8178

8279
@Override
8380
public boolean equals(Object obj) {
84-
return obj instanceof DiskTypeId && baseEquals((DiskTypeId) obj);
81+
return obj instanceof DiskTypeId
82+
&& baseEquals((DiskTypeId) obj)
83+
&& Objects.equals(diskType, ((DiskTypeId) obj).diskType);
8584
}
8685

8786
@Override
@@ -118,19 +117,14 @@ public static DiskTypeId of(String project, String zone, String diskType) {
118117
* Returns {@code false} otherwise.
119118
*/
120119
static boolean matchesUrl(String url) {
121-
return url.matches(REGEX);
120+
return PATTERN.matcher(url).matches();
122121
}
123122

124123
static DiskTypeId fromUrl(String url) {
125-
if (!matchesUrl(url)) {
124+
Matcher matcher = PATTERN.matcher(url);
125+
if (!matcher.matches()) {
126126
throw new IllegalArgumentException(url + " is not a valid disk type URL");
127127
}
128-
int projectsIndex = url.indexOf("/projects/");
129-
int zonesIndex = url.indexOf("/zones/");
130-
int diskTypesIndex = url.indexOf("/diskTypes/");
131-
String project = url.substring(projectsIndex + 10, zonesIndex);
132-
String zone = url.substring(zonesIndex + 7, diskTypesIndex);
133-
String diskType = url.substring(diskTypesIndex + 11, url.length());
134-
return DiskTypeId.of(project, zone, diskType);
128+
return DiskTypeId.of(matcher.group(1), matcher.group(2), matcher.group(3));
135129
}
136130
}

0 commit comments

Comments
 (0)