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