1616
1717package com .google .gcloud .dns ;
1818
19- import static com .google .common .base .Preconditions .checkNotNull ;
20-
2119import com .google .api .services .dns .model .Change ;
2220import com .google .common .base .Function ;
23- import com .google .common .base .MoreObjects ;
24- import com .google .common .collect .ImmutableList ;
25- import com .google .common .collect .Lists ;
26-
27- import org .joda .time .DateTime ;
28- import org .joda .time .format .ISODateTimeFormat ;
2921
30- import java .io .Serializable ;
31- import java .util . LinkedList ;
22+ import java .io .IOException ;
23+ import java .io . ObjectInputStream ;
3224import java .util .List ;
3325import java .util .Objects ;
3426
3830 *
3931 * @see <a href="https://cloud.google.com/dns/api/v1/changes">Google Cloud DNS documentation</a>
4032 */
41- public class ChangeRequest implements Serializable {
33+ public class ChangeRequest extends ChangeRequestInfo {
4234
43- static final Function <Change , ChangeRequest > FROM_PB_FUNCTION =
44- new Function <Change , ChangeRequest >() {
45- @ Override
46- public ChangeRequest apply (com .google .api .services .dns .model .Change pb ) {
47- return ChangeRequest .fromPb (pb );
48- }
49- };
5035 private static final long serialVersionUID = -9027378042756366333L ;
51- private final List <RecordSet > additions ;
52- private final List <RecordSet > deletions ;
53- private final String id ;
54- private final Long startTimeMillis ;
55- private final Status status ;
36+ private final DnsOptions options ;
37+ private final String zoneName ;
38+ private transient Dns dns ;
5639
5740 /**
5841 * This enumerates the possible states of a {@code ChangeRequest}.
@@ -68,250 +51,152 @@ public enum Status {
6851 /**
6952 * A builder for {@code ChangeRequest}s.
7053 */
71- public static class Builder {
54+ public static class Builder extends ChangeRequestInfo . Builder {
7255
73- private List <RecordSet > additions = new LinkedList <>();
74- private List <RecordSet > deletions = new LinkedList <>();
75- private String id ;
76- private Long startTimeMillis ;
77- private Status status ;
56+ private final Dns dns ;
57+ private final String zoneName ;
58+ private final ChangeRequestInfo .BuilderImpl infoBuilder ;
7859
7960 private Builder (ChangeRequest cr ) {
80- this .additions = Lists .newLinkedList (cr .additions ());
81- this .deletions = Lists .newLinkedList (cr .deletions ());
82- this .id = cr .id ();
83- this .startTimeMillis = cr .startTimeMillis ();
84- this .status = cr .status ();
61+ this .dns = cr .dns ;
62+ this .zoneName = cr .zoneName ;
63+ this .infoBuilder = new ChangeRequestInfo .BuilderImpl (cr );
8564 }
8665
87- private Builder () {
88- }
89-
90- /**
91- * Sets a collection of {@link RecordSet}s which are to be added to the zone upon executing this
92- * {@code ChangeRequest}.
93- */
66+ @ Override
9467 public Builder additions (List <RecordSet > additions ) {
95- this .additions = Lists . newLinkedList ( checkNotNull ( additions ) );
68+ infoBuilder .additions ( additions );
9669 return this ;
9770 }
9871
99- /**
100- * Sets a collection of {@link RecordSet}s which are to be deleted from the zone upon executing
101- * this {@code ChangeRequest}.
102- */
72+ @ Override
10373 public Builder deletions (List <RecordSet > deletions ) {
104- this .deletions = Lists . newLinkedList ( checkNotNull ( deletions ) );
74+ infoBuilder .deletions ( deletions );
10575 return this ;
10676 }
10777
108- /**
109- * Adds a {@link RecordSet} to be <strong>added</strong> to the zone upon executing this {@code
110- * ChangeRequest}.
111- */
78+ @ Override
11279 public Builder add (RecordSet recordSet ) {
113- this . additions . add (checkNotNull ( recordSet ) );
80+ infoBuilder . add (recordSet );
11481 return this ;
11582 }
11683
117- /**
118- * Adds a {@link RecordSet} to be <strong>deleted</strong> to the zone upon executing this
119- * {@code ChangeRequest}.
120- */
84+ @ Override
12185 public Builder delete (RecordSet recordSet ) {
122- this . deletions . add ( checkNotNull ( recordSet ) );
86+ infoBuilder . delete ( recordSet );
12387 return this ;
12488 }
12589
126- /**
127- * Clears the collection of {@link RecordSet}s which are to be added to the zone upon executing
128- * this {@code ChangeRequest}.
129- */
90+ @ Override
13091 public Builder clearAdditions () {
131- this . additions . clear ();
92+ infoBuilder . clearAdditions ();
13293 return this ;
13394 }
13495
135- /**
136- * Clears the collection of {@link RecordSet}s which are to be deleted from the zone upon
137- * executing this {@code ChangeRequest}.
138- */
96+ @ Override
13997 public Builder clearDeletions () {
140- this . deletions . clear ();
98+ infoBuilder . clearDeletions ();
14199 return this ;
142100 }
143101
144- /**
145- * Removes a single {@link RecordSet} from the collection of records to be
146- * <strong>added</strong> to the zone upon executing this {@code ChangeRequest}.
147- */
102+ @ Override
148103 public Builder removeAddition (RecordSet recordSet ) {
149- this . additions . remove (recordSet );
104+ infoBuilder . removeAddition (recordSet );
150105 return this ;
151106 }
152107
153- /**
154- * Removes a single {@link RecordSet} from the collection of records to be
155- * <strong>deleted</strong> from the zone upon executing this {@code ChangeRequest}.
156- */
108+ @ Override
157109 public Builder removeDeletion (RecordSet recordSet ) {
158- this . deletions . remove (recordSet );
110+ infoBuilder . removeDeletion (recordSet );
159111 return this ;
160112 }
161113
162- /**
163- * Associates a server-assigned id to this {@code ChangeRequest}.
164- */
114+ @ Override
165115 Builder id (String id ) {
166- this .id = checkNotNull (id );
116+ infoBuilder .id (id );
167117 return this ;
168118 }
169119
170- /**
171- * Sets the time when this {@code ChangeRequest} was started by a server.
172- */
120+ @ Override
173121 Builder startTimeMillis (long startTimeMillis ) {
174- this .startTimeMillis = startTimeMillis ;
122+ infoBuilder .startTimeMillis ( startTimeMillis ) ;
175123 return this ;
176124 }
177125
178- /**
179- * Sets the current status of this {@code ChangeRequest}.
180- */
126+ @ Override
181127 Builder status (Status status ) {
182- this .status = checkNotNull (status );
128+ infoBuilder .status (status );
183129 return this ;
184130 }
185131
186- /**
187- * Creates a {@code ChangeRequest} instance populated by the values associated with this
188- * builder.
189- */
132+ @ Override
190133 public ChangeRequest build () {
191- return new ChangeRequest (this );
134+ return new ChangeRequest (dns , zoneName , infoBuilder );
192135 }
193136 }
194137
195- private ChangeRequest (Builder builder ) {
196- this .additions = ImmutableList .copyOf (builder .additions );
197- this .deletions = ImmutableList .copyOf (builder .deletions );
198- this .id = builder .id ;
199- this .startTimeMillis = builder .startTimeMillis ;
200- this .status = builder .status ;
201- }
202-
203- /**
204- * Returns an empty builder for the {@code ChangeRequest} class.
205- */
206- public static Builder builder () {
207- return new Builder ();
208- }
209-
210- /**
211- * Creates a builder populated with values of this {@code ChangeRequest}.
212- */
213- public Builder toBuilder () {
214- return new Builder (this );
215- }
216-
217- /**
218- * Returns the list of {@link RecordSet}s to be added to the zone upon submitting this {@code
219- * ChangeRequest}.
220- */
221- public List <RecordSet > additions () {
222- return additions ;
138+ ChangeRequest (Dns dns , String zoneName , ChangeRequest .BuilderImpl infoBuilder ) {
139+ super (infoBuilder );
140+ this .zoneName = zoneName ;
141+ this .dns = dns ;
142+ this .options = dns .options ();
223143 }
224144
225- /**
226- * Returns the list of {@link RecordSet}s to be deleted from the zone upon submitting this {@code
227- * ChangeRequest}.
228- */
229- public List <RecordSet > deletions () {
230- return deletions ;
145+ static Function <Change , ChangeRequest > fromPbFunction (final Dns dns , final String zoneName ) {
146+ return new Function <Change , ChangeRequest >() {
147+ @ Override
148+ public ChangeRequest apply (com .google .api .services .dns .model .Change pb ) {
149+ return ChangeRequest .fromPb (dns , zoneName , pb );
150+ }
151+ };
231152 }
232153
233154 /**
234- * Returns the id assigned to this {@code ChangeRequest} by the server .
155+ * Returns the name of the {@link Zone} associated with this change request .
235156 */
236- public String id () {
237- return id ;
157+ public String zoneName () {
158+ return this . zoneName ;
238159 }
239160
240161 /**
241- * Returns the time when this {@code ChangeRequest} was started by the server .
162+ * Returns the {@link Dns} service object associated with this change request .
242163 */
243- public Long startTimeMillis () {
244- return startTimeMillis ;
164+ public Dns dns () {
165+ return dns ;
245166 }
246167
247168 /**
248- * Returns the status of this {@code ChangeRequest} .
169+ * Applies this change request to a zone that it is associated with .
249170 */
250- public Status status ( ) {
251- return status ;
171+ public ChangeRequest applyTo ( Dns . ChangeRequestOption ... options ) {
172+ return dns . applyChangeRequest ( zoneName , this , options ) ;
252173 }
253174
254- com .google .api .services .dns .model .Change toPb () {
255- com .google .api .services .dns .model .Change pb =
256- new com .google .api .services .dns .model .Change ();
257- // set id
258- if (id () != null ) {
259- pb .setId (id ());
260- }
261- // set timestamp
262- if (startTimeMillis () != null ) {
263- pb .setStartTime (ISODateTimeFormat .dateTime ().withZoneUTC ().print (startTimeMillis ()));
264- }
265- // set status
266- if (status () != null ) {
267- pb .setStatus (status ().name ().toLowerCase ());
268- }
269- // set a list of additions
270- pb .setAdditions (Lists .transform (additions (), RecordSet .TO_PB_FUNCTION ));
271- // set a list of deletions
272- pb .setDeletions (Lists .transform (deletions (), RecordSet .TO_PB_FUNCTION ));
273- return pb ;
274- }
275-
276- static ChangeRequest fromPb (com .google .api .services .dns .model .Change pb ) {
277- Builder builder = builder ();
278- if (pb .getId () != null ) {
279- builder .id (pb .getId ());
280- }
281- if (pb .getStartTime () != null ) {
282- builder .startTimeMillis (DateTime .parse (pb .getStartTime ()).getMillis ());
283- }
284- if (pb .getStatus () != null ) {
285- // we are assuming that status indicated in pb is a lower case version of the enum name
286- builder .status (ChangeRequest .Status .valueOf (pb .getStatus ().toUpperCase ()));
287- }
288- if (pb .getDeletions () != null ) {
289- builder .deletions (Lists .transform (pb .getDeletions (), RecordSet .FROM_PB_FUNCTION ));
290- }
291- if (pb .getAdditions () != null ) {
292- builder .additions (Lists .transform (pb .getAdditions (), RecordSet .FROM_PB_FUNCTION ));
293- }
294- return builder .build ();
175+ @ Override
176+ public Builder toBuilder () {
177+ return new Builder (this );
295178 }
296179
297180 @ Override
298- public boolean equals (Object other ) {
299- return (other instanceof ChangeRequest ) && toPb ().equals (((ChangeRequest ) other ).toPb ());
181+ public boolean equals (Object obj ) {
182+ return obj instanceof ChangeRequest && Objects .equals (toPb (), ((ChangeRequest ) obj ).toPb ())
183+ && Objects .equals (options , ((ChangeRequest ) obj ).options )
184+ && Objects .equals (zoneName , ((ChangeRequest ) obj ).zoneName );
300185 }
301186
302187 @ Override
303188 public int hashCode () {
304- return Objects .hash (additions , deletions , id , startTimeMillis , status );
189+ return Objects .hash (super . hashCode (), options , zoneName );
305190 }
306191
307- @ Override
308- public String toString () {
309- return MoreObjects . toStringHelper ( this )
310- . add ( "additions" , additions )
311- . add ( "deletions" , deletions )
312- . add ( "id" , id )
313- . add ( "startTimeMillis" , startTimeMillis )
314- . add ( "status" , status )
315- . toString ( );
192+ private void readObject ( ObjectInputStream in ) throws IOException , ClassNotFoundException {
193+ in . defaultReadObject ();
194+ this . dns = options . service ();
195+ }
196+
197+ static ChangeRequest fromPb ( Dns dns , String zoneName ,
198+ com . google . api . services . dns . model . Change pb ) {
199+ ChangeRequestInfo info = ChangeRequestInfo . fromPb ( pb );
200+ return new ChangeRequest ( dns , zoneName , new ChangeRequestInfo . BuilderImpl ( info ) );
316201 }
317202}
0 commit comments