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.util.Collection;
36 import java.util.Collections;
37 import java.util.LinkedList;
38 import lombok.EqualsAndHashCode;
39 import lombok.ToString;
40
41
42
43
44
45
46
47
48
49 @Immutable
50 @SuppressWarnings("PMD.TooManyMethods")
51 public interface IssueLabels {
52
53
54
55
56
57 Issue issue();
58
59
60
61
62
63
64
65 void add(Iterable<String> labels) throws IOException;
66
67
68
69
70
71
72
73 void replace(Iterable<String> labels) throws IOException;
74
75
76
77
78
79
80 Iterable<Label> iterate();
81
82
83
84
85
86
87
88 void remove(String name) throws IOException;
89
90
91
92
93
94
95 void clear() throws IOException;
96
97
98
99
100 @Immutable
101 @ToString
102 @Loggable(Loggable.DEBUG)
103 @EqualsAndHashCode(of = "labels")
104 final class Smart implements IssueLabels {
105
106
107
108 private final transient IssueLabels labels;
109
110
111
112
113 public Smart(final IssueLabels lbl) {
114 this.labels = lbl;
115 }
116
117
118
119
120
121 public boolean contains(final String name) {
122 boolean contains = false;
123 for (final Label label : this.labels.iterate()) {
124 if (label.name().equals(name)) {
125 contains = true;
126 break;
127 }
128 }
129 return contains;
130 }
131
132
133
134
135
136
137 public Label get(final String name) {
138 Label label = null;
139 int count = 0;
140 for (final Label opt : this.labels.iterate()) {
141 if (opt.name().equals(name)) {
142 label = opt;
143 break;
144 }
145 ++count;
146 }
147 if (label == null) {
148 throw new IllegalArgumentException(
149 String.format(
150
151 "label '%s' not found among %d others, use #contains() first",
152 name, count
153 )
154 );
155 }
156 return label;
157 }
158
159
160
161
162
163
164 public boolean addIfAbsent(final String name) throws IOException {
165 final boolean added;
166 if (this.contains(name)) {
167 added = false;
168 } else {
169 new Labels.Smart(this.labels.issue().repo().labels())
170 .createOrGet(name);
171 this.labels.add(Collections.singletonList(name));
172 added = true;
173 }
174 return added;
175 }
176
177
178
179
180
181
182
183
184 public boolean addIfAbsent(
185 final String name, final String color
186 ) throws IOException {
187 Label label = null;
188 for (final Label opt : new Bulk<>(this.labels.iterate())) {
189 if (opt.name().equals(name)) {
190 label = opt;
191 break;
192 }
193 }
194 boolean added = false;
195 if (label == null) {
196 added = true;
197 label = new Labels.Smart(this.labels.issue().repo().labels())
198 .createOrGet(name, color);
199 this.labels.add(Collections.singletonList(name));
200 }
201 final Label.Smart smart = new Label.Smart(label);
202 if (!smart.color().equals(color)) {
203 smart.color(color);
204 }
205 return added;
206 }
207
208
209
210
211
212
213
214 public Collection<Label> findByColor(final String color)
215 throws IOException {
216 final Collection<Label> found = new LinkedList<>();
217 for (final Label label : this.labels.iterate()) {
218 if (new Label.Smart(label).color().equals(color)) {
219 found.add(label);
220 }
221 }
222 return found;
223 }
224
225
226
227
228
229
230
231 public boolean removeIfExists(final String name)
232 throws IOException {
233 boolean removed = false;
234 for (final Label label : this.labels.iterate()) {
235 if (label.name().equals(name)) {
236 this.remove(name);
237 removed = true;
238 break;
239 }
240 }
241 return removed;
242 }
243 @Override
244 public Issue issue() {
245 return this.labels.issue();
246 }
247 @Override
248 public void add(final Iterable<String> names) throws IOException {
249 this.labels.add(names);
250 }
251 @Override
252 public void replace(final Iterable<String> names) throws IOException {
253 this.labels.replace(names);
254 }
255 @Override
256 public Iterable<Label> iterate() {
257 return this.labels.iterate();
258 }
259 @Override
260 public void remove(final String name) throws IOException {
261 this.labels.remove(name);
262 }
263 @Override
264 public void clear() throws IOException {
265 this.labels.clear();
266 }
267 }
268 }