View Javadoc
1   package org.eclipse.aether.util.filter;
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.Arrays;
23  import java.util.Collection;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.eclipse.aether.graph.Dependency;
29  import org.eclipse.aether.graph.DependencyFilter;
30  import org.eclipse.aether.graph.DependencyNode;
31  
32  /**
33   * A dependency filter based on dependency scopes. <em>Note:</em> This filter does not assume any relationships between
34   * the scopes. In particular, the filter is not aware of scopes that logically include other scopes.
35   * 
36   * @see Dependency#getScope()
37   */
38  public final class ScopeDependencyFilter
39      implements DependencyFilter
40  {
41  
42      private final Set<String> included = new HashSet<>();
43  
44      private final Set<String> excluded = new HashSet<>();
45  
46      /**
47       * Creates a new filter using the specified includes and excludes.
48       * 
49       * @param included The set of scopes to include, may be {@code null} or empty to include any scope.
50       * @param excluded The set of scopes to exclude, may be {@code null} or empty to exclude no scope.
51       */
52      public ScopeDependencyFilter( Collection<String> included, Collection<String> excluded )
53      {
54          if ( included != null )
55          {
56              this.included.addAll( included );
57          }
58          if ( excluded != null )
59          {
60              this.excluded.addAll( excluded );
61          }
62      }
63  
64      /**
65       * Creates a new filter using the specified excludes.
66       * 
67       * @param excluded The set of scopes to exclude, may be {@code null} or empty to exclude no scope.
68       */
69      public ScopeDependencyFilter( String... excluded )
70      {
71          if ( excluded != null )
72          {
73              this.excluded.addAll( Arrays.asList( excluded ) );
74          }
75      }
76  
77      public boolean accept( DependencyNode node, List<DependencyNode> parents )
78      {
79          Dependency dependency = node.getDependency();
80  
81          if ( dependency == null )
82          {
83              return true;
84          }
85  
86          String scope = node.getDependency().getScope();
87          return ( included.isEmpty() || included.contains( scope ) )
88              && ( excluded.isEmpty() || !excluded.contains( scope ) );
89      }
90  
91      @Override
92      public boolean equals( Object obj )
93      {
94          if ( this == obj )
95          {
96              return true;
97          }
98  
99          if ( obj == null || !getClass().equals( obj.getClass() ) )
100         {
101             return false;
102         }
103 
104         ScopeDependencyFilter that = (ScopeDependencyFilter) obj;
105 
106         return this.included.equals( that.included ) && this.excluded.equals( that.excluded );
107     }
108 
109     @Override
110     public int hashCode()
111     {
112         int hash = 17;
113         hash = hash * 31 + included.hashCode();
114         hash = hash * 31 + excluded.hashCode();
115         return hash;
116     }
117 
118 }