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  /**
31   * A dependency filter that combines zero or more other filters using a logical {@code OR}.
32   */
33  public final class OrDependencyFilter implements DependencyFilter {
34  
35      private final Set<DependencyFilter> filters = new LinkedHashSet<>();
36  
37      /**
38       * Creates a new filter from the specified filters.
39       *
40       * @param filters The filters to combine, may be {@code null}.
41       */
42      public OrDependencyFilter(DependencyFilter... filters) {
43          if (filters != null) {
44              Collections.addAll(this.filters, filters);
45          }
46      }
47  
48      /**
49       * Creates a new filter from the specified filters.
50       *
51       * @param filters The filters to combine, may be {@code null}.
52       */
53      public OrDependencyFilter(Collection<DependencyFilter> filters) {
54          if (filters != null) {
55              this.filters.addAll(filters);
56          }
57      }
58  
59      /**
60       * Creates a new filter from the specified filters.
61       *
62       * @param filter1 The first filter to combine, may be {@code null}.
63       * @param filter2 The first filter to combine, may be {@code null}.
64       * @return The combined filter or {@code null} if both filter were {@code null}.
65       */
66      public static DependencyFilter newInstance(DependencyFilter filter1, DependencyFilter filter2) {
67          if (filter1 == null) {
68              return filter2;
69          } else if (filter2 == null) {
70              return filter1;
71          }
72          return new OrDependencyFilter(filter1, filter2);
73      }
74  
75      public boolean accept(DependencyNode node, List<DependencyNode> parents) {
76          for (DependencyFilter filter : filters) {
77              if (filter.accept(node, parents)) {
78                  return true;
79              }
80          }
81          return false;
82      }
83  
84      @Override
85      public boolean equals(Object obj) {
86          if (this == obj) {
87              return true;
88          }
89  
90          if (obj == null || !getClass().equals(obj.getClass())) {
91              return false;
92          }
93  
94          OrDependencyFilter that = (OrDependencyFilter) obj;
95  
96          return this.filters.equals(that.filters);
97      }
98  
99      @Override
100     public int hashCode() {
101         int hash = getClass().hashCode();
102         hash = hash * 31 + filters.hashCode();
103         return hash;
104     }
105 }