1
2
3
4
5 package org.apache.maven.api.model;
6
7 import java.io.Serializable;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.Map;
11 import java.util.Objects;
12 import java.util.Optional;
13 import java.util.Set;
14 import java.util.stream.Collectors;
15 import java.util.stream.Stream;
16 import org.apache.maven.api.annotations.Experimental;
17 import org.apache.maven.api.annotations.Generated;
18 import org.apache.maven.api.annotations.Immutable;
19 import org.apache.maven.api.annotations.Nonnull;
20 import org.apache.maven.api.annotations.NotThreadSafe;
21 import org.apache.maven.api.annotations.ThreadSafe;
22
23
24
25
26 @Experimental
27 @Generated @ThreadSafe @Immutable
28 public class Organization
29 implements Serializable, InputLocationTracker
30 {
31
32
33
34 final String name;
35
36
37
38 final String url;
39
40 final Map<Object, InputLocation> locations;
41
42 final InputLocation importedFrom;
43
44
45
46
47
48 protected Organization(Builder builder) {
49 this.name = builder.name != null ? builder.name : (builder.base != null ? builder.base.name : null);
50 this.url = builder.url != null ? builder.url : (builder.base != null ? builder.base.url : null);
51 this.locations = builder.computeLocations();
52 this.importedFrom = builder.importedFrom;
53 }
54
55
56
57
58
59
60 public String getName() {
61 return this.name;
62 }
63
64
65
66
67
68
69 public String getUrl() {
70 return this.url;
71 }
72
73
74
75
76
77
78
79
80 public InputLocation getLocation(Object key) {
81 Objects.requireNonNull(key, "key");
82 return locations.get(key);
83 }
84
85
86
87
88 public Set<Object> getLocationKeys() {
89 return locations.keySet();
90 }
91
92 protected Stream<Object> getLocationKeyStream() {
93 return locations.keySet().stream();
94 }
95
96
97
98
99 public InputLocation getImportedFrom() {
100 return importedFrom;
101 }
102
103
104
105
106
107
108 @Nonnull
109 public Builder with() {
110 return newBuilder(this);
111 }
112
113
114
115
116
117
118 @Nonnull
119 public Organization withName(String name) {
120 return newBuilder(this, true).name(name).build();
121 }
122
123
124
125
126
127
128 @Nonnull
129 public Organization withUrl(String url) {
130 return newBuilder(this, true).url(url).build();
131 }
132
133
134
135
136
137
138
139
140 @Nonnull
141 public static Organization newInstance() {
142 return newInstance(true);
143 }
144
145
146
147
148
149
150
151
152 @Nonnull
153 public static Organization newInstance(boolean withDefaults) {
154 return newBuilder(withDefaults).build();
155 }
156
157
158
159
160
161
162
163
164 @Nonnull
165 public static Builder newBuilder() {
166 return newBuilder(true);
167 }
168
169
170
171
172
173
174
175 @Nonnull
176 public static Builder newBuilder(boolean withDefaults) {
177 return new Builder(withDefaults);
178 }
179
180
181
182
183
184
185
186
187 @Nonnull
188 public static Builder newBuilder(Organization from) {
189 return newBuilder(from, false);
190 }
191
192
193
194
195
196
197
198
199 @Nonnull
200 public static Builder newBuilder(Organization from, boolean forceCopy) {
201 return new Builder(from, forceCopy);
202 }
203
204
205
206
207
208
209 @NotThreadSafe
210 public static class Builder
211 {
212 Organization base;
213 String name;
214 String url;
215 Map<Object, InputLocation> locations;
216 InputLocation importedFrom;
217
218 protected Builder(boolean withDefaults) {
219 if (withDefaults) {
220 }
221 }
222
223 protected Builder(Organization base, boolean forceCopy) {
224 if (forceCopy) {
225 this.name = base.name;
226 this.url = base.url;
227 this.locations = base.locations;
228 this.importedFrom = base.importedFrom;
229 } else {
230 this.base = base;
231 }
232 }
233
234 @Nonnull
235 public Builder name(String name) {
236 this.name = name;
237 return this;
238 }
239
240 @Nonnull
241 public Builder url(String url) {
242 this.url = url;
243 return this;
244 }
245
246
247 @Nonnull
248 public Builder location(Object key, InputLocation location) {
249 if (location != null) {
250 if (!(this.locations instanceof HashMap)) {
251 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
252 }
253 this.locations.put(key, location);
254 }
255 return this;
256 }
257
258 @Nonnull
259 public Builder importedFrom(InputLocation importedFrom) {
260 this.importedFrom = importedFrom;
261 return this;
262 }
263
264 @Nonnull
265 public Organization build() {
266
267 if (base != null
268 && (name == null || name == base.name)
269 && (url == null || url == base.url)
270 ) {
271 return base;
272 }
273 return new Organization(this);
274 }
275
276 Map<Object, InputLocation> computeLocations() {
277 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
278 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
279 if (newlocs.isEmpty()) {
280 return Map.copyOf(oldlocs);
281 }
282 if (oldlocs.isEmpty()) {
283 return Map.copyOf(newlocs);
284 }
285 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
286
287 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
288 }
289 }
290
291
292
293
294
295
296 public String toString()
297 {
298 return "Organization {name=" + getName() + ", url=" + getUrl() + "}";
299 }
300
301
302 }