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 static org.apache.maven.api.services.BaseRequest.nonNull;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
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  /**
36   *
37   * @since 4.0
38   */
39  @Experimental
40  @Immutable
41  public interface DependencyCoordinateFactoryRequest extends ArtifactCoordinateFactoryRequest {
42  
43      String getScope();
44  
45      boolean isOptional();
46  
47      @Nonnull
48      Collection<Exclusion> getExclusions();
49  
50      @Nonnull
51      static DependencyCoordinateFactoryRequest build(
52              @Nonnull Session session,
53              String groupId,
54              String artifactId,
55              String version,
56              String classifier,
57              String extension,
58              String type) {
59          return DependencyCoordinateFactoryRequest.builder()
60                  .session(nonNull(session, "session cannot be null"))
61                  .groupId(groupId)
62                  .artifactId(artifactId)
63                  .version(version)
64                  .classifier(classifier)
65                  .extension(extension)
66                  .type(type)
67                  .build();
68      }
69  
70      @Nonnull
71      static DependencyCoordinateFactoryRequest build(@Nonnull Session session, @Nonnull ArtifactCoordinate coordinate) {
72          return builder()
73                  .session(nonNull(session, "session cannot be null"))
74                  .groupId(nonNull(coordinate, "coordinate cannot be null").getGroupId())
75                  .artifactId(coordinate.getArtifactId())
76                  .version(coordinate.getVersion().asString())
77                  .classifier(coordinate.getClassifier())
78                  .extension(coordinate.getExtension())
79                  .build();
80      }
81  
82      @Nonnull
83      static DependencyCoordinateFactoryRequest build(@Nonnull Session session, @Nonnull Dependency dependency) {
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          return new DependencyCoordinateFactoryRequestBuilder();
100     }
101 
102     @NotThreadSafe
103     class DependencyCoordinateFactoryRequestBuilder {
104         private Session session;
105         private String groupId;
106         private String artifactId;
107         private String version;
108         private String classifier;
109         private String extension;
110         private String type;
111         private String scope;
112         private boolean optional;
113         private Collection<Exclusion> exclusions = Collections.emptyList();
114 
115         DependencyCoordinateFactoryRequestBuilder() {}
116 
117         public DependencyCoordinateFactoryRequestBuilder session(Session session) {
118             this.session = session;
119             return this;
120         }
121 
122         public DependencyCoordinateFactoryRequestBuilder groupId(String groupId) {
123             this.groupId = groupId;
124             return this;
125         }
126 
127         public DependencyCoordinateFactoryRequestBuilder artifactId(String artifactId) {
128             this.artifactId = artifactId;
129             return this;
130         }
131 
132         public DependencyCoordinateFactoryRequestBuilder version(String version) {
133             this.version = version;
134             return this;
135         }
136 
137         public DependencyCoordinateFactoryRequestBuilder classifier(String classifier) {
138             this.classifier = classifier;
139             return this;
140         }
141 
142         public DependencyCoordinateFactoryRequestBuilder extension(String extension) {
143             this.extension = extension;
144             return this;
145         }
146 
147         public DependencyCoordinateFactoryRequestBuilder type(String type) {
148             this.type = type;
149             return this;
150         }
151 
152         public DependencyCoordinateFactoryRequestBuilder scope(String scope) {
153             this.scope = scope;
154             return this;
155         }
156 
157         public DependencyCoordinateFactoryRequestBuilder optional(boolean optional) {
158             this.optional = optional;
159             return this;
160         }
161 
162         public DependencyCoordinateFactoryRequestBuilder exclusions(Collection<Exclusion> exclusions) {
163             if (exclusions != null) {
164                 if (this.exclusions.isEmpty()) {
165                     this.exclusions = new ArrayList<>();
166                 }
167                 this.exclusions.addAll(exclusions);
168             }
169             return this;
170         }
171 
172         public DependencyCoordinateFactoryRequestBuilder exclusion(Exclusion exclusion) {
173             if (exclusion != null) {
174                 if (this.exclusions.isEmpty()) {
175                     this.exclusions = new ArrayList<>();
176                 }
177                 this.exclusions.add(exclusion);
178             }
179             return this;
180         }
181 
182         public DependencyCoordinateFactoryRequest build() {
183             return new DefaultDependencyCoordinateFactoryRequest(
184                     session, groupId, artifactId, version, classifier, extension, type, scope, optional, exclusions);
185         }
186 
187         private static class DefaultDependencyCoordinateFactoryRequest extends BaseRequest
188                 implements DependencyCoordinateFactoryRequest {
189             private final String groupId;
190             private final String artifactId;
191             private final String version;
192             private final String classifier;
193             private final String extension;
194             private final String type;
195             private final String scope;
196             private final boolean optional;
197             private final Collection<Exclusion> exclusions;
198 
199             @SuppressWarnings("checkstyle:ParameterNumber")
200             private DefaultDependencyCoordinateFactoryRequest(
201                     @Nonnull Session session,
202                     String groupId,
203                     String artifactId,
204                     String version,
205                     String classifier,
206                     String extension,
207                     String type,
208                     String scope,
209                     boolean optional,
210                     Collection<Exclusion> exclusions) {
211                 super(session);
212                 this.groupId = groupId;
213                 this.artifactId = artifactId;
214                 this.version = version;
215                 this.classifier = classifier;
216                 this.extension = extension;
217                 this.type = type;
218                 this.scope = scope;
219                 this.optional = optional;
220                 this.exclusions = exclusions;
221             }
222 
223             @Override
224             public String getGroupId() {
225                 return groupId;
226             }
227 
228             @Override
229             public String getArtifactId() {
230                 return artifactId;
231             }
232 
233             @Override
234             public String getVersion() {
235                 return version;
236             }
237 
238             @Override
239             public String getClassifier() {
240                 return classifier;
241             }
242 
243             @Override
244             public String getExtension() {
245                 return extension;
246             }
247 
248             @Override
249             public String getType() {
250                 return type;
251             }
252 
253             @Override
254             public String getScope() {
255                 return scope;
256             }
257 
258             @Override
259             public boolean isOptional() {
260                 return optional;
261             }
262 
263             @Nonnull
264             @Override
265             public Collection<Exclusion> getExclusions() {
266                 return exclusions;
267             }
268         }
269     }
270 }