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.shared.artifact.filter.collection;
20
21 import java.util.LinkedHashSet;
22 import java.util.Set;
23
24 import org.apache.maven.artifact.Artifact;
25
26 /**
27 * <p>ProjectTransitivityFilter class.</p>
28 *
29 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
30 */
31 public class ProjectTransitivityFilter extends AbstractArtifactsFilter {
32
33 private boolean excludeTransitive;
34
35 private final Set<Artifact> directDependencies;
36
37 /**
38 * <p>Constructor for ProjectTransitivityFilter.</p>
39 *
40 * @param directDependencies set of direct dependencies.
41 * @param excludeTransitive {@code true} exclude transitive deps {@code false} otherwise.
42 */
43 public ProjectTransitivityFilter(Set<Artifact> directDependencies, boolean excludeTransitive) {
44 this.excludeTransitive = excludeTransitive;
45 this.directDependencies = directDependencies;
46 }
47
48 /** {@inheritDoc} */
49 public Set<Artifact> filter(Set<Artifact> artifacts) {
50 // why not just take the directDependencies here?
51 // because if this filter is run after some other process, the
52 // set of artifacts may not be the same as the directDependencies.
53 Set<Artifact> result = artifacts;
54
55 if (excludeTransitive) {
56 result = new LinkedHashSet<>();
57 for (Artifact artifact : artifacts) {
58 if (artifactIsADirectDependency(artifact)) {
59 result.add(artifact);
60 }
61 }
62 }
63 return result;
64 }
65
66 /**
67 * Compares the artifact to the list of dependencies to see if it is directly included by this project
68 *
69 * @param artifact representing the item to compare.
70 * @return true if artifact is a direct dependency
71 */
72 public boolean artifactIsADirectDependency(Artifact artifact) {
73 for (Artifact dependency : this.directDependencies) {
74 if (dependency.equals(artifact)) {
75 return true;
76 }
77 }
78 return false;
79 }
80
81 /**
82 * <p>isExcludeTransitive.</p>
83 *
84 * @return Returns the excludeTransitive.
85 */
86 public boolean isExcludeTransitive() {
87 return this.excludeTransitive;
88 }
89
90 /**
91 * <p>Setter for the field <code>excludeTransitive</code>.</p>
92 *
93 * @param excludeTransitive The excludeTransitive to set.
94 */
95 public void setExcludeTransitive(boolean excludeTransitive) {
96 this.excludeTransitive = excludeTransitive;
97 }
98 }