Skip to content

Commit a5550f5

Browse files
Convert all Compute deserialized enum types to StringEnumValue
Addresses #1944.
1 parent ae640b7 commit a5550f5

4 files changed

Lines changed: 189 additions & 17 deletions

File tree

google-cloud-compute/src/main/java/com/google/cloud/compute/NetworkInterface.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

21+
import com.google.api.core.ApiFunction;
22+
import com.google.cloud.StringEnumType;
23+
import com.google.cloud.StringEnumValue;
2124
import com.google.common.base.Function;
2225
import com.google.common.base.MoreObjects;
2326
import com.google.common.collect.ImmutableList;
@@ -106,8 +109,48 @@ public com.google.api.services.compute.model.AccessConfig apply(
106109
/**
107110
* The type of network access configuration. The only supported value is {@code ONE_TO_ONE_NAT}.
108111
*/
109-
public enum Type {
110-
ONE_TO_ONE_NAT
112+
public static final class Type extends StringEnumValue {
113+
private static final long serialVersionUID = -1181560540824208967L;
114+
115+
private static final ApiFunction<String, Type> CONSTRUCTOR =
116+
new ApiFunction<String, Type>() {
117+
@Override
118+
public Type apply(String constant) {
119+
return new Type(constant);
120+
}
121+
};
122+
123+
private static final StringEnumType<Type> type = new StringEnumType(
124+
Type.class,
125+
CONSTRUCTOR);
126+
127+
public static final Type ONE_TO_ONE_NAT = type.createAndRegister("ONE_TO_ONE_NAT");
128+
129+
private Type(String constant) {
130+
super(constant);
131+
}
132+
133+
/**
134+
* Get the Type for the given String constant, and throw an exception if the constant is
135+
* not recognized.
136+
*/
137+
public static Type valueOfStrict(String constant) {
138+
return type.valueOfStrict(constant);
139+
}
140+
141+
/**
142+
* Get the Type for the given String constant, and allow unrecognized values.
143+
*/
144+
public static Type valueOf(String constant) {
145+
return type.valueOf(constant);
146+
}
147+
148+
/**
149+
* Return the known values for Type.
150+
*/
151+
public static Type[] values() {
152+
return type.values();
153+
}
111154
}
112155

113156
public static final class Builder {

google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@
1616

1717
package com.google.cloud.compute;
1818

19+
import com.google.api.core.ApiFunction;
20+
import com.google.cloud.StringEnumType;
21+
import com.google.cloud.StringEnumValue;
1922
import com.google.common.base.Function;
2023
import com.google.common.base.MoreObjects;
2124
import com.google.common.collect.ImmutableList;
2225
import com.google.common.collect.Lists;
2326

24-
import org.joda.time.format.DateTimeFormatter;
25-
import org.joda.time.format.ISODateTimeFormat;
26-
2727
import java.io.Serializable;
2828
import java.math.BigInteger;
2929
import java.util.List;
3030
import java.util.Objects;
3131

32+
import org.joda.time.format.DateTimeFormatter;
33+
import org.joda.time.format.ISODateTimeFormat;
34+
3235
/**
3336
* A Google Compute Engine region.
3437
*
@@ -66,9 +69,49 @@ public com.google.api.services.compute.model.Region apply(Region region) {
6669
/**
6770
* Status of the region.
6871
*/
69-
public enum Status {
70-
UP,
71-
DOWN
72+
public static final class Status extends StringEnumValue {
73+
private static final long serialVersionUID = 658578478474360264L;
74+
75+
private static final ApiFunction<String, Status> CONSTRUCTOR =
76+
new ApiFunction<String, Status>() {
77+
@Override
78+
public Status apply(String constant) {
79+
return new Status(constant);
80+
}
81+
};
82+
83+
private static final StringEnumType<Status> type = new StringEnumType(
84+
Status.class,
85+
CONSTRUCTOR);
86+
87+
public static final Status UP = type.createAndRegister("UP");
88+
public static final Status DOWN = type.createAndRegister("DOWN");
89+
90+
private Status(String constant) {
91+
super(constant);
92+
}
93+
94+
/**
95+
* Get the Status for the given String constant, and throw an exception if the constant is
96+
* not recognized.
97+
*/
98+
public static Status valueOfStrict(String constant) {
99+
return type.valueOfStrict(constant);
100+
}
101+
102+
/**
103+
* Get the Status for the given String constant, and allow unrecognized values.
104+
*/
105+
public static Status valueOf(String constant) {
106+
return type.valueOf(constant);
107+
}
108+
109+
/**
110+
* Return the known values for Status.
111+
*/
112+
public static Status[] values() {
113+
return type.values();
114+
}
72115
}
73116

74117
/**

google-cloud-compute/src/main/java/com/google/cloud/compute/SchedulingOptions.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.google.cloud.compute;
1818

19+
import com.google.api.core.ApiFunction;
20+
import com.google.cloud.StringEnumType;
21+
import com.google.cloud.StringEnumValue;
1922
import com.google.common.base.MoreObjects;
2023

2124
import java.io.Serializable;
@@ -42,16 +45,56 @@ public final class SchedulingOptions implements Serializable {
4245
/**
4346
* Defines the maintenance behavior for this instance.
4447
*/
45-
public enum Maintenance {
48+
public static final class Maintenance extends StringEnumValue {
49+
private static final long serialVersionUID = 1041388027965833378L;
50+
51+
private static final ApiFunction<String, Maintenance> CONSTRUCTOR =
52+
new ApiFunction<String, Maintenance>() {
53+
@Override
54+
public Maintenance apply(String constant) {
55+
return new Maintenance(constant);
56+
}
57+
};
58+
59+
private static final StringEnumType<Maintenance> type = new StringEnumType(
60+
Maintenance.class,
61+
CONSTRUCTOR);
62+
4663
/**
4764
* The default behavior for standard instances.
4865
*/
49-
MIGRATE,
66+
public static final Maintenance MIGRATE = type.createAndRegister("MIGRATE");
5067

5168
/**
5269
* The default and only possible behavior for preemptible instances.
5370
*/
54-
TERMINATE
71+
public static final Maintenance TERMINATE = type.createAndRegister("TERMINATE");
72+
73+
private Maintenance(String constant) {
74+
super(constant);
75+
}
76+
77+
/**
78+
* Get the Maintenance for the given String constant, and throw an exception if the constant is
79+
* not recognized.
80+
*/
81+
public static Maintenance valueOfStrict(String constant) {
82+
return type.valueOfStrict(constant);
83+
}
84+
85+
/**
86+
* Get the Maintenance for the given String constant, and allow unrecognized values.
87+
*/
88+
public static Maintenance valueOf(String constant) {
89+
return type.valueOf(constant);
90+
}
91+
92+
/**
93+
* Return the known values for Maintenance.
94+
*/
95+
public static Maintenance[] values() {
96+
return type.values();
97+
}
5598
}
5699

57100
private SchedulingOptions(Boolean automaticRestart, Maintenance maintenance,

google-cloud-compute/src/main/java/com/google/cloud/compute/Zone.java

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616

1717
package com.google.cloud.compute;
1818

19+
import com.google.api.core.ApiFunction;
20+
import com.google.cloud.StringEnumType;
21+
import com.google.cloud.StringEnumValue;
1922
import com.google.common.base.Function;
2023
import com.google.common.base.MoreObjects;
2124

22-
import org.joda.time.format.DateTimeFormatter;
23-
import org.joda.time.format.ISODateTimeFormat;
24-
2525
import java.io.Serializable;
2626
import java.math.BigInteger;
2727
import java.util.Objects;
2828

29+
import org.joda.time.format.DateTimeFormatter;
30+
import org.joda.time.format.ISODateTimeFormat;
31+
2932
/**
3033
* A Google Compute Engine zone.
3134
*
@@ -62,9 +65,49 @@ public com.google.api.services.compute.model.Zone apply(Zone region) {
6265
/**
6366
* Status of the region.
6467
*/
65-
public enum Status {
66-
UP,
67-
DOWN
68+
public static final class Status extends StringEnumValue {
69+
private static final long serialVersionUID = -1052872318386811804L;
70+
71+
private static final ApiFunction<String, Status> CONSTRUCTOR =
72+
new ApiFunction<String, Status>() {
73+
@Override
74+
public Status apply(String constant) {
75+
return new Status(constant);
76+
}
77+
};
78+
79+
private static final StringEnumType<Status> type = new StringEnumType(
80+
Status.class,
81+
CONSTRUCTOR);
82+
83+
public static final Status UP = type.createAndRegister("UP");
84+
public static final Status DOWN = type.createAndRegister("DOWN");
85+
86+
private Status(String constant) {
87+
super(constant);
88+
}
89+
90+
/**
91+
* Get the Status for the given String constant, and throw an exception if the constant is
92+
* not recognized.
93+
*/
94+
public static Status valueOfStrict(String constant) {
95+
return type.valueOfStrict(constant);
96+
}
97+
98+
/**
99+
* Get the Status for the given String constant, and allow unrecognized values.
100+
*/
101+
public static Status valueOf(String constant) {
102+
return type.valueOf(constant);
103+
}
104+
105+
/**
106+
* Return the known values for Status.
107+
*/
108+
public static Status[] values() {
109+
return type.values();
110+
}
68111
}
69112

70113
static final class Builder {

0 commit comments

Comments
 (0)