1 package org.apache.maven.api.services;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25
26 import org.apache.maven.api.ArtifactCoordinate;
27 import org.apache.maven.api.Dependency;
28 import org.apache.maven.api.Exclusion;
29 import org.apache.maven.api.Session;
30 import org.apache.maven.api.annotations.Experimental;
31 import org.apache.maven.api.annotations.Immutable;
32 import org.apache.maven.api.annotations.Nonnull;
33 import org.apache.maven.api.annotations.NotThreadSafe;
34
35 import static org.apache.maven.api.services.BaseRequest.nonNull;
36
37
38
39
40
41 @Experimental
42 @Immutable
43 public interface DependencyCoordinateFactoryRequest extends ArtifactCoordinateFactoryRequest
44 {
45
46 String getScope();
47
48 boolean isOptional();
49
50 @Nonnull
51 Collection<Exclusion> getExclusions();
52
53 @Nonnull
54 static DependencyCoordinateFactoryRequest build( @Nonnull Session session, String groupId, String artifactId,
55 String version, String classifier, String extension, String type )
56 {
57 return DependencyCoordinateFactoryRequest.builder()
58 .session( nonNull( session, "session cannot be null" ) )
59 .groupId( groupId )
60 .artifactId( artifactId )
61 .version( version )
62 .classifier( classifier )
63 .extension( extension )
64 .type( type )
65 .build();
66 }
67
68 @Nonnull
69 static DependencyCoordinateFactoryRequest build( @Nonnull Session session, @Nonnull ArtifactCoordinate coordinate )
70 {
71 return builder()
72 .session( nonNull( session, "session cannot be null" ) )
73 .groupId( nonNull( coordinate, "coordinate cannot be null" ).getGroupId() )
74 .artifactId( coordinate.getArtifactId() )
75 .version( coordinate.getVersion().asString() )
76 .classifier( coordinate.getClassifier() )
77 .extension( coordinate.getExtension() )
78 .build();
79 }
80
81 @Nonnull
82 static DependencyCoordinateFactoryRequest build( @Nonnull Session session, @Nonnull Dependency dependency )
83 {
84 return builder()
85 .session( nonNull( session, "session cannot be null" ) )
86 .groupId( nonNull( dependency, "dependency" ).getGroupId() )
87 .artifactId( dependency.getArtifactId() )
88 .version( dependency.getVersion().asString() )
89 .classifier( dependency.getClassifier() )
90 .extension( dependency.getExtension() )
91 .type( dependency.getType().getName() )
92 .scope( dependency.getScope().id() )
93 .optional( dependency.isOptional() )
94 .build();
95 }
96
97 @Nonnull
98 static DependencyCoordinateFactoryRequestBuilder builder()
99 {
100 return new DependencyCoordinateFactoryRequestBuilder();
101 }
102
103 @NotThreadSafe
104 class DependencyCoordinateFactoryRequestBuilder
105 {
106 private Session session;
107 private String groupId;
108 private String artifactId;
109 private String version;
110 private String classifier;
111 private String extension;
112 private String type;
113 private String scope;
114 private boolean optional;
115 private Collection<Exclusion> exclusions = Collections.emptyList();
116
117 DependencyCoordinateFactoryRequestBuilder()
118 {
119 }
120
121 public DependencyCoordinateFactoryRequestBuilder session( Session session )
122 {
123 this.session = session;
124 return this;
125 }
126
127 public DependencyCoordinateFactoryRequestBuilder groupId( String groupId )
128 {
129 this.groupId = groupId;
130 return this;
131 }
132
133 public DependencyCoordinateFactoryRequestBuilder artifactId( String artifactId )
134 {
135 this.artifactId = artifactId;
136 return this;
137 }
138
139 public DependencyCoordinateFactoryRequestBuilder version( String version )
140 {
141 this.version = version;
142 return this;
143 }
144
145 public DependencyCoordinateFactoryRequestBuilder classifier( String classifier )
146 {
147 this.classifier = classifier;
148 return this;
149 }
150
151 public DependencyCoordinateFactoryRequestBuilder extension( String extension )
152 {
153 this.extension = extension;
154 return this;
155 }
156
157 public DependencyCoordinateFactoryRequestBuilder type( String type )
158 {
159 this.type = type;
160 return this;
161 }
162
163 public DependencyCoordinateFactoryRequestBuilder scope( String scope )
164 {
165 this.scope = scope;
166 return this;
167 }
168
169 public DependencyCoordinateFactoryRequestBuilder optional( boolean optional )
170 {
171 this.optional = optional;
172 return this;
173 }
174
175 public DependencyCoordinateFactoryRequestBuilder exclusions( Collection<Exclusion> exclusions )
176 {
177 if ( exclusions != null )
178 {
179 if ( this.exclusions.isEmpty() )
180 {
181 this.exclusions = new ArrayList<>();
182 }
183 this.exclusions.addAll( exclusions );
184 }
185 return this;
186 }
187
188 public DependencyCoordinateFactoryRequestBuilder exclusion( Exclusion exclusion )
189 {
190 if ( exclusion != null )
191 {
192 if ( this.exclusions.isEmpty() )
193 {
194 this.exclusions = new ArrayList<>();
195 }
196 this.exclusions.add( exclusion );
197 }
198 return this;
199 }
200
201 public DependencyCoordinateFactoryRequest build()
202 {
203 return new DefaultDependencyCoordinateFactoryRequest( session, groupId, artifactId, version,
204 classifier, extension, type, scope, optional, exclusions );
205 }
206
207 private static class DefaultDependencyCoordinateFactoryRequest
208 extends BaseRequest
209 implements DependencyCoordinateFactoryRequest
210 {
211 private final String groupId;
212 private final String artifactId;
213 private final String version;
214 private final String classifier;
215 private final String extension;
216 private final String type;
217 private final String scope;
218 private final boolean optional;
219 private final Collection<Exclusion> exclusions;
220
221 @SuppressWarnings( "checkstyle:ParameterNumber" )
222 private DefaultDependencyCoordinateFactoryRequest(
223 @Nonnull Session session, String groupId,
224 String artifactId,
225 String version,
226 String classifier,
227 String extension,
228 String type,
229 String scope,
230 boolean optional,
231 Collection<Exclusion> exclusions )
232 {
233 super( session );
234 this.groupId = groupId;
235 this.artifactId = artifactId;
236 this.version = version;
237 this.classifier = classifier;
238 this.extension = extension;
239 this.type = type;
240 this.scope = scope;
241 this.optional = optional;
242 this.exclusions = exclusions;
243 }
244
245 @Override
246 public String getGroupId()
247 {
248 return groupId;
249 }
250
251 @Override
252 public String getArtifactId()
253 {
254 return artifactId;
255 }
256
257 @Override
258 public String getVersion()
259 {
260 return version;
261 }
262
263 @Override
264 public String getClassifier()
265 {
266 return classifier;
267 }
268
269 @Override
270 public String getExtension()
271 {
272 return extension;
273 }
274
275 @Override
276 public String getType()
277 {
278 return type;
279 }
280
281 @Override
282 public String getScope()
283 {
284 return scope;
285 }
286
287 @Override
288 public boolean isOptional()
289 {
290 return optional;
291 }
292
293 @Nonnull
294 @Override
295 public Collection<Exclusion> getExclusions()
296 {
297 return exclusions;
298 }
299 }
300 }
301
302 }