Skip to content

Commit

Permalink
Refactor Compute immutable resources' identities and classes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mziccard committed Feb 29, 2016
1 parent 0465926 commit 9d48528
Show file tree
Hide file tree
Showing 26 changed files with 353 additions and 422 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.gcloud.compute;

import com.google.api.client.util.DateTime;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;

Expand All @@ -24,14 +25,16 @@

/**
* The deprecation status associated to a Google Compute Engine resource.
*
* @param <T> The Google Compute Engine resource to which the deprecation status refers to.
*/
public final class DeprecationStatus<T extends ResourceId> implements Serializable {

private static final long serialVersionUID = -2695077634793679794L;

private final String deleted;
private final String deprecated;
private final String obsolete;
private final Long deleted;
private final Long deprecated;
private final Long obsolete;
private final T replacement;
private final Status status;

Expand All @@ -58,8 +61,7 @@ public enum Status {
DELETED
}

DeprecationStatus(String deleted, String deprecated, String obsolete, T replacement,
Status status) {
DeprecationStatus(Long deleted, Long deprecated, Long obsolete, T replacement, Status status) {
this.deleted = deleted;
this.deprecated = deprecated;
this.obsolete = obsolete;
Expand All @@ -68,32 +70,26 @@ public enum Status {
}

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

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

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

Expand Down Expand Up @@ -137,11 +133,17 @@ public boolean equals(Object obj) {
com.google.api.services.compute.model.DeprecationStatus toPb() {
com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb =
new com.google.api.services.compute.model.DeprecationStatus();
deprecationStatusPb.setDeleted(deleted);
deprecationStatusPb.setDeprecated(deprecated);
deprecationStatusPb.setObsolete(obsolete);
if (deleted != null) {
deprecationStatusPb.setDeleted(new DateTime(deleted).toStringRfc3339());
}
if (deprecated != null) {
deprecationStatusPb.setDeprecated(new DateTime(deprecated).toStringRfc3339());
}
if (obsolete != null) {
deprecationStatusPb.setObsolete(new DateTime(obsolete).toStringRfc3339());
}
if (replacement != null) {
deprecationStatusPb.setReplacement(replacement.toUrl());
deprecationStatusPb.setReplacement(replacement.selfLink());
}
if (status() != null) {
deprecationStatusPb.setState(status.name());
Expand All @@ -152,10 +154,13 @@ com.google.api.services.compute.model.DeprecationStatus toPb() {
static <T extends ResourceId> DeprecationStatus<T> fromPb(
com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb,
Function<String, T> fromUrl) {
return new DeprecationStatus<T>(
deprecationStatusPb.getDeleted(),
deprecationStatusPb.getDeprecated(),
deprecationStatusPb.getObsolete(),
return new DeprecationStatus<>(
deprecationStatusPb.getDeleted() != null
? DateTime.parseRfc3339(deprecationStatusPb.getDeleted()).getValue() : null,
deprecationStatusPb.getDeprecated() != null
? DateTime.parseRfc3339(deprecationStatusPb.getDeprecated()).getValue() : null,
deprecationStatusPb.getObsolete() != null
? DateTime.parseRfc3339(deprecationStatusPb.getObsolete()).getValue() : null,
deprecationStatusPb.getReplacement() != null
? fromUrl.apply(deprecationStatusPb.getReplacement()) : null,
deprecationStatusPb.getState() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.gcloud.compute;

import com.google.api.client.util.DateTime;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;

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

private static final long serialVersionUID = -944042261695072026L;

private final BigInteger id;
private final String id;
private final DiskTypeId diskTypeId;
private final String creationTimestamp;
private final Long creationTimestamp;
private final String description;
private final String validDiskSize;
private final String selfLink;
private final Long defaultDiskSizeGb;
private final DeprecationStatus<DiskTypeId> deprecationStatus;

static final class Builder {

private BigInteger id;
private String id;
private DiskTypeId diskTypeId;
private String creationTimestamp;
private Long creationTimestamp;
private String description;
private String validDiskSize;
private String selfLink;
private Long defaultDiskSizeGb;
private DeprecationStatus<DiskTypeId> deprecationStatus;

private Builder() {}

Builder id(BigInteger id) {
Builder id(String id) {
this.id = id;
return this;
}

Builder creationTimestamp(String creationTimestamp) {
Builder creationTimestamp(Long creationTimestamp) {
this.creationTimestamp = creationTimestamp;
return this;
}
Expand All @@ -95,11 +94,6 @@ Builder validDiskSize(String validDiskSize) {
return this;
}

Builder selfLink(String selfLink) {
this.selfLink = selfLink;
return this;
}

Builder defaultDiskSizeGb(Long defaultDiskSizeGb) {
this.defaultDiskSizeGb = defaultDiskSizeGb;
return this;
Expand All @@ -121,17 +115,14 @@ private DiskType(Builder builder) {
this.diskTypeId = builder.diskTypeId;
this.description = builder.description;
this.validDiskSize = builder.validDiskSize;
this.selfLink = builder.selfLink;
this.defaultDiskSizeGb = builder.defaultDiskSizeGb;
this.deprecationStatus = builder.deprecationStatus;
}

/**
* Returns the creation timestamp in RFC3339 text format.
*
* @see <a href="https://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
* Returns the creation timestamp in milliseconds since epoch.
*/
public String creationTimestamp() {
public Long creationTimestamp() {
return creationTimestamp;
}

Expand All @@ -145,7 +136,7 @@ public DiskTypeId diskTypeId() {
/**
* Returns an unique identifier for the disk type; defined by the service.
*/
public BigInteger id() {
public String id() {
return id;
}

Expand All @@ -163,13 +154,6 @@ public String validDiskSize() {
return validDiskSize;
}

/**
* Returns a service-defined URL for the disk type.
*/
public String selfLink() {
return selfLink;
}

/**
* Returns the service-defined default disk size in GB.
*/
Expand All @@ -193,7 +177,6 @@ public String toString() {
.add("creationTimestamp", creationTimestamp)
.add("description", description)
.add("validDiskSize", validDiskSize)
.add("selfLink", selfLink)
.add("defaultDiskSizeGb", defaultDiskSizeGb)
.add("deprecationStatus", deprecationStatus)
.toString();
Expand All @@ -212,13 +195,17 @@ public boolean equals(Object obj) {
com.google.api.services.compute.model.DiskType toPb() {
com.google.api.services.compute.model.DiskType diskTypePb =
new com.google.api.services.compute.model.DiskType();
diskTypePb.setId(id);
diskTypePb.setCreationTimestamp(creationTimestamp);
if (id != null) {
diskTypePb.setId(new BigInteger(id));
}
if (creationTimestamp != null) {
diskTypePb.setCreationTimestamp(new DateTime(creationTimestamp).toStringRfc3339());
}
diskTypePb.setDescription(description);
diskTypePb.setValidDiskSize(validDiskSize);
diskTypePb.setSelfLink(selfLink);
diskTypePb.setSelfLink(diskTypeId.selfLink());
diskTypePb.setDefaultDiskSizeGb(defaultDiskSizeGb);
diskTypePb.setZone(diskTypeId.zoneId().toUrl());
diskTypePb.setZone(diskTypeId.zoneId().selfLink());
if (deprecationStatus != null) {
diskTypePb.setDeprecated(deprecationStatus.toPb());
}
Expand All @@ -231,12 +218,16 @@ static Builder builder() {

static DiskType fromPb(com.google.api.services.compute.model.DiskType diskTypePb) {
Builder builder = builder();
builder.id(diskTypePb.getId());
builder.creationTimestamp(diskTypePb.getCreationTimestamp());
if (diskTypePb.getId() != null) {
builder.id(diskTypePb.getId().toString());
}
if (diskTypePb.getCreationTimestamp() != null) {
builder.creationTimestamp(
DateTime.parseRfc3339(diskTypePb.getCreationTimestamp()).getValue());
}
builder.diskTypeId(DiskTypeId.fromUrl(diskTypePb.getSelfLink()));
builder.description(diskTypePb.getDescription());
builder.validDiskSize(diskTypePb.getValidDiskSize());
builder.selfLink(diskTypePb.getSelfLink());
builder.defaultDiskSizeGb(diskTypePb.getDefaultDiskSizeGb());
if (diskTypePb.getDeprecated() != null) {
builder.deprecationStatus(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.google.common.base.MoreObjects;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Identity for a Google Compute Engine disk type.
Expand All @@ -37,11 +39,12 @@ public DiskTypeId apply(String pb) {
static final Function<DiskTypeId, String> TO_URL_FUNCTION = new Function<DiskTypeId, String>() {
@Override
public String apply(DiskTypeId diskTypeId) {
return diskTypeId.toUrl();
return diskTypeId.selfLink();
}
};

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

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

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

@Override
public String toUrl() {
return super.toUrl() + "/diskTypes/" + diskType;
public String selfLink() {
return super.selfLink() + "/diskTypes/" + diskType;
}

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

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), diskType);
return Objects.hash(super.baseHashCode(), diskType);
}

@Override
public boolean equals(Object obj) {
return obj instanceof DiskTypeId && baseEquals((DiskTypeId) obj);
return obj instanceof DiskTypeId
&& baseEquals((DiskTypeId) obj)
&& Objects.equals(diskType, ((DiskTypeId) obj).diskType);
}

@Override
Expand Down Expand Up @@ -118,19 +117,14 @@ public static DiskTypeId of(String project, String zone, String diskType) {
* Returns {@code false} otherwise.
*/
static boolean matchesUrl(String url) {
return url.matches(REGEX);
return PATTERN.matcher(url).matches();
}

static DiskTypeId fromUrl(String url) {
if (!matchesUrl(url)) {
Matcher matcher = PATTERN.matcher(url);
if (!matcher.matches()) {
throw new IllegalArgumentException(url + " is not a valid disk type URL");
}
int projectsIndex = url.indexOf("/projects/");
int zonesIndex = url.indexOf("/zones/");
int diskTypesIndex = url.indexOf("/diskTypes/");
String project = url.substring(projectsIndex + 10, zonesIndex);
String zone = url.substring(zonesIndex + 7, diskTypesIndex);
String diskType = url.substring(diskTypesIndex + 11, url.length());
return DiskTypeId.of(project, zone, diskType);
return DiskTypeId.of(matcher.group(1), matcher.group(2), matcher.group(3));
}
}
Loading

0 comments on commit 9d48528

Please sign in to comment.