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