1
1
package org .kohsuke .github ;
2
2
3
3
import com .fasterxml .jackson .annotation .JacksonInject ;
4
- import com .fasterxml .jackson .annotation .JsonCreator ;
5
4
6
5
import java .io .IOException ;
7
6
import java .util .ArrayList ;
8
7
import java .util .Collection ;
9
8
import java .util .List ;
10
9
import java .util .Objects ;
11
- import java .util .function .Consumer ;
12
10
13
11
import javax .annotation .Nonnull ;
14
12
@@ -68,11 +66,11 @@ public String getDescription() {
68
66
* 6-letter hex color code, like "f29513"
69
67
* @throws IOException
70
68
* the io exception
71
- * @deprecated use {@link #update(Consumer )} instead
69
+ * @deprecated use {@link #set( )} instead
72
70
*/
73
71
@ Deprecated
74
72
public void setColor (String newColor ) throws IOException {
75
- this .update ( i -> i .color (newColor ) );
73
+ this .set () .color (newColor );
76
74
}
77
75
78
76
/**
@@ -82,11 +80,11 @@ public void setColor(String newColor) throws IOException {
82
80
* Description of label
83
81
* @throws IOException
84
82
* the io exception
85
- * @deprecated use {@link #update(Consumer )} instead
83
+ * @deprecated use {@link #set( )} instead
86
84
*/
87
85
@ Deprecated
88
86
public void setDescription (String newDescription ) throws IOException {
89
- this .update ( i -> i .description (newDescription ) );
87
+ this .set () .description (newDescription );
90
88
}
91
89
92
90
static Collection <String > toNames (Collection <GHLabel > labels ) {
@@ -99,16 +97,20 @@ static Collection<String> toNames(Collection<GHLabel> labels) {
99
97
100
98
// NEW IMPLEMENTATION STARTS HERE
101
99
100
+ @ JacksonInject
102
101
@ Nonnull
103
- private final GitHub root ;
102
+ protected final GitHub root ;
104
103
105
104
@ Nonnull
106
105
private final String url , name , color , description ;
107
106
108
- private GHRepository repository ;
107
+ protected GHRepository repository ;
109
108
110
- @ JsonCreator (mode = JsonCreator .Mode .DELEGATING )
111
- private GHLabel (@ Nonnull Builder builder ) {
109
+ GHLabel () {
110
+ this (new GHLabelBuilder ());
111
+ }
112
+
113
+ private GHLabel (@ Nonnull GHLabelBuilder builder ) {
112
114
this .root = builder .root ;
113
115
this .url = builder .url ;
114
116
this .name = builder .name ;
@@ -122,18 +124,8 @@ private GHLabel(@Nonnull Builder builder) {
122
124
* @throws IOException
123
125
* the io exception
124
126
*/
125
- public static GHLabel create (GHRepository repository , Consumer <Builder > initializer ) throws IOException {
126
- Builder builder = new Builder ();
127
- initializer .accept (builder );
128
- return repository .root .createRequest ()
129
- .withUrlPath (repository .getApiTailUrl ("labels" ))
130
- .method ("POST" )
131
- .with ("name" , builder .name )
132
- .with ("color" , builder .color )
133
- .with ("description" , builder .description )
134
- .fetch (GHLabel .class )
135
- .lateBind (repository );
136
-
127
+ public static Creator create (GHRepository repository ) throws IOException {
128
+ return new Creator (repository );
137
129
}
138
130
139
131
/**
@@ -212,18 +204,18 @@ GHLabel lateBind(GHRepository repo) {
212
204
* @throws IOException
213
205
* the io exception
214
206
*/
215
- public GHLabel update (Consumer < Builder > updater ) throws IOException {
216
- Builder builder = new Builder (this );
217
- updater . accept ( builder );
207
+ public BatchUpdater update () throws IOException {
208
+ return new BatchUpdater (this );
209
+ }
218
210
219
- return repository . root . createRequest ()
220
- . method ( "PATCH" )
221
- . with ( "name" , builder . name )
222
- . with ( "color" , builder . color )
223
- . with ( "description" , builder . description )
224
- . setRawUrlPath ( url )
225
- . fetch ( GHLabel . class )
226
- . lateBind ( repository );
211
+ /**
212
+ * Modifies a label in a repository.
213
+ *
214
+ * @throws IOException
215
+ * the io exception
216
+ */
217
+ public SingleUpdater set () throws IOException {
218
+ return new SingleUpdater ( this );
227
219
}
228
220
229
221
/**
@@ -253,41 +245,115 @@ public int hashCode() {
253
245
return Objects .hash (url , name , color , repository );
254
246
}
255
247
256
- public static class Builder {
248
+ public static class SingleUpdater extends Builder <GHLabel > {
249
+ private SingleUpdater (GHLabel base ) throws IOException {
250
+ super (base , true );
251
+ requester .method ("PATCH" ).setRawUrlPath (base .url ());
252
+ }
253
+ }
254
+
255
+ public static class BatchUpdater extends Builder <BatchUpdater > {
256
+ private BatchUpdater (GHLabel base ) throws IOException {
257
+ super (base , false );
258
+ requester .method ("PATCH" ).setRawUrlPath (base .url ());
259
+ }
260
+ }
261
+
262
+ public static class Creator extends Builder <Creator > {
263
+ private Creator (GHRepository repository ) throws IOException {
264
+ super (repository );
265
+ requester .method ("POST" ).withUrlPath (repository .getApiTailUrl ("labels" ));
266
+ }
267
+ }
268
+
269
+ public static class GHLabelBuilder {
257
270
private String url , name , color , description ;
258
271
259
272
@ JacksonInject
260
273
private GitHub root ;
261
274
262
- public Builder () {
275
+ public GHLabelBuilder () {
276
+ root = null ;
263
277
url = "" ;
264
278
name = "" ;
265
279
color = "" ;
266
280
description = "" ;
267
281
}
282
+ }
283
+
284
+ public static class Builder <U > extends BaseBuilder <GHLabel , U > {
285
+
286
+ final GHRepository repository ;
287
+
288
+ public Builder (GHLabel label , boolean immediate ) throws IOException {
289
+ super (label .root , GHLabel .class , label , immediate );
290
+ repository = label .repository ;
291
+ }
292
+
293
+ public Builder (GHRepository repository ) throws IOException {
294
+ super (repository .root , GHLabel .class , new GHLabel (new GHLabelBuilder ()), false );
295
+ this .repository = repository ;
296
+ }
297
+
298
+ public U name (String value ) throws IOException {
299
+ return with ("name" , value );
300
+ }
301
+
302
+ public U color (String value ) throws IOException {
303
+ return with ("color" , value );
304
+ }
268
305
269
- public Builder (GHLabel label ) {
270
- this .root = label .root ;
271
- // Url is maintained on the mutator but cannot be changed locally.
272
- url = label .url ();
273
- name = label .name ();
274
- color = label .color ();
275
- description = label .description ();
306
+ public U description (String value ) throws IOException {
307
+ return with ("description" , value );
276
308
}
277
309
278
- public Builder name (String value ) {
279
- name = value ;
280
- return this ;
310
+ @ Override
311
+ protected void initialize (GHLabel base ) throws IOException {
312
+ // Set initial values
313
+ name (base .name ());
314
+ color (base .color ());
315
+ description (base .description ());
281
316
}
282
317
283
- public Builder color ( String value ) {
284
- color = value ;
285
- return this ;
318
+ @ Override
319
+ public GHLabel done () throws IOException {
320
+ return requester . fetch ( returnType ). lateBind ( repository ) ;
286
321
}
322
+ }
323
+
324
+ /**
325
+ *
326
+ * @param <T>
327
+ * @param <U>
328
+ */
329
+ public abstract static class BaseBuilder <T , U > {
330
+
331
+ private final boolean initialized ;
332
+ private final boolean immediate ;
333
+ protected final Class <T > returnType ;
334
+ protected final Requester requester ;
335
+
336
+ protected BaseBuilder (GitHub root , Class <T > returnType , T base , boolean immediate ) throws IOException {
337
+ this .requester = root .createRequest ();
338
+ this .immediate = immediate ;
339
+ this .returnType = returnType ;
340
+ initialize (base );
341
+ this .initialized = true ;
342
+ }
343
+
344
+ public abstract T done () throws IOException ;
345
+
346
+ protected abstract void initialize (T base ) throws IOException ;
287
347
288
- public Builder description (String value ) {
289
- description = value ;
290
- return this ;
348
+ protected U with (String name , Object value ) throws IOException {
349
+ requester .with (name , value );
350
+ if (initialized ) {
351
+ if (immediate ) {
352
+ return (U ) done ();
353
+ }
354
+ return (U ) this ;
355
+ }
356
+ return null ;
291
357
}
292
358
}
293
359
}
0 commit comments