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 InputLocation location;
40
41 final InputLocation groupIdLocation;
42
43 final InputLocation artifactIdLocation;
44
45 final InputLocation versionLocation;
46
47 final Map<Object, InputLocation> locations;
48
49
50
51
52
53 Extension(
54 String groupId,
55 String artifactId,
56 String version,
57 Map<Object, InputLocation> locations,
58 InputLocation location,
59 InputLocation groupIdLocation,
60 InputLocation artifactIdLocation,
61 InputLocation versionLocation
62 )
63 {
64 this.groupId = groupId;
65 this.artifactId = artifactId;
66 this.version = version;
67 this.locations = ImmutableCollections.copy( locations );
68 this.location = location;
69 this.groupIdLocation = groupIdLocation;
70 this.artifactIdLocation = artifactIdLocation;
71 this.versionLocation = versionLocation;
72 }
73
74
75
76
77
78
79 public String getGroupId()
80 {
81 return this.groupId;
82 }
83
84
85
86
87
88
89 public String getArtifactId()
90 {
91 return this.artifactId;
92 }
93
94
95
96
97
98
99 public String getVersion()
100 {
101 return this.version;
102 }
103
104
105
106
107 public InputLocation getLocation( Object key )
108 {
109 if ( key instanceof String )
110 {
111 switch ( ( String ) key )
112 {
113 case "":
114 return location;
115 case "groupId":
116 return groupIdLocation;
117 case "artifactId":
118 return artifactIdLocation;
119 case "version":
120 return versionLocation;
121 }
122 }
123 return locations != null ? locations.get( key ) : null;
124 }
125
126
127
128
129
130
131 @Nonnull
132 public Builder with()
133 {
134 return newBuilder( this );
135 }
136
137
138
139
140
141
142 @Nonnull
143 public Extension withGroupId( String groupId )
144 {
145 return with().groupId( groupId ).build();
146 }
147
148
149
150
151
152
153 @Nonnull
154 public Extension withArtifactId( String artifactId )
155 {
156 return with().artifactId( artifactId ).build();
157 }
158
159
160
161
162
163
164 @Nonnull
165 public Extension withVersion( String version )
166 {
167 return with().version( version ).build();
168 }
169
170
171
172
173
174
175
176
177 @Nonnull
178 public static Extension newInstance()
179 {
180 return newInstance( true );
181 }
182
183
184
185
186
187
188
189
190 @Nonnull
191 public static Extension newInstance( boolean withDefaults )
192 {
193 return newBuilder( withDefaults ).build();
194 }
195
196
197
198
199
200
201
202
203 @Nonnull
204 public static Builder newBuilder()
205 {
206 return newBuilder( true );
207 }
208
209
210
211
212
213
214
215 @Nonnull
216 public static Builder newBuilder( boolean withDefaults )
217 {
218 return new Builder( withDefaults );
219 }
220
221
222
223
224
225
226
227
228 @Nonnull
229 public static Builder newBuilder( Extension from )
230 {
231 return newBuilder( from, false );
232 }
233
234
235
236
237
238
239
240
241 @Nonnull
242 public static Builder newBuilder( Extension from, boolean forceCopy )
243 {
244 return new Builder( from, forceCopy );
245 }
246
247
248
249
250
251
252 @NotThreadSafe
253 public static class Builder
254 {
255 Extension base;
256 String groupId;
257 String artifactId;
258 String version;
259 Map<Object, InputLocation> locations;
260
261 Builder( boolean withDefaults )
262 {
263 if ( withDefaults )
264 {
265 }
266 }
267
268 Builder( Extension base, boolean forceCopy )
269 {
270 if ( forceCopy )
271 {
272 this.groupId = base.groupId;
273 this.artifactId = base.artifactId;
274 this.version = base.version;
275 }
276 else
277 {
278 this.base = base;
279 }
280 }
281
282 @Nonnull
283 public Builder groupId( String groupId )
284 {
285 this.groupId = groupId;
286 return this;
287 }
288
289 @Nonnull
290 public Builder artifactId( String artifactId )
291 {
292 this.artifactId = artifactId;
293 return this;
294 }
295
296 @Nonnull
297 public Builder version( String version )
298 {
299 this.version = version;
300 return this;
301 }
302
303
304 @Nonnull
305 public Builder location( Object key, InputLocation location )
306 {
307 if ( location != null )
308 {
309 if ( this.locations == null )
310 {
311 this.locations = new HashMap<>();
312 }
313 this.locations.put( key, location );
314 }
315 return this;
316 }
317
318 @Nonnull
319 public Extension build()
320 {
321 if ( base != null
322 && ( groupId == null || groupId == base.groupId )
323 && ( artifactId == null || artifactId == base.artifactId )
324 && ( version == null || version == base.version )
325 )
326 {
327 return base;
328 }
329 Map<Object, InputLocation> locations = null;
330 InputLocation location = null;
331 InputLocation groupIdLocation = null;
332 InputLocation artifactIdLocation = null;
333 InputLocation versionLocation = null;
334 if ( this.locations != null )
335 {
336 locations = this.locations;
337 location = locations.remove( "" );
338 groupIdLocation = locations.remove( "groupId" );
339 artifactIdLocation = locations.remove( "artifactId" );
340 versionLocation = locations.remove( "version" );
341 }
342 return new Extension(
343 groupId != null ? groupId : ( base != null ? base.groupId : null ),
344 artifactId != null ? artifactId : ( base != null ? base.artifactId : null ),
345 version != null ? version : ( base != null ? base.version : null ),
346 locations != null ? locations : ( base != null ? base.locations : null ),
347 location != null ? location : ( base != null ? base.location : null ),
348 groupIdLocation != null ? groupIdLocation : ( base != null ? base.groupIdLocation : null ),
349 artifactIdLocation != null ? artifactIdLocation : ( base != null ? base.artifactIdLocation : null ),
350 versionLocation != null ? versionLocation : ( base != null ? base.versionLocation : null )
351 );
352 }
353 }
354
355
356
357
358
359
360 public boolean equals( Object o )
361 {
362 if ( this == o )
363 {
364 return true;
365 }
366
367 if ( !( o instanceof Extension ) )
368 {
369 return false;
370 }
371
372 Extension e = (Extension) o;
373
374 if ( !equal( e.getArtifactId(), getArtifactId() ) )
375 {
376 return false;
377 }
378 else if ( !equal( e.getGroupId(), getGroupId() ) )
379 {
380 return false;
381 }
382 else if ( !equal( e.getVersion(), getVersion() ) )
383 {
384 return false;
385 }
386 return true;
387 }
388
389 private static <T> boolean equal( T obj1, T obj2 )
390 {
391 return ( obj1 != null ) ? obj1.equals( obj2 ) : obj2 == null;
392 }
393
394
395
396
397 public int hashCode()
398 {
399 int result = 17;
400 result = 37 * result + ( getArtifactId() != null ? getArtifactId().hashCode() : 0 );
401 result = 37 * result + ( getGroupId() != null ? getGroupId().hashCode() : 0 );
402 result = 37 * result + ( getVersion() != null ? getVersion().hashCode() : 0 );
403 return result;
404 }
405
406
407 }