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