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.Collection;
23  import java.util.Collections;
24  import java.util.LinkedHashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.eclipse.aether.graph.DependencyFilter;
29  import org.eclipse.aether.graph.DependencyNode;
30  
31  /**
32   * A dependency filter that combines zero or more other filters using a logical {@code AND}. The resulting filter
33   * accepts a given dependency node if and only if all constituent filters accept it.
34   */
35  public final class AndDependencyFilter
36      implements DependencyFilter
37  {
38  
39      private final Set<DependencyFilter> filters = new LinkedHashSet<DependencyFilter>();
40  
41      /**
42       * Creates a new filter from the specified filters. Prefer {@link #newInstance(DependencyFilter, DependencyFilter)}
43       * if any of the input filters might be {@code null}.
44       * 
45       * @param filters The filters to combine, may be {@code null} but must not contain {@code null} elements.
46       */
47      public AndDependencyFilter( DependencyFilter... filters )
48      {
49          if ( filters != null )
50          {
51              Collections.addAll( this.filters, filters );
52          }
53      }
54  
55      /**
56       * Creates a new filter from the specified filters.
57       * 
58       * @param filters The filters to combine, may be {@code null} but must not contain {@code null} elements.
59       */
60      public AndDependencyFilter( Collection<DependencyFilter> filters )
61      {
62          if ( filters != null )
63          {
64              this.filters.addAll( filters );
65          }
66      }
67  
68      /**
69       * Creates a new filter from the specified filters.
70       * 
71       * @param filter1 The first filter to combine, may be {@code null}.
72       * @param filter2 The second filter to combine, may be {@code null}.
73       * @return The combined filter or {@code null} if both filter were {@code null}.
74       */
75      public static DependencyFilter newInstance( DependencyFilter filter1, DependencyFilter filter2 )
76      {
77          if ( filter1 == null )
78          {
79              return filter2;
80          }
81          else if ( filter2 == null )
82          {
83              return filter1;
84          }
85          return new AndDependencyFilter( filter1, filter2 );
86      }
87  
88      public boolean accept( DependencyNode node, List<DependencyNode> parents )
89      {
90          for ( DependencyFilter filter : filters )
91          {
92              if ( !filter.accept( node, parents ) )
93              {
94                  return false;
95              }
96          }
97          return true;
98      }
99  
100     @Override
101     public boolean equals( Object obj )
102     {
103         if ( this == obj )
104         {
105             return true;
106         }
107 
108         if ( obj == null || !getClass().equals( obj.getClass() ) )
109         {
110             return false;
111         }
112 
113         AndDependencyFilter that = (AndDependencyFilter) obj;
114 
115         return this.filters.equals( that.filters );
116     }
117 
118     @Override
119     public int hashCode()
120     {
121         int hash = getClass().hashCode();
122         hash = hash * 31 + filters.hashCode();
123         return hash;
124     }
125 
126 }