1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.github;
31
32 import com.jcabi.aspects.Immutable;
33 import com.jcabi.aspects.Loggable;
34 import java.io.IOException;
35 import java.net.URL;
36 import java.text.ParseException;
37 import java.util.Date;
38 import javax.json.Json;
39 import javax.json.JsonObject;
40 import lombok.EqualsAndHashCode;
41 import lombok.ToString;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 @Immutable
61 @SuppressWarnings("PMD.TooManyMethods")
62 public interface Milestone extends Comparable<Milestone>,
63 JsonReadable, JsonPatchable {
64
65
66
67
68 String OPEN_STATE = "open";
69
70
71
72
73 String CLOSED_STATE = "closed";
74
75
76
77
78
79 Repo repo();
80
81
82
83
84
85 int number();
86
87
88
89
90 @Immutable
91 @ToString
92 @Loggable(Loggable.DEBUG)
93 @EqualsAndHashCode(of = { "milestone", "jsn" })
94 final class Smart implements Milestone {
95
96
97
98
99 private static final String STATE = "state";
100
101
102
103
104 private static final String DESCRIPTION = "description";
105
106
107
108
109 private static final String TITLE = "title";
110
111
112
113
114 private static final String DUE_ON = "due_on";
115
116
117
118
119 private final transient Milestone milestone;
120
121
122
123
124 private final transient SmartJson jsn;
125
126
127
128
129
130 public Smart(
131 final Milestone mls
132 ) {
133 this.milestone = mls;
134 this.jsn = new SmartJson(mls);
135 }
136
137
138
139
140
141
142 public User creator() throws IOException {
143 return this.milestone.repo().github().users().get(
144 this.jsn.value(
145 "creator", JsonObject.class
146 ).getString("login")
147 );
148 }
149
150
151
152
153
154
155 public boolean isOpen() throws IOException {
156 return Milestone.OPEN_STATE.equals(this.state());
157 }
158
159
160
161
162
163 public void open() throws IOException {
164 this.state(Milestone.OPEN_STATE);
165 }
166
167
168
169
170
171 public void close() throws IOException {
172 this.state(Milestone.CLOSED_STATE);
173 }
174
175
176
177
178
179
180 public String state() throws IOException {
181 return this.jsn.text(STATE);
182 }
183
184
185
186
187
188
189 public void state(
190 final String state
191 ) throws IOException {
192 this.milestone.patch(
193 Json.createObjectBuilder().add(STATE, state).build()
194 );
195 }
196
197
198
199
200
201
202 public String title() throws IOException {
203 return this.jsn.text(TITLE);
204 }
205
206
207
208
209
210
211 public void title(
212 final String title
213 ) throws IOException {
214 this.milestone.patch(
215 Json.createObjectBuilder().add(TITLE, title).build()
216 );
217 }
218
219
220
221
222
223
224 public String description() throws IOException {
225 return this.jsn.text(DESCRIPTION);
226 }
227
228
229
230
231
232
233 public void description(
234 final String description
235 ) throws IOException {
236 this.milestone.patch(
237 Json.createObjectBuilder()
238 .add(DESCRIPTION, description).build()
239 );
240 }
241
242
243
244
245
246
247 public URL url() throws IOException {
248 return new URL(this.jsn.text("url"));
249 }
250
251
252
253
254
255
256 public Date createdAt() throws IOException {
257 try {
258 return new Github.Time(
259 this.jsn.text("created_at")
260 ).date();
261 } catch (final ParseException ex) {
262 throw new IllegalStateException(ex);
263 }
264 }
265
266
267
268
269
270
271 public Date dueOn() throws IOException {
272 try {
273 return new Github.Time(
274 this.jsn.text(DUE_ON)
275 ).date();
276 } catch (final ParseException ex) {
277 throw new IllegalStateException(ex);
278 }
279 }
280
281
282
283
284
285
286 public void dueOn(
287 final Date dueon
288 ) throws IOException {
289 this.milestone.patch(
290 Json.createObjectBuilder()
291 .add(DUE_ON, new Github.Time(dueon).toString()).build()
292 );
293 }
294
295
296
297
298
299
300 public int openIssues() throws IOException {
301 return this.jsn.number("open_issues");
302 }
303
304
305
306
307
308
309 public int closedIssues() throws IOException {
310 return this.jsn.number("closed_issues");
311 }
312
313 @Override
314 public Repo repo() {
315 return this.milestone.repo();
316 }
317
318 @Override
319 public int number() {
320 return this.milestone.number();
321 }
322
323 @Override
324 public JsonObject json() throws IOException {
325 return this.milestone.json();
326 }
327
328 @Override
329 public void patch(
330 final JsonObject json
331 ) throws IOException {
332 this.milestone.patch(json);
333 }
334
335 @Override
336 public int compareTo(
337 final Milestone obj
338 ) {
339 return this.milestone.compareTo(obj);
340 }
341
342 }
343 }