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  import java.util.Objects;
25  
26  import org.apache.maven.api.ArtifactCoordinates;
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  import org.apache.maven.api.annotations.Nullable;
35  
36  import static java.util.Objects.requireNonNull;
37  
38  /**
39   *
40   * @since 4.0.0
41   */
42  @Experimental
43  @Immutable
44  public interface DependencyCoordinatesFactoryRequest extends ArtifactCoordinatesFactoryRequest {
45  
46      String getScope();
47  
48      boolean isOptional();
49  
50      @Nonnull
51      Collection<Exclusion> getExclusions();
52  
53      @Nonnull
54      static DependencyCoordinatesFactoryRequest build(
55              @Nonnull Session session,
56              String groupId,
57              String artifactId,
58              String version,
59              String classifier,
60              String extension,
61              String type) {
62          return DependencyCoordinatesFactoryRequest.builder()
63                  .session(requireNonNull(session, "session cannot be null"))
64                  .groupId(groupId)
65                  .artifactId(artifactId)
66                  .version(version)
67                  .classifier(classifier)
68                  .extension(extension)
69                  .type(type)
70                  .build();
71      }
72  
73      @Nonnull
74      static DependencyCoordinatesFactoryRequest build(
75              @Nonnull Session session, @Nonnull ArtifactCoordinates coordinates) {
76          return builder()
77                  .session(requireNonNull(session, "session cannot be null"))
78                  .groupId(requireNonNull(coordinates, "coordinates cannot be null")
79                          .getGroupId())
80                  .artifactId(coordinates.getArtifactId())
81                  .version(coordinates.getVersionConstraint().toString())
82                  .classifier(coordinates.getClassifier())
83                  .extension(coordinates.getExtension())
84                  .build();
85      }
86  
87      @Nonnull
88      static DependencyCoordinatesFactoryRequest build(@Nonnull Session session, @Nonnull Dependency dependency) {
89          return builder()
90                  .session(requireNonNull(session, "session cannot be null"))
91                  .groupId(requireNonNull(dependency, "dependency").getGroupId())
92                  .artifactId(dependency.getArtifactId())
93                  .version(dependency.getVersion().toString())
94                  .classifier(dependency.getClassifier())
95                  .extension(dependency.getExtension())
96                  .type(dependency.getType().id())
97                  .scope(dependency.getScope().id())
98                  .optional(dependency.isOptional())
99                  .build();
100     }
101 
102     @Nonnull
103     static DependencyCoordinatesFactoryRequestBuilder builder() {
104         return new DependencyCoordinatesFactoryRequestBuilder();
105     }
106 
107     @NotThreadSafe
108     class DependencyCoordinatesFactoryRequestBuilder {
109         private Session session;
110         private RequestTrace trace;
111         private String groupId;
112         private String artifactId;
113         private String version;
114         private String classifier;
115         private String extension;
116         private String type;
117         private String coordinateString;
118         private String scope;
119         private boolean optional;
120         private Collection<Exclusion> exclusions = Collections.emptyList();
121 
122         DependencyCoordinatesFactoryRequestBuilder() {}
123 
124         public DependencyCoordinatesFactoryRequestBuilder session(Session session) {
125             this.session = session;
126             return this;
127         }
128 
129         public DependencyCoordinatesFactoryRequestBuilder trace(RequestTrace trace) {
130             this.trace = trace;
131             return this;
132         }
133 
134         public DependencyCoordinatesFactoryRequestBuilder groupId(String groupId) {
135             this.groupId = groupId;
136             return this;
137         }
138 
139         public DependencyCoordinatesFactoryRequestBuilder artifactId(String artifactId) {
140             this.artifactId = artifactId;
141             return this;
142         }
143 
144         public DependencyCoordinatesFactoryRequestBuilder version(String version) {
145             this.version = version;
146             return this;
147         }
148 
149         public DependencyCoordinatesFactoryRequestBuilder classifier(String classifier) {
150             this.classifier = classifier;
151             return this;
152         }
153 
154         public DependencyCoordinatesFactoryRequestBuilder extension(String extension) {
155             this.extension = extension;
156             return this;
157         }
158 
159         public DependencyCoordinatesFactoryRequestBuilder type(String type) {
160             this.type = type;
161             return this;
162         }
163 
164         public DependencyCoordinatesFactoryRequestBuilder coordinateString(String coordinateString) {
165             this.coordinateString = coordinateString;
166             return this;
167         }
168 
169         public DependencyCoordinatesFactoryRequestBuilder scope(String scope) {
170             this.scope = scope;
171             return this;
172         }
173 
174         public DependencyCoordinatesFactoryRequestBuilder optional(boolean optional) {
175             this.optional = optional;
176             return this;
177         }
178 
179         public DependencyCoordinatesFactoryRequestBuilder exclusions(Collection<Exclusion> exclusions) {
180             if (exclusions != null) {
181                 if (this.exclusions.isEmpty()) {
182                     this.exclusions = new ArrayList<>();
183                 }
184                 this.exclusions.addAll(exclusions);
185             }
186             return this;
187         }
188 
189         public DependencyCoordinatesFactoryRequestBuilder exclusion(Exclusion exclusion) {
190             if (exclusion != null) {
191                 if (this.exclusions.isEmpty()) {
192                     this.exclusions = new ArrayList<>();
193                 }
194                 this.exclusions.add(exclusion);
195             }
196             return this;
197         }
198 
199         public DependencyCoordinatesFactoryRequest build() {
200             return new DefaultDependencyCoordinatesFactoryRequest(
201                     session,
202                     trace,
203                     groupId,
204                     artifactId,
205                     version,
206                     classifier,
207                     extension,
208                     type,
209                     coordinateString,
210                     scope,
211                     optional,
212                     exclusions);
213         }
214 
215         private static class DefaultDependencyCoordinatesFactoryRequest extends BaseRequest<Session>
216                 implements DependencyCoordinatesFactoryRequest {
217             private final String groupId;
218             private final String artifactId;
219             private final String version;
220             private final String classifier;
221             private final String extension;
222             private final String type;
223             private final String coordinateString;
224             private final String scope;
225             private final boolean optional;
226             private final Collection<Exclusion> exclusions;
227 
228             @SuppressWarnings("checkstyle:ParameterNumber")
229             private DefaultDependencyCoordinatesFactoryRequest(
230                     @Nonnull Session session,
231                     @Nullable RequestTrace trace,
232                     String groupId,
233                     String artifactId,
234                     String version,
235                     String classifier,
236                     String extension,
237                     String type,
238                     String coordinateString,
239                     String scope,
240                     boolean optional,
241                     Collection<Exclusion> exclusions) {
242                 super(session, trace);
243                 this.groupId = groupId;
244                 this.artifactId = artifactId;
245                 this.version = version;
246                 this.classifier = classifier;
247                 this.extension = extension;
248                 this.type = type;
249                 this.coordinateString = coordinateString;
250                 this.scope = scope;
251                 this.optional = optional;
252                 this.exclusions = exclusions;
253             }
254 
255             @Override
256             public String getGroupId() {
257                 return groupId;
258             }
259 
260             @Override
261             public String getArtifactId() {
262                 return artifactId;
263             }
264 
265             @Override
266             public String getVersion() {
267                 return version;
268             }
269 
270             @Override
271             public String getClassifier() {
272                 return classifier;
273             }
274 
275             @Override
276             public String getExtension() {
277                 return extension;
278             }
279 
280             @Override
281             public String getType() {
282                 return type;
283             }
284 
285             @Override
286             public String getCoordinatesString() {
287                 return coordinateString;
288             }
289 
290             @Override
291             public String getScope() {
292                 return scope;
293             }
294 
295             @Override
296             public boolean isOptional() {
297                 return optional;
298             }
299 
300             @Nonnull
301             @Override
302             public Collection<Exclusion> getExclusions() {
303                 return exclusions;
304             }
305 
306             @Override
307             public boolean equals(Object o) {
308                 return o instanceof DefaultDependencyCoordinatesFactoryRequest that
309                         && optional == that.optional
310                         && Objects.equals(groupId, that.groupId)
311                         && Objects.equals(artifactId, that.artifactId)
312                         && Objects.equals(version, that.version)
313                         && Objects.equals(classifier, that.classifier)
314                         && Objects.equals(extension, that.extension)
315                         && Objects.equals(type, that.type)
316                         && Objects.equals(coordinateString, that.coordinateString)
317                         && Objects.equals(scope, that.scope)
318                         && Objects.equals(exclusions, that.exclusions);
319             }
320 
321             @Override
322             public int hashCode() {
323                 return Objects.hash(
324                         groupId,
325                         artifactId,
326                         version,
327                         classifier,
328                         extension,
329                         type,
330                         coordinateString,
331                         scope,
332                         optional,
333                         exclusions);
334             }
335 
336             @Override
337             public String toString() {
338                 return "DependencyCoordinatesFactoryRequest[" + "groupId='"
339                         + groupId + '\'' + ", artifactId='"
340                         + artifactId + '\'' + ", version='"
341                         + version + '\'' + ", classifier='"
342                         + classifier + '\'' + ", extension='"
343                         + extension + '\'' + ", type='"
344                         + type + '\'' + ", coordinateString='"
345                         + coordinateString + '\'' + ", scope='"
346                         + scope + '\'' + ", optional="
347                         + optional + ", exclusions="
348                         + exclusions + ']';
349             }
350         }
351     }
352 }