Skip to content

Commit f2231c6

Browse files
committed
Add ImageInfo and related non-functional classes
- Add public builder and factory methods to DeprecationStatus - Add setters/getters for DeprecationStatus timestamps as String - Add ImageId, configuration classes and ImageInfo class
1 parent bc2d7a1 commit f2231c6

16 files changed

Lines changed: 1944 additions & 76 deletions

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

Lines changed: 229 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.gcloud.compute;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
1921
import com.google.common.base.Function;
2022
import com.google.common.base.MoreObjects;
2123

@@ -28,16 +30,17 @@
2830
/**
2931
* The deprecation status associated to a Google Compute Engine resource.
3032
*
31-
* @param <T> The Google Compute Engine resource to which the deprecation status refers to.
33+
* @param <T> The Google Compute Engine resource identity to which the deprecation status refers
3234
*/
3335
public final class DeprecationStatus<T extends ResourceId> implements Serializable {
3436

3537
private static final long serialVersionUID = -2695077634793679794L;
3638
private static final DateTimeFormatter TIMESTAMP_FORMATTER = ISODateTimeFormat.dateTime();
39+
private static final DateTimeFormatter TIMESTAMP_PARSER = ISODateTimeFormat.dateTimeParser();
3740

38-
private final Long deleted;
39-
private final Long deprecated;
40-
private final Long obsolete;
41+
private final String deleted;
42+
private final String deprecated;
43+
private final String obsolete;
4144
private final T replacement;
4245
private final Status status;
4346

@@ -64,38 +67,203 @@ public enum Status {
6467
DELETED
6568
}
6669

67-
DeprecationStatus(Long deleted, Long deprecated, Long obsolete, T replacement, Status status) {
68-
this.deleted = deleted;
69-
this.deprecated = deprecated;
70-
this.obsolete = obsolete;
71-
this.replacement = replacement;
72-
this.status = status;
70+
/**
71+
* A builder for {@code DeprecationStatus} objects.
72+
*
73+
* @param <T> The Google Compute Engine resource identity to which the deprecation status refers
74+
*/
75+
public static final class Builder<T extends ResourceId> {
76+
77+
private String deleted;
78+
private String deprecated;
79+
private String obsolete;
80+
private T replacement;
81+
private Status status;
82+
83+
Builder() {}
84+
85+
Builder(DeprecationStatus<T> deprecationStatus) {
86+
this.deleted = deprecationStatus.deleted;
87+
this.deprecated = deprecationStatus.deprecated;
88+
this.obsolete = deprecationStatus.obsolete;
89+
this.replacement = deprecationStatus.replacement;
90+
this.status = deprecationStatus.status;
91+
}
92+
93+
/**
94+
* Sets the timestamp on or after which the deprecation state of this resource will be changed
95+
* to {@link Status#DELETED}. Timestamp should be in RFC3339 format.
96+
*
97+
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
98+
*/
99+
// todo(mziccard): remove this method if #732 is closed
100+
public Builder<T> deleted(String deleted) {
101+
this.deleted = deleted;
102+
return this;
103+
}
104+
105+
/**
106+
* Sets the timestamp on or after which the deprecation state of this resource will be changed
107+
* to {@link Status#DEPRECATED}. Timestamp should be in RFC3339 format.
108+
*
109+
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
110+
*/
111+
// todo(mziccard): remove this method if #732 is closed
112+
public Builder<T> deprecated(String deprecated) {
113+
this.deprecated = deprecated;
114+
return this;
115+
}
116+
117+
/**
118+
* Sets the timestamp on or after which the deprecation state of this resource will be changed
119+
* to {@link Status#OBSOLETE}. Timestamp should be in RFC3339 format.
120+
*
121+
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
122+
*/
123+
// todo(mziccard): remove this method if #732 is closed
124+
public Builder<T> obsolete(String obsolete) {
125+
this.obsolete = obsolete;
126+
return this;
127+
}
128+
129+
/**
130+
* Sets the timestamp on or after which the deprecation state of this resource will be changed
131+
* to {@link Status#DELETED}. In milliseconds since epoch.
132+
*/
133+
public Builder<T> deleted(long deleted) {
134+
this.deleted = TIMESTAMP_FORMATTER.print(deleted);
135+
return this;
136+
}
137+
138+
/**
139+
* Sets the timestamp on or after which the deprecation state of this resource will be changed
140+
* to {@link Status#DEPRECATED}. In milliseconds since epoch.
141+
*/
142+
public Builder<T> deprecated(long deprecated) {
143+
this.deprecated = TIMESTAMP_FORMATTER.print(deprecated);
144+
return this;
145+
}
146+
147+
/**
148+
* Sets the timestamp on or after which the deprecation state of this resource will be changed
149+
* to {@link Status#OBSOLETE}. In milliseconds since epoch.
150+
*/
151+
public Builder<T> obsolete(long obsolete) {
152+
this.obsolete = TIMESTAMP_FORMATTER.print(obsolete);
153+
return this;
154+
}
155+
156+
/**
157+
* Sets the identity of the suggested replacement for a deprecated resource. The suggested
158+
* replacement resource must be the same kind of resource as the deprecated resource.
159+
*/
160+
public Builder<T> replacement(T replacement) {
161+
this.replacement = replacement;
162+
return this;
163+
}
164+
165+
/**
166+
* Sets the status of the deprecated resource.
167+
*/
168+
public Builder<T> status(Status status) {
169+
this.status = checkNotNull(status);
170+
return this;
171+
}
172+
173+
/**
174+
* Creates a {@code DeprecationStatus} object.
175+
*/
176+
public DeprecationStatus<T> build() {
177+
return new DeprecationStatus<T>(this);
178+
}
179+
}
180+
181+
DeprecationStatus(Builder<T> builder) {
182+
this.deleted = builder.deleted;
183+
this.deprecated = builder.deprecated;
184+
this.obsolete = builder.obsolete;
185+
this.replacement = builder.replacement;
186+
this.status = checkNotNull(builder.status);
73187
}
74188

75189
/**
76190
* Returns the timestamp on or after which the deprecation state of this resource will be changed
77-
* to {@link Status#DELETED}. In milliseconds since epoch.
191+
* to {@link Status#DELETED}. Returns {@code null} if not set. This value should be in RFC3339
192+
* format.
193+
*
194+
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
78195
*/
79-
public Long deleted() {
196+
// todo(mziccard): remove this method if #732 is closed
197+
public String deleted() {
80198
return deleted;
81199
}
82200

83201
/**
84202
* Returns the timestamp on or after which the deprecation state of this resource will be changed
85-
* to {@link Status#DEPRECATED}. In milliseconds since epoch.
203+
* to {@link Status#DEPRECATED}. Returns {@code null} if not set. This value should be in RFC3339
204+
* format.
205+
*
206+
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
86207
*/
87-
public Long deprecated() {
208+
// todo(mziccard): remove this method if #732 is closed
209+
public String deprecated() {
88210
return deprecated;
89211
}
90212

91213
/**
92214
* Returns the timestamp on or after which the deprecation state of this resource will be changed
93-
* to {@link Status#OBSOLETE}. In milliseconds since epoch.
215+
* to {@link Status#OBSOLETE}. Returns {@code null} if not set. This value should be in RFC3339
216+
* format.
217+
*
218+
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
94219
*/
95-
public Long obsolete() {
220+
// todo(mziccard): remove this method if #732 is closed
221+
public String obsolete() {
96222
return obsolete;
97223
}
98224

225+
/**
226+
* Returns the timestamp (in milliseconds since epoch) on or after which the deprecation state of
227+
* this resource will be changed to {@link Status#DELETED}. Returns {@code null} if not set.
228+
*
229+
* @throws IllegalStateException if {@link #deleted()} is not a valid date, time or datetime
230+
*/
231+
public Long deletedMillis() {
232+
try {
233+
return deleted != null ? TIMESTAMP_PARSER.parseMillis(deleted) : null;
234+
} catch (IllegalArgumentException ex) {
235+
throw new IllegalStateException(ex.getMessage(), ex);
236+
}
237+
}
238+
239+
/**
240+
* Returns the timestamp (in milliseconds since epoch) on or after which the deprecation state of
241+
* this resource will be changed to {@link Status#DEPRECATED}. Returns {@code null} if not set.
242+
*
243+
* @throws IllegalStateException if {@link #deprecated()} is not a valid date, time or datetime
244+
*/
245+
public Long deprecatedMillis() {
246+
try {
247+
return deprecated != null ? TIMESTAMP_PARSER.parseMillis(deprecated) : null;
248+
} catch (IllegalArgumentException ex) {
249+
throw new IllegalStateException(ex.getMessage(), ex);
250+
}
251+
}
252+
253+
/**
254+
* Returns the timestamp (in milliseconds since epoch) on or after which the deprecation state of
255+
* this resource will be changed to {@link Status#OBSOLETE}. Returns {@code null} if not set.
256+
*
257+
* @throws IllegalStateException if {@link #obsolete()} is not a valid date, time or datetime
258+
*/
259+
public Long obsoleteMillis() {
260+
try {
261+
return obsolete != null ? TIMESTAMP_PARSER.parseMillis(obsolete) : null;
262+
} catch (IllegalArgumentException ex) {
263+
throw new IllegalStateException(ex.getMessage(), ex);
264+
}
265+
}
266+
99267
/**
100268
* Returns the identity of the suggested replacement for a deprecated resource. The suggested
101269
* replacement resource must be the same kind of resource as the deprecated resource.
@@ -111,6 +279,13 @@ public Status status() {
111279
return status;
112280
}
113281

282+
/**
283+
* Returns a builder for the {@code DeprecationStatus} object.
284+
*/
285+
public Builder<T> toBuilder() {
286+
return new Builder<>(this);
287+
}
288+
114289
@Override
115290
public String toString() {
116291
return MoreObjects.toStringHelper(this)
@@ -136,37 +311,49 @@ public boolean equals(Object obj) {
136311
com.google.api.services.compute.model.DeprecationStatus toPb() {
137312
com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb =
138313
new com.google.api.services.compute.model.DeprecationStatus();
139-
if (deleted != null) {
140-
deprecationStatusPb.setDeleted(TIMESTAMP_FORMATTER.print(deleted));
141-
}
142-
if (deprecated != null) {
143-
deprecationStatusPb.setDeprecated(TIMESTAMP_FORMATTER.print(deprecated));
144-
}
145-
if (obsolete != null) {
146-
deprecationStatusPb.setObsolete(TIMESTAMP_FORMATTER.print(obsolete));
147-
}
148-
if (replacement != null) {
149-
deprecationStatusPb.setReplacement(replacement.selfLink());
150-
}
151-
if (status() != null) {
152-
deprecationStatusPb.setState(status.name());
153-
}
314+
deprecationStatusPb.setDeleted(deleted);
315+
deprecationStatusPb.setDeprecated(deprecated);
316+
deprecationStatusPb.setObsolete(obsolete);
317+
deprecationStatusPb.setReplacement(replacement.selfLink());
318+
deprecationStatusPb.setState(status.name());
154319
return deprecationStatusPb;
155320
}
156321

322+
/**
323+
* Returns the builder for a {@code DeprecationStatus} object given the status.
324+
*/
325+
public static <T extends ResourceId> Builder<T> builder(Status status) {
326+
return new Builder<T>().status(status);
327+
}
328+
329+
/**
330+
* Returns the builder for a {@code DeprecationStatus} object given the status and replacement's
331+
* identity.
332+
*/
333+
public static <T extends ResourceId> Builder<T> builder(Status status, T replacement) {
334+
return new Builder<T>().status(status).replacement(replacement);
335+
}
336+
337+
/**
338+
* Returns a {@code DeprecationStatus} object given the status and replacement's identity.
339+
*/
340+
public static <T extends ResourceId> DeprecationStatus<T> of(Status status, T replacement) {
341+
return builder(status, replacement).build();
342+
}
343+
157344
static <T extends ResourceId> DeprecationStatus<T> fromPb(
158345
com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb,
159346
Function<String, T> fromUrl) {
160-
return new DeprecationStatus<>(
161-
deprecationStatusPb.getDeleted() != null
162-
? TIMESTAMP_FORMATTER.parseMillis(deprecationStatusPb.getDeleted()) : null,
163-
deprecationStatusPb.getDeprecated() != null
164-
? TIMESTAMP_FORMATTER.parseMillis(deprecationStatusPb.getDeprecated()) : null,
165-
deprecationStatusPb.getObsolete() != null
166-
? TIMESTAMP_FORMATTER.parseMillis(deprecationStatusPb.getObsolete()) : null,
167-
deprecationStatusPb.getReplacement() != null
168-
? fromUrl.apply(deprecationStatusPb.getReplacement()) : null,
169-
deprecationStatusPb.getState() != null
170-
? Status.valueOf(deprecationStatusPb.getState()) : null);
347+
Builder<T> builder = new Builder<>();
348+
builder.deleted(deprecationStatusPb.getDeleted());
349+
builder.deprecated(deprecationStatusPb.getDeprecated());
350+
builder.obsolete(deprecationStatusPb.getObsolete());
351+
if (deprecationStatusPb.getReplacement() != null) {
352+
builder.replacement(fromUrl.apply(deprecationStatusPb.getReplacement()));
353+
}
354+
if (deprecationStatusPb.getState() != null) {
355+
builder.status(Status.valueOf(deprecationStatusPb.getState()));
356+
}
357+
return builder.build();
171358
}
172359
}

0 commit comments

Comments
 (0)