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