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 org.apache.maven.api.annotations.Experimental;
12 import org.apache.maven.api.annotations.Generated;
13 import org.apache.maven.api.annotations.Immutable;
14 import org.apache.maven.api.annotations.Nonnull;
15 import org.apache.maven.api.annotations.NotThreadSafe;
16 import org.apache.maven.api.annotations.ThreadSafe;
17
18
19
20
21 @Experimental
22 @Generated @ThreadSafe @Immutable
23 public class Extension
24 implements Serializable, InputLocationTracker
25 {
26
27
28
29 final String groupId;
30
31
32
33 final String artifactId;
34
35
36
37 final String version;
38
39 final Map<Object, InputLocation> locations;
40
41
42
43
44
45 Extension(
46 String groupId,
47 String artifactId,
48 String version,
49 Map<Object, InputLocation> locations
50 ) {
51 this.groupId = groupId;
52 this.artifactId = artifactId;
53 this.version = version;
54 this.locations = ImmutableCollections.copy(locations);
55 }
56
57
58
59
60
61
62 public String getGroupId() {
63 return this.groupId;
64 }
65
66
67
68
69
70
71 public String getArtifactId() {
72 return this.artifactId;
73 }
74
75
76
77
78
79
80 public String getVersion() {
81 return this.version;
82 }
83
84
85
86
87 public InputLocation getLocation(Object key) {
88 return locations != null ? locations.get(key) : null;
89 }
90
91
92
93
94
95
96 @Nonnull
97 public Builder with() {
98 return newBuilder(this);
99 }
100
101
102
103
104
105
106 @Nonnull
107 public Extension withGroupId(String groupId) {
108 return newBuilder(this, true).groupId(groupId).build();
109 }
110
111
112
113
114
115
116 @Nonnull
117 public Extension withArtifactId(String artifactId) {
118 return newBuilder(this, true).artifactId(artifactId).build();
119 }
120
121
122
123
124
125
126 @Nonnull
127 public Extension withVersion(String version) {
128 return newBuilder(this, true).version(version).build();
129 }
130
131
132
133
134
135
136
137
138 @Nonnull
139 public static Extension newInstance() {
140 return newInstance(true);
141 }
142
143
144
145
146
147
148
149
150 @Nonnull
151 public static Extension newInstance(boolean withDefaults) {
152 return newBuilder(withDefaults).build();
153 }
154
155
156
157
158
159
160
161
162 @Nonnull
163 public static Builder newBuilder() {
164 return newBuilder(true);
165 }
166
167
168
169
170
171
172
173 @Nonnull
174 public static Builder newBuilder(boolean withDefaults) {
175 return new Builder(withDefaults);
176 }
177
178
179
180
181
182
183
184
185 @Nonnull
186 public static Builder newBuilder(Extension from) {
187 return newBuilder(from, false);
188 }
189
190
191
192
193
194
195
196
197 @Nonnull
198 public static Builder newBuilder(Extension from, boolean forceCopy) {
199 return new Builder(from, forceCopy);
200 }
201
202
203
204
205
206
207 @NotThreadSafe
208 public static class Builder
209 {
210 Extension base;
211 String groupId;
212 String artifactId;
213 String version;
214 Map<Object, InputLocation> locations;
215
216 Builder(boolean withDefaults) {
217 if (withDefaults) {
218 }
219 }
220
221 Builder(Extension base, boolean forceCopy) {
222 if (forceCopy) {
223 this.groupId = base.groupId;
224 this.artifactId = base.artifactId;
225 this.version = base.version;
226 this.locations = base.locations;
227 } else {
228 this.base = base;
229 }
230 }
231
232 @Nonnull
233 public Builder groupId(String groupId) {
234 this.groupId = groupId;
235 return this;
236 }
237
238 @Nonnull
239 public Builder artifactId(String artifactId) {
240 this.artifactId = artifactId;
241 return this;
242 }
243
244 @Nonnull
245 public Builder version(String version) {
246 this.version = version;
247 return this;
248 }
249
250
251 @Nonnull
252 public Builder location(Object key, InputLocation location) {
253 if (location != null) {
254 if (!(this.locations instanceof HashMap)) {
255 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
256 }
257 this.locations.put(key, location);
258 }
259 return this;
260 }
261
262 @Nonnull
263 public Extension build() {
264 if (base != null
265 && (groupId == null || groupId == base.groupId)
266 && (artifactId == null || artifactId == base.artifactId)
267 && (version == null || version == base.version)
268 ) {
269 return base;
270 }
271 Map<Object, InputLocation> locations = null;
272 InputLocation location = null;
273 InputLocation groupIdLocation = null;
274 InputLocation artifactIdLocation = null;
275 InputLocation versionLocation = null;
276 if (this.locations != null) {
277 locations = this.locations;
278 }
279 return new Extension(
280 groupId != null ? groupId : (base != null ? base.groupId : null),
281 artifactId != null ? artifactId : (base != null ? base.artifactId : null),
282 version != null ? version : (base != null ? base.version : null),
283 locations != null ? locations : (base != null ? base.locations : null)
284 );
285 }
286 }
287
288
289
290
291
292
293 public boolean equals( Object o )
294 {
295 if ( this == o )
296 {
297 return true;
298 }
299
300 if ( !( o instanceof Extension ) )
301 {
302 return false;
303 }
304
305 Extension e = (Extension) o;
306
307 if ( !equal( e.getArtifactId(), getArtifactId() ) )
308 {
309 return false;
310 }
311 else if ( !equal( e.getGroupId(), getGroupId() ) )
312 {
313 return false;
314 }
315 else if ( !equal( e.getVersion(), getVersion() ) )
316 {
317 return false;
318 }
319 return true;
320 }
321
322 private static <T> boolean equal( T obj1, T obj2 )
323 {
324 return ( obj1 != null ) ? obj1.equals( obj2 ) : obj2 == null;
325 }
326
327
328
329
330 public int hashCode()
331 {
332 int result = 17;
333 result = 37 * result + ( getArtifactId() != null ? getArtifactId().hashCode() : 0 );
334 result = 37 * result + ( getGroupId() != null ? getGroupId().hashCode() : 0 );
335 result = 37 * result + ( getVersion() != null ? getVersion().hashCode() : 0 );
336 return result;
337 }
338
339
340 }