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