/
CeTask.java
274 lines (234 loc) · 7.1 KB
/
CeTask.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.ce.task;
import com.google.common.base.MoreObjects;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.emptyToNull;
import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableMap;
import static java.util.Objects.requireNonNull;
@Immutable
public class CeTask {
private final String organizationUuid;
private final String type;
private final String uuid;
private final Component component;
private final Component mainComponent;
private final User submitter;
private final Map<String, String> characteristics;
private CeTask(Builder builder) {
this.organizationUuid = requireNonNull(emptyToNull(builder.organizationUuid), "organizationUuid can't be null nor empty");
this.uuid = requireNonNull(emptyToNull(builder.uuid), "uuid can't be null nor empty");
this.type = requireNonNull(emptyToNull(builder.type), "type can't be null nor empty");
checkArgument((builder.component == null) == (builder.mainComponent == null),
"None or both component and main component must be non null");
this.component = builder.component;
this.mainComponent = builder.mainComponent;
this.submitter = builder.submitter;
if (builder.characteristics == null) {
this.characteristics = emptyMap();
} else {
this.characteristics = unmodifiableMap(new HashMap<>(builder.characteristics));
}
}
@Immutable
public static final class User {
private final String uuid;
private final String login;
public User(String uuid, @Nullable String login) {
this.uuid = requireNonNull(uuid);
this.login = emptyToNull(login);
}
public String getUuid() {
return uuid;
}
@CheckForNull
public String getLogin() {
return login;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User other = (User) o;
return uuid.equals(other.uuid);
}
@Override
public String toString() {
return "User{" +
"uuid='" + uuid + '\'' +
", login='" + login + '\'' +
'}';
}
@Override
public int hashCode() {
return uuid.hashCode();
}
}
public String getOrganizationUuid() {
return organizationUuid;
}
public String getUuid() {
return uuid;
}
public String getType() {
return type;
}
public Optional<Component> getComponent() {
return Optional.ofNullable(component);
}
public Optional<Component> getMainComponent() {
return Optional.ofNullable(mainComponent);
}
@CheckForNull
public User getSubmitter() {
return submitter;
}
public Map<String, String> getCharacteristics() {
return characteristics;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("organizationUuid", organizationUuid)
.add("type", type)
.add("uuid", uuid)
.add("component", component)
.add("mainComponent", mainComponent)
.add("submitter", submitter)
.toString();
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CeTask ceTask = (CeTask) o;
return uuid.equals(ceTask.uuid);
}
@Override
public int hashCode() {
return uuid.hashCode();
}
public static final class Builder {
private String organizationUuid;
private String uuid;
private String type;
private Component component;
private Component mainComponent;
private User submitter;
private Map<String, String> characteristics;
public Builder setOrganizationUuid(String organizationUuid) {
this.organizationUuid = organizationUuid;
return this;
}
// FIXME remove this method when organization support is added to the Compute Engine queue
public boolean hasOrganizationUuid() {
return organizationUuid != null;
}
public Builder setUuid(String uuid) {
this.uuid = uuid;
return this;
}
public Builder setType(String type) {
this.type = type;
return this;
}
public Builder setComponent(@Nullable Component component) {
this.component = component;
return this;
}
public Builder setMainComponent(@Nullable Component mainComponent) {
this.mainComponent = mainComponent;
return this;
}
public Builder setSubmitter(@Nullable User s) {
this.submitter = s;
return this;
}
public Builder setCharacteristics(@Nullable Map<String, String> m) {
this.characteristics = m;
return this;
}
public CeTask build() {
return new CeTask(this);
}
}
public static final class Component {
private final String uuid;
@CheckForNull
private final String key;
@CheckForNull
private final String name;
public Component(String uuid, @Nullable String key, @Nullable String name) {
this.uuid = requireNonNull(emptyToNull(uuid), "uuid can't be null nor empty");
this.key = emptyToNull(key);
this.name = emptyToNull(name);
}
public String getUuid() {
return uuid;
}
public Optional<String> getKey() {
return Optional.ofNullable(key);
}
public Optional<String> getName() {
return Optional.ofNullable(name);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Component component = (Component) o;
return Objects.equals(uuid, component.uuid) &&
Objects.equals(key, component.key) &&
Objects.equals(name, component.name);
}
@Override
public int hashCode() {
return Objects.hash(uuid, key, name);
}
@Override
public String toString() {
return "Component{" +
"uuid='" + uuid + '\'' +
", key='" + key + '\'' +
", name='" + name + '\'' +
'}';
}
}
}