View Javadoc
1   package org.apache.maven.artifact.resolver.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.HashSet;
24  import java.util.Set;
25  
26  /**
27   * Filter to only retain objects in the given scope or better. This implementation allows the accumulation of multiple
28   * scopes and their associated implied scopes, so that the user can filter apply a series of implication rules in a
29   * single step. This should be a more efficient implementation of multiple standard {@link ScopeArtifactFilter}
30   * instances ORed together.
31   * 
32   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
33   * @author jdcasey
34   */
35  public class CumulativeScopeArtifactFilter
36      extends AbstractScopeArtifactFilter
37  {
38  
39      private Set<String> scopes;
40  
41      /**
42       * Create a new filter with the specified scopes and their implied scopes enabled.
43       * 
44       * @param scopes The scopes to enable, along with all implied scopes, may be {@code null}.
45       */
46      public CumulativeScopeArtifactFilter( Collection<String> scopes )
47      {
48          this.scopes = new HashSet<String>();
49  
50          addScopes( scopes );
51      }
52  
53      /**
54       * Creates a new filter that combines the specified filters.
55       * 
56       * @param filters The filters to combine, may be {@code null}.
57       */
58      public CumulativeScopeArtifactFilter( CumulativeScopeArtifactFilter... filters )
59      {
60          this.scopes = new HashSet<String>();
61  
62          if ( filters != null )
63          {
64              for ( CumulativeScopeArtifactFilter filter : filters )
65              {
66                  addScopes( filter.getScopes() );
67              }
68          }
69      }
70  
71      private void addScopes( Collection<String> scopes )
72      {
73          if ( scopes != null )
74          {
75              for ( String scope : scopes )
76              {
77                  addScope( scope );
78              }
79          }
80      }
81  
82      private void addScope( String scope )
83      {
84          this.scopes.add( scope );
85  
86          addScopeInternal( scope );
87      }
88  
89      public Set<String> getScopes()
90      {
91          return scopes;
92      }
93  
94      @Override
95      public int hashCode()
96      {
97          int hash = 17;
98  
99          hash = hash * 31 + scopes.hashCode();
100 
101         return hash;
102     }
103 
104     @Override
105     public boolean equals( Object obj )
106     {
107         if ( this == obj )
108         {
109             return true;
110         }
111 
112         if ( !( obj instanceof CumulativeScopeArtifactFilter ) )
113         {
114             return false;
115         }
116 
117         CumulativeScopeArtifactFilter that = (CumulativeScopeArtifactFilter) obj;
118 
119         return scopes.equals( that.scopes );
120     }
121 
122 }