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 public InputLocation getLocation(Object key) {
77 return locations.get(key);
78 }
79
80
81
82
83 public Set<Object> getLocationKeys() {
84 return locations.keySet();
85 }
86
87 protected Stream<Object> getLocationKeyStream() {
88 return locations.keySet().stream();
89 }
90
91
92
93
94 public InputLocation getImportedFrom() {
95 return importedFrom;
96 }
97
98
99
100
101
102
103 @Nonnull
104 public Builder with() {
105 return newBuilder(this);
106 }
107
108
109
110
111
112
113 @Nonnull
114 public Organization withName(String name) {
115 return newBuilder(this, true).name(name).build();
116 }
117
118
119
120
121
122
123 @Nonnull
124 public Organization withUrl(String url) {
125 return newBuilder(this, true).url(url).build();
126 }
127
128
129
130
131
132
133
134
135 @Nonnull
136 public static Organization newInstance() {
137 return newInstance(true);
138 }
139
140
141
142
143
144
145
146
147 @Nonnull
148 public static Organization newInstance(boolean withDefaults) {
149 return newBuilder(withDefaults).build();
150 }
151
152
153
154
155
156
157
158
159 @Nonnull
160 public static Builder newBuilder() {
161 return newBuilder(true);
162 }
163
164
165
166
167
168
169
170 @Nonnull
171 public static Builder newBuilder(boolean withDefaults) {
172 return new Builder(withDefaults);
173 }
174
175
176
177
178
179
180
181
182 @Nonnull
183 public static Builder newBuilder(Organization from) {
184 return newBuilder(from, false);
185 }
186
187
188
189
190
191
192
193
194 @Nonnull
195 public static Builder newBuilder(Organization from, boolean forceCopy) {
196 return new Builder(from, forceCopy);
197 }
198
199
200
201
202
203
204 @NotThreadSafe
205 public static class Builder
206 {
207 Organization base;
208 String name;
209 String url;
210 Map<Object, InputLocation> locations;
211 InputLocation importedFrom;
212
213 protected Builder(boolean withDefaults) {
214 if (withDefaults) {
215 }
216 }
217
218 protected Builder(Organization base, boolean forceCopy) {
219 if (forceCopy) {
220 this.name = base.name;
221 this.url = base.url;
222 this.locations = base.locations;
223 this.importedFrom = base.importedFrom;
224 } else {
225 this.base = base;
226 }
227 }
228
229 @Nonnull
230 public Builder name(String name) {
231 this.name = name;
232 return this;
233 }
234
235 @Nonnull
236 public Builder url(String url) {
237 this.url = url;
238 return this;
239 }
240
241
242 @Nonnull
243 public Builder location(Object key, InputLocation location) {
244 if (location != null) {
245 if (!(this.locations instanceof HashMap)) {
246 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
247 }
248 this.locations.put(key, location);
249 }
250 return this;
251 }
252
253 @Nonnull
254 public Builder importedFrom(InputLocation importedFrom) {
255 this.importedFrom = importedFrom;
256 return this;
257 }
258
259 @Nonnull
260 public Organization build() {
261
262 if (base != null
263 && (name == null || name == base.name)
264 && (url == null || url == base.url)
265 ) {
266 return base;
267 }
268 return new Organization(this);
269 }
270
271 Map<Object, InputLocation> computeLocations() {
272 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
273 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
274 if (newlocs.isEmpty()) {
275 return Map.copyOf(oldlocs);
276 }
277 if (oldlocs.isEmpty()) {
278 return Map.copyOf(newlocs);
279 }
280 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
281
282 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
283 }
284 }
285
286
287
288
289
290
291 public String toString()
292 {
293 return "Organization {name=" + getName() + ", url=" + getUrl() + "}";
294 }
295
296
297 }