Skip to content

Commit 2d691aa

Browse files
committed
Refactor identity classes
- Remove Zone/RegionResourceId - Make OperationId and AddressId abstract classes - Refactor javadoc - Add abstract setProjectId to AddressId and OperationId - Split testToAndFromUrl tests - Add better javadoc to identity classes
1 parent f3c30f1 commit 2d691aa

17 files changed

Lines changed: 391 additions & 425 deletions

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

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@
1616

1717
package com.google.gcloud.compute;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.common.base.MoreObjects;
22+
23+
import java.util.Objects;
24+
1925
/**
20-
* Interface for Google Compute Engine address identities.
26+
* Base class for Google Compute Engine address identities.
2127
*/
22-
public interface AddressId {
28+
public abstract class AddressId extends ResourceId {
29+
30+
private static final long serialVersionUID = 147328216049936438L;
31+
32+
private final String address;
2333

2434
/**
2535
* Possible types for a Google Compute Engine address identity.
@@ -29,35 +39,53 @@ enum Type {
2939
* Global static external IP addresses can be assigned to global forwarding rules.
3040
*/
3141
GLOBAL,
42+
3243
/**
3344
* Region static external IP addresses can be assigned to instances and region forwarding rules.
3445
*/
3546
REGION
3647
}
3748

38-
/**
39-
* Returns the type of this address identity.
40-
*/
41-
Type type();
49+
AddressId(String project, String address) {
50+
super(project);
51+
this.address = checkNotNull(address);
52+
}
4253

4354
/**
44-
* Returns the name of the project.
55+
* Returns the type of this address identity.
4556
*/
46-
String project();
57+
public abstract Type type();
4758

4859
/**
49-
* Returns the name of the address resource. The name must be 1-63 characters long, and comply
50-
* with RFC1035. Specifically, the name must be 1-63 characters long and match the regular
51-
* expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a
52-
* lowercase letter, and all following characters must be a dash, lowercase letter, or digit,
53-
* except the last character, which cannot be a dash.
60+
* Returns the name of the address resource. The name must be 1-63 characters long and comply with
61+
* RFC1035. Specifically, the name must match the regular expression
62+
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter,
63+
* and all following characters must be a dash, lowercase letter, or digit, except the last
64+
* character, which cannot be a dash.
5465
*
5566
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>
5667
*/
57-
String address();
68+
public String address() {
69+
return address;
70+
}
5871

59-
/**
60-
* Returns a fully qualified URL to the entity.
61-
*/
62-
String selfLink();
72+
@Override
73+
MoreObjects.ToStringHelper toStringHelper() {
74+
return super.toStringHelper().add("address", address);
75+
}
76+
77+
@Override
78+
final int baseHashCode() {
79+
return Objects.hash(super.baseHashCode(), address);
80+
}
81+
82+
@Override
83+
final boolean baseEquals(ResourceId resourceId) {
84+
return resourceId instanceof AddressId
85+
&& super.baseEquals(resourceId)
86+
&& Objects.equals(address, ((AddressId) resourceId).address);
87+
}
88+
89+
@Override
90+
abstract AddressId setProjectId(String projectId);
6391
}

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* Identity for a Google Compute Engine disk type.
3030
*/
31-
public final class DiskTypeId extends ZoneResourceId {
31+
public final class DiskTypeId extends ResourceId {
3232

3333
static final Function<String, DiskTypeId> FROM_URL_FUNCTION = new Function<String, DiskTypeId>() {
3434
@Override
@@ -43,14 +43,16 @@ public String apply(DiskTypeId diskTypeId) {
4343
}
4444
};
4545

46-
private static final String REGEX = ZoneResourceId.REGEX + "diskTypes/([^/]+)";
46+
private static final String REGEX = ResourceId.REGEX + "zones/([^/]+)/diskTypes/([^/]+)";
4747
private static final Pattern PATTERN = Pattern.compile(REGEX);
4848
private static final long serialVersionUID = 7337881474103686219L;
4949

50+
private final String zone;
5051
private final String diskType;
5152

5253
private DiskTypeId(String project, String zone, String diskType) {
53-
super(project, zone);
54+
super(project);
55+
this.zone = checkNotNull(zone);
5456
this.diskType = checkNotNull(diskType);
5557
}
5658

@@ -61,25 +63,40 @@ public String diskType() {
6163
return diskType;
6264
}
6365

66+
/**
67+
* Returns the name of the zone this disk type belongs to.
68+
*/
69+
public String zone() {
70+
return zone;
71+
}
72+
73+
/**
74+
* Returns the identity of the zone this disk type belongs to.
75+
*/
76+
public ZoneId zoneId() {
77+
return ZoneId.of(project(), zone);
78+
}
79+
6480
@Override
6581
public String selfLink() {
66-
return super.selfLink() + "/diskTypes/" + diskType;
82+
return super.selfLink() + "/zones/" + zone + "/diskTypes/" + diskType;
6783
}
6884

6985
@Override
7086
MoreObjects.ToStringHelper toStringHelper() {
71-
return super.toStringHelper().add("diskType", diskType);
87+
return super.toStringHelper().add("zone", zone).add("diskType", diskType);
7288
}
7389

7490
@Override
7591
public int hashCode() {
76-
return Objects.hash(super.baseHashCode(), diskType);
92+
return Objects.hash(super.baseHashCode(), zone, diskType);
7793
}
7894

7995
@Override
8096
public boolean equals(Object obj) {
8197
return obj instanceof DiskTypeId
8298
&& baseEquals((DiskTypeId) obj)
99+
&& Objects.equals(zone, ((DiskTypeId) obj).zone)
83100
&& Objects.equals(diskType, ((DiskTypeId) obj).diskType);
84101
}
85102

@@ -88,7 +105,7 @@ DiskTypeId setProjectId(String projectId) {
88105
if (project() != null) {
89106
return this;
90107
}
91-
return DiskTypeId.of(projectId, zone(), diskType);
108+
return DiskTypeId.of(projectId, zone, diskType);
92109
}
93110

94111
/**

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

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,25 @@
1616

1717
package com.google.gcloud.compute;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.common.base.MoreObjects;
22+
23+
import java.util.Objects;
24+
1925
/**
2026
* Interface for Google Compute Engine forwarding rule identities.
2127
*/
22-
public interface ForwardingRuleId {
28+
public abstract class ForwardingRuleId extends ResourceId {
29+
30+
private static final long serialVersionUID = -4352410760458355391L;
31+
32+
private final String rule;
33+
34+
ForwardingRuleId(String project, String rule) {
35+
super(project);
36+
this.rule = checkNotNull(rule);
37+
}
2338

2439
/**
2540
* Possible types for a Google Compute Engine forwarding rule identity.
@@ -30,6 +45,7 @@ enum Type {
3045
* load balancing.
3146
*/
3247
GLOBAL,
48+
3349
/**
3450
* Region forwarding rules are used to forward traffic to the correct pool of target virtual
3551
* machines.
@@ -40,26 +56,38 @@ enum Type {
4056
/**
4157
* Returns the type of this forwarding rule identity.
4258
*/
43-
Type type();
44-
45-
/**
46-
* Returns the name of the project.
47-
*/
48-
String project();
59+
public abstract Type type();
4960

5061
/**
51-
* Returns the name of the forwarding rule. The name must be 1-63 characters long, and comply with
52-
* RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression
62+
* Returns the name of the forwarding rule. The forwarding rule name must be 1-63 characters long
63+
* and comply with RFC1035. Specifically, the name must match the regular expression
5364
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter,
5465
* and all following characters must be a dash, lowercase letter, or digit, except the last
5566
* character, which cannot be a dash.
5667
*
5768
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>
5869
*/
59-
String rule();
70+
public String rule() {
71+
return rule;
72+
}
6073

61-
/**
62-
* Returns a fully qualified URL to the entity.
63-
*/
64-
String selfLink();
74+
@Override
75+
MoreObjects.ToStringHelper toStringHelper() {
76+
return super.toStringHelper().add("rule", rule);
77+
}
78+
79+
@Override
80+
final int baseHashCode() {
81+
return Objects.hash(super.baseHashCode(), rule);
82+
}
83+
84+
@Override
85+
final boolean baseEquals(ResourceId resourceId) {
86+
return resourceId instanceof ForwardingRuleId
87+
&& super.baseEquals(resourceId)
88+
&& Objects.equals(rule, ((ForwardingRuleId) resourceId).rule);
89+
}
90+
91+
@Override
92+
abstract ForwardingRuleId setProjectId(String projectId);
6593
}

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

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,76 +16,56 @@
1616

1717
package com.google.gcloud.compute;
1818

19-
import static com.google.common.base.Preconditions.checkNotNull;
20-
21-
import com.google.common.base.MoreObjects.ToStringHelper;
22-
23-
import java.util.Objects;
2419
import java.util.regex.Matcher;
2520
import java.util.regex.Pattern;
2621

2722
/**
2823
* Identity for a Google Compute Engine global address.
2924
*/
30-
public final class GlobalAddressId extends ResourceId implements AddressId {
25+
public final class GlobalAddressId extends AddressId {
3126

3227
private static final String REGEX = ResourceId.REGEX + "global/addresses/([^/]+)";
3328
private static final Pattern PATTERN = Pattern.compile(REGEX);
3429
private static final long serialVersionUID = -2950815290049218593L;
3530

36-
private final String address;
37-
3831
private GlobalAddressId(String project, String address) {
39-
super(project);
40-
this.address = checkNotNull(address);
32+
super(project, address);
4133
}
4234

4335
@Override
4436
public Type type() {
4537
return Type.GLOBAL;
4638
}
4739

48-
@Override
49-
public String address() {
50-
return address;
51-
}
52-
5340
@Override
5441
public String selfLink() {
55-
return super.selfLink() + "/global/addresses/" + address;
56-
}
57-
58-
@Override
59-
public ToStringHelper toStringHelper() {
60-
return super.toStringHelper().add("address", address);
42+
return super.selfLink() + "/global/addresses/" + address();
6143
}
6244

6345
@Override
6446
public int hashCode() {
65-
return Objects.hash(baseHashCode(), address);
47+
return baseHashCode();
6648
}
6749

6850
@Override
6951
public boolean equals(Object obj) {
70-
return obj instanceof GlobalAddressId
71-
&& baseEquals((GlobalAddressId) obj)
72-
&& Objects.equals(address, ((GlobalAddressId) obj).address);
52+
return obj instanceof GlobalAddressId && baseEquals((GlobalAddressId) obj);
7353
}
7454

7555
@Override
7656
GlobalAddressId setProjectId(String projectId) {
7757
if (project() != null) {
7858
return this;
7959
}
80-
return GlobalAddressId.of(projectId, address);
60+
return GlobalAddressId.of(projectId, address());
8161
}
8262

8363
/**
8464
* Returns an address identity given the address name. The address name must be 1-63 characters
85-
* long, and comply with RFC1035. Specifically, the name must be 1-63 characters long and match
86-
* the regular expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must
87-
* be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit,
88-
* except the last character, which cannot be a dash.
65+
* long and comply with RFC1035. Specifically, the name must match the regular expression
66+
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter,
67+
* and all following characters must be a dash, lowercase letter, or digit, except the last
68+
* character, which cannot be a dash.
8969
*
9070
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>
9171
*/
@@ -95,10 +75,10 @@ public static GlobalAddressId of(String address) {
9575

9676
/**
9777
* Returns an address identity given project and address names. The address name must be 1-63
98-
* characters long, and comply with RFC1035. Specifically, the name must be 1-63 characters long
99-
* and match the regular expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first
100-
* character must be a lowercase letter, and all following characters must be a dash, lowercase
101-
* letter, or digit, except the last character, which cannot be a dash.
78+
* characters long and comply with RFC1035. Specifically, the name must match the regular
79+
* expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a
80+
* lowercase letter, and all following characters must be a dash, lowercase letter, or digit,
81+
* except the last character, which cannot be a dash.
10282
*
10383
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>
10484
*/

0 commit comments

Comments
 (0)