View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.api.services;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  
25  import org.apache.maven.api.ArtifactCoordinates;
26  import org.apache.maven.api.Dependency;
27  import org.apache.maven.api.Exclusion;
28  import org.apache.maven.api.Session;
29  import org.apache.maven.api.annotations.Experimental;
30  import org.apache.maven.api.annotations.Immutable;
31  import org.apache.maven.api.annotations.Nonnull;
32  import org.apache.maven.api.annotations.NotThreadSafe;
33  
34  import static org.apache.maven.api.services.BaseRequest.nonNull;
35  
36  /**
37   *
38   * @since 4.0.0
39   */
40  @Experimental
41  @Immutable
42  public interface DependencyCoordinatesFactoryRequest extends ArtifactCoordinatesFactoryRequest {
43  
44      String getScope();
45  
46      boolean isOptional();
47  
48      @Nonnull
49      Collection<Exclusion> getExclusions();
50  
51      @Nonnull
52      static DependencyCoordinatesFactoryRequest build(
53              @Nonnull Session session,
54              String groupId,
55              String artifactId,
56              String version,
57              String classifier,
58              String extension,
59              String type) {
60          return DependencyCoordinatesFactoryRequest.builder()
61                  .session(nonNull(session, "session cannot be null"))
62                  .groupId(groupId)
63                  .artifactId(artifactId)
64                  .version(version)
65                  .classifier(classifier)
66                  .extension(extension)
67                  .type(type)
68                  .build();
69      }
70  
71      @Nonnull
72      static DependencyCoordinatesFactoryRequest build(
73              @Nonnull Session session, @Nonnull ArtifactCoordinates coordinates) {
74          return builder()
75                  .session(nonNull(session, "session cannot be null"))
76                  .groupId(nonNull(coordinates, "coordinates cannot be null").getGroupId())
77                  .artifactId(coordinates.getArtifactId())
78                  .version(coordinates.getVersionConstraint().asString())
79                  .classifier(coordinates.getClassifier())
80                  .extension(coordinates.getExtension())
81                  .build();
82      }
83  
84      @Nonnull
85      static DependencyCoordinatesFactoryRequest build(@Nonnull Session session, @Nonnull Dependency dependency) {
86          return builder()
87                  .session(nonNull(session, "session cannot be null"))
88                  .groupId(nonNull(dependency, "dependency").getGroupId())
89                  .artifactId(dependency.getArtifactId())
90                  .version(dependency.getVersion().asString())
91                  .classifier(dependency.getClassifier())
92                  .extension(dependency.getExtension())
93                  .type(dependency.getType().id())
94                  .scope(dependency.getScope().id())
95                  .optional(dependency.isOptional())
96                  .build();
97      }
98  
99      @Nonnull
100     static DependencyCoordinatesFactoryRequestBuilder builder() {
101         return new DependencyCoordinatesFactoryRequestBuilder();
102     }
103 
104     @NotThreadSafe
105     class DependencyCoordinatesFactoryRequestBuilder {
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 coordinateString;
114         private String scope;
115         private boolean optional;
116         private Collection<Exclusion> exclusions = Collections.emptyList();
117 
118         DependencyCoordinatesFactoryRequestBuilder() {}
119 
120         public DependencyCoordinatesFactoryRequestBuilder session(Session session) {
121             this.session = session;
122             return this;
123         }
124 
125         public DependencyCoordinatesFactoryRequestBuilder groupId(String groupId) {
126             this.groupId = groupId;
127             return this;
128         }
129 
130         public DependencyCoordinatesFactoryRequestBuilder artifactId(String artifactId) {
131             this.artifactId = artifactId;
132             return this;
133         }
134 
135         public DependencyCoordinatesFactoryRequestBuilder version(String version) {
136             this.version = version;
137             return this;
138         }
139 
140         public DependencyCoordinatesFactoryRequestBuilder classifier(String classifier) {
141             this.classifier = classifier;
142             return this;
143         }
144 
145         public DependencyCoordinatesFactoryRequestBuilder extension(String extension) {
146             this.extension = extension;
147             return this;
148         }
149 
150         public DependencyCoordinatesFactoryRequestBuilder type(String type) {
151             this.type = type;
152             return this;
153         }
154 
155         public DependencyCoordinatesFactoryRequestBuilder coordinateString(String coordinateString) {
156             this.coordinateString = coordinateString;
157             return this;
158         }
159 
160         public DependencyCoordinatesFactoryRequestBuilder scope(String scope) {
161             this.scope = scope;
162             return this;
163         }
164 
165         public DependencyCoordinatesFactoryRequestBuilder optional(boolean optional) {
166             this.optional = optional;
167             return this;
168         }
169 
170         public DependencyCoordinatesFactoryRequestBuilder exclusions(Collection<Exclusion> exclusions) {
171             if (exclusions != null) {
172                 if (this.exclusions.isEmpty()) {
173                     this.exclusions = new ArrayList<>();
174                 }
175                 this.exclusions.addAll(exclusions);
176             }
177             return this;
178         }
179 
180         public DependencyCoordinatesFactoryRequestBuilder exclusion(Exclusion exclusion) {
181             if (exclusion != null) {
182                 if (this.exclusions.isEmpty()) {
183                     this.exclusions = new ArrayList<>();
184                 }
185                 this.exclusions.add(exclusion);
186             }
187             return this;
188         }
189 
190         public DependencyCoordinatesFactoryRequest build() {
191             return new DefaultDependencyCoordinatesFactoryRequest(
192                     session,
193                     groupId,
194                     artifactId,
195                     version,
196                     classifier,
197                     extension,
198                     type,
199                     coordinateString,
200                     scope,
201                     optional,
202                     exclusions);
203         }
204 
205         private static class DefaultDependencyCoordinatesFactoryRequest extends BaseRequest
206                 implements DependencyCoordinatesFactoryRequest {
207             private final String groupId;
208             private final String artifactId;
209             private final String version;
210             private final String classifier;
211             private final String extension;
212             private final String type;
213             private final String coordinateString;
214             private final String scope;
215             private final boolean optional;
216             private final Collection<Exclusion> exclusions;
217 
218             @SuppressWarnings("checkstyle:ParameterNumber")
219             private DefaultDependencyCoordinatesFactoryRequest(
220                     @Nonnull Session session,
221                     String groupId,
222                     String artifactId,
223                     String version,
224                     String classifier,
225                     String extension,
226                     String type,
227                     String coordinateString,
228                     String scope,
229                     boolean optional,
230                     Collection<Exclusion> exclusions) {
231                 super(session);
232                 this.groupId = groupId;
233                 this.artifactId = artifactId;
234                 this.version = version;
235                 this.classifier = classifier;
236                 this.extension = extension;
237                 this.type = type;
238                 this.coordinateString = coordinateString;
239                 this.scope = scope;
240                 this.optional = optional;
241                 this.exclusions = exclusions;
242             }
243 
244             @Override
245             public String getGroupId() {
246                 return groupId;
247             }
248 
249             @Override
250             public String getArtifactId() {
251                 return artifactId;
252             }
253 
254             @Override
255             public String getVersion() {
256                 return version;
257             }
258 
259             @Override
260             public String getClassifier() {
261                 return classifier;
262             }
263 
264             @Override
265             public String getExtension() {
266                 return extension;
267             }
268 
269             @Override
270             public String getType() {
271                 return type;
272             }
273 
274             public String getCoordinatesString() {
275                 return coordinateString;
276             }
277 
278             @Override
279             public String getScope() {
280                 return scope;
281             }
282 
283             @Override
284             public boolean isOptional() {
285                 return optional;
286             }
287 
288             @Nonnull
289             @Override
290             public Collection<Exclusion> getExclusions() {
291                 return exclusions;
292             }
293         }
294     }
295 }