1 package org.apache.maven.shared.artifact.filter.collection;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.Set;
25
26 import org.apache.maven.artifact.Artifact;
27
28 /**
29 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
30 * @version $Id: ProjectTransitivityFilter.java 803330 2009-08-11 23:58:24Z aheritier $
31 */
32 public class ProjectTransitivityFilter
33 extends AbstractArtifactsFilter
34 {
35
36 private boolean excludeTransitive;
37
38 private Set directDependencies;
39
40 public ProjectTransitivityFilter( Set directDependencies, boolean excludeTransitive )
41 {
42 this.excludeTransitive = excludeTransitive;
43 this.directDependencies = directDependencies;
44 }
45
46 public Set filter( Set artifacts )
47 {
48 // why not just take the directDependencies here?
49 // because if this filter is run after some other process, the
50 // set of artifacts may not be the same as the directDependencies.
51 Set result = artifacts;
52
53 if ( excludeTransitive )
54 {
55 result = new HashSet();
56 Iterator iterator = artifacts.iterator();
57 while ( iterator.hasNext() )
58 {
59 Artifact artifact = (Artifact) iterator.next();
60 if ( artifactIsADirectDependency( artifact ) )
61 {
62 result.add( artifact );
63 }
64 }
65 }
66 return result;
67 }
68
69 /**
70 * Compares the artifact to the list of dependencies to see if it is directly included by this project
71 *
72 * @param artifact representing the item to compare.
73 * @return true if artifact is a direct dependency
74 */
75 public boolean artifactIsADirectDependency( Artifact artifact )
76 {
77 boolean result = false;
78 Iterator iterator = this.directDependencies.iterator();
79 while ( iterator.hasNext() )
80 {
81 Artifact dependency = (Artifact) iterator.next();
82 if ( dependency.equals( artifact ) )
83 {
84 result = true;
85 break;
86 }
87 }
88 return result;
89 }
90
91 /**
92 * @return Returns the excludeTransitive.
93 */
94 public boolean isExcludeTransitive()
95 {
96 return this.excludeTransitive;
97 }
98
99 /**
100 * @param excludeTransitive The excludeTransitive to set.
101 */
102 public void setExcludeTransitive( boolean excludeTransitive )
103 {
104 this.excludeTransitive = excludeTransitive;
105 }
106 }