1616
1717package com .google .gcloud .dns ;
1818
19+ import static com .google .common .base .Preconditions .checkNotNull ;
20+
21+ import com .google .api .services .dns .model .ResourceRecordSet ;
22+ import com .google .common .base .Function ;
1923import com .google .common .base .MoreObjects ;
24+ import com .google .common .collect .ImmutableList ;
2025import com .google .common .collect .Lists ;
2126
2227import org .joda .time .DateTime ;
2732import java .util .List ;
2833import java .util .Objects ;
2934
30- import static com .google .common .base .Preconditions .checkNotNull ;
31-
3235/**
33- * A class representing a change. A change is an atomic update to a collection of {@link DnsRecord}s
34- * within a {@code ManagedZone}.
36+ * A class representing an atomic update to a collection of {@link DnsRecord}s within a {@code
37+ * ManagedZone}.
3538 *
3639 * @see <a href="https://cloud.google.com/dns/api/v1/changes">Google Cloud DNS documentation</a>
3740 */
3841public class ChangeRequest implements Serializable {
3942
40- private static final long serialVersionUID = 201601251649L ;
43+ private static final Function <ResourceRecordSet , DnsRecord > FROM_PB_FUNCTION =
44+ new Function <com .google .api .services .dns .model .ResourceRecordSet , DnsRecord >() {
45+ @ Override
46+ public DnsRecord apply (com .google .api .services .dns .model .ResourceRecordSet pb ) {
47+ return DnsRecord .fromPb (pb );
48+ }
49+ };
50+ private static final Function <DnsRecord , ResourceRecordSet > TO_PB_FUNCTION =
51+ new Function <DnsRecord , ResourceRecordSet >() {
52+ @ Override
53+ public com .google .api .services .dns .model .ResourceRecordSet apply (DnsRecord error ) {
54+ return error .toPb ();
55+ }
56+ };
57+ private static final long serialVersionUID = -8703939628990291682L ;
4158 private final List <DnsRecord > additions ;
4259 private final List <DnsRecord > deletions ;
4360 private final String id ;
@@ -51,24 +68,8 @@ public class ChangeRequest implements Serializable {
5168 * documentation</a>
5269 */
5370 public enum Status {
54- PENDING ("pending" ),
55- DONE ("done" );
56-
57- private final String status ;
58-
59- Status (String status ) {
60- this .status = status ;
61- }
62-
63- static Status translate (String status ) {
64- if ("pending" .equals (status )) {
65- return PENDING ;
66- } else if ("done" .equals (status )) {
67- return DONE ;
68- } else {
69- throw new IllegalArgumentException ("Such a status is unknown." );
70- }
71- }
71+ PENDING ,
72+ DONE
7273 }
7374
7475 /**
@@ -101,18 +102,27 @@ public Builder additions(List<DnsRecord> additions) {
101102 this .additions = Lists .newLinkedList (checkNotNull (additions ));
102103 return this ;
103104 }
105+
106+ /**
107+ * Sets a collection of {@link DnsRecord}s which are to be deleted from the zone upon executing
108+ * this {@code ChangeRequest}.
109+ */
110+ public Builder deletions (List <DnsRecord > deletions ) {
111+ this .deletions = Lists .newLinkedList (checkNotNull (deletions ));
112+ return this ;
113+ }
104114
105115 /**
106- * Adds a {@link DnsRecord} which to be <strong>added</strong> to the zone upon executing this
107- * {@code ChangeRequest}.
116+ * Adds a {@link DnsRecord} to be <strong>added</strong> to the zone upon executing this {@code
117+ * ChangeRequest}.
108118 */
109119 public Builder add (DnsRecord record ) {
110120 this .additions .add (checkNotNull (record ));
111121 return this ;
112122 }
113123
114124 /**
115- * Adds a {@link DnsRecord} which to be <strong>deleted</strong> to the zone upon executing this
125+ * Adds a {@link DnsRecord} to be <strong>deleted</strong> to the zone upon executing this
116126 * {@code ChangeRequest}.
117127 */
118128 public Builder delete (DnsRecord record ) {
@@ -139,7 +149,7 @@ public Builder clearDeletions() {
139149 }
140150
141151 /**
142- * Removes a single {@link DnsRecord} from the collection of of records to be
152+ * Removes a single {@link DnsRecord} from the collection of records to be
143153 * <strong>added</strong> to the zone upon executing this {@code ChangeRequest}.
144154 */
145155 public Builder removeAddition (DnsRecord record ) {
@@ -148,23 +158,14 @@ public Builder removeAddition(DnsRecord record) {
148158 }
149159
150160 /**
151- * Removes a single {@link DnsRecord} from the collection of of records to be
161+ * Removes a single {@link DnsRecord} from the collection of records to be
152162 * <strong>deleted</strong> from the zone upon executing this {@code ChangeRequest}.
153163 */
154164 public Builder removeDeletion (DnsRecord record ) {
155165 this .deletions .remove (record );
156166 return this ;
157167 }
158168
159- /**
160- * Sets a collection of {@link DnsRecord}s which are to be deleted from the zone upon executing
161- * this {@code ChangeRequest}.
162- */
163- public Builder deletions (List <DnsRecord > deletions ) {
164- this .deletions = Lists .newLinkedList (checkNotNull (deletions ));
165- return this ;
166- }
167-
168169 /**
169170 * Associates a server-assigned id to this {@code ChangeRequest}.
170171 */
@@ -199,8 +200,8 @@ public ChangeRequest build() {
199200 }
200201
201202 private ChangeRequest (Builder builder ) {
202- this .additions = builder .additions ;
203- this .deletions = builder .deletions ;
203+ this .additions = ImmutableList . copyOf ( builder .additions ) ;
204+ this .deletions = ImmutableList . copyOf ( builder .deletions ) ;
204205 this .id = builder .id ;
205206 this .startTimeMillis = builder .startTimeMillis ;
206207 this .status = builder .status ;
@@ -221,15 +222,15 @@ public Builder toBuilder() {
221222 }
222223
223224 /**
224- * Returns the list of {@link DnsRecord}s to be added to the zone upon executing this {@code
225+ * Returns the list of {@link DnsRecord}s to be added to the zone upon submitting this {@code
225226 * ChangeRequest}.
226227 */
227228 public List <DnsRecord > additions () {
228229 return additions ;
229230 }
230231
231232 /**
232- * Returns the list of {@link DnsRecord}s to be deleted from the zone upon executing this {@code
233+ * Returns the list of {@link DnsRecord}s to be deleted from the zone upon submitting this {@code
233234 * ChangeRequest}.
234235 */
235236 public List <DnsRecord > deletions () {
@@ -270,56 +271,39 @@ com.google.api.services.dns.model.Change toPb() {
270271 }
271272 // set status
272273 if (status () != null ) {
273- pb .setStatus (status ().status );
274+ pb .setStatus (status ().name (). toLowerCase () );
274275 }
275276 // set a list of additions
276- if (additions () != null ) {
277- LinkedList <com .google .api .services .dns .model .ResourceRecordSet > additionsPb =
278- new LinkedList <>();
279- for (DnsRecord addition : additions ()) {
280- additionsPb .add (addition .toPb ());
281- }
282- pb .setAdditions (additionsPb );
283- }
277+ pb .setAdditions (Lists .transform (additions (), TO_PB_FUNCTION ));
284278 // set a list of deletions
285- if (deletions () != null ) {
286- LinkedList <com .google .api .services .dns .model .ResourceRecordSet > deletionsPb =
287- new LinkedList <>();
288- for (DnsRecord deletion : deletions ()) {
289- deletionsPb .add (deletion .toPb ());
290- }
291- pb .setDeletions (deletionsPb );
292- }
279+ pb .setDeletions (Lists .transform (deletions (), TO_PB_FUNCTION ));
293280 return pb ;
294281 }
295282
296283 static ChangeRequest fromPb (com .google .api .services .dns .model .Change pb ) {
297- Builder b = builder ();
284+ Builder builder = builder ();
298285 if (pb .getId () != null ) {
299- b .id (pb .getId ());
286+ builder .id (pb .getId ());
300287 }
301288 if (pb .getStartTime () != null ) {
302- b .startTimeMillis (DateTime .parse (pb .getStartTime ()).getMillis ());
289+ builder .startTimeMillis (DateTime .parse (pb .getStartTime ()).getMillis ());
303290 }
304291 if (pb .getStatus () != null ) {
305- b .status (ChangeRequest .Status .translate (pb .getStatus ()));
292+ // we are assuming that status indicated in pb is a lower case version of the enum name
293+ builder .status (ChangeRequest .Status .valueOf (pb .getStatus ().toUpperCase ()));
306294 }
307295 if (pb .getDeletions () != null ) {
308- for (com .google .api .services .dns .model .ResourceRecordSet deletion : pb .getDeletions ()) {
309- b .delete (DnsRecord .fromPb (deletion ));
310- }
296+ builder .deletions (Lists .transform (pb .getDeletions (), FROM_PB_FUNCTION ));
311297 }
312298 if (pb .getAdditions () != null ) {
313- for (com .google .api .services .dns .model .ResourceRecordSet addition : pb .getAdditions ()) {
314- b .add (DnsRecord .fromPb (addition ));
315- }
299+ builder .additions (Lists .transform (pb .getAdditions (), FROM_PB_FUNCTION ));
316300 }
317- return b .build ();
301+ return builder .build ();
318302 }
319303
320304 @ Override
321- public boolean equals (Object o ) {
322- return (o instanceof ChangeRequest ) && this . toPb ().equals (((ChangeRequest ) o ).toPb ());
305+ public boolean equals (Object other ) {
306+ return (other instanceof ChangeRequest ) && toPb ().equals (((ChangeRequest ) other ).toPb ());
323307 }
324308
325309 @ Override
0 commit comments