View Javadoc
1   package org.apache.maven.shared.artifact.resolver;
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  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
26  
27  /**
28   * Filter to only retain objects in the given scope or better. This implementation allows the 
29   * accumulation of multiple scopes and their associated implied scopes, so that the user can filter
30   * apply a series of implication rules in a single step. This should be a more efficient implementation
31   * of multiple standard {@link org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter} instances ORed together.
32   *
33   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
34   * @author jdcasey
35   * @version $Id: CumulativeScopeArtifactFilter.html 987631 2016-05-07 06:12:51Z hboutemy $
36   */
37  final class CumulativeScopeArtifactFilter
38      implements ArtifactFilter
39  {
40      private boolean compileScope;
41  
42      private boolean runtimeScope;
43  
44      private boolean testScope;
45  
46      private boolean providedScope;
47  
48      private boolean systemScope;
49  
50      /**
51       * Create a new filter with all scopes disabled.
52       */
53      CumulativeScopeArtifactFilter()
54      {
55      }
56      
57      /**
58       * Create a new filter with the specified scope and its implied scopes enabled.
59       * @param scope The scope to enable, along with all implied scopes.
60       */
61      CumulativeScopeArtifactFilter( String scope )
62      {
63          addScope( scope );
64      }
65  
66      /**
67       * Create a new filter with the specified scopes and their implied scopes enabled.
68       * 
69       * @param scopes The scopes to enable, along with all implied scopes, may be {@code null}.
70       */
71      CumulativeScopeArtifactFilter( Collection<String> scopes )
72      {
73          if ( scopes != null )
74          {
75              for ( String scope : scopes )
76              {
77                  addScope( scope );
78              }
79          }
80      }
81  
82      /**
83       * Enable a new scope, along with its implied scopes, in this filter.
84       * @param scope The scope to enable, along with all implied scopes.
85       */
86      void addScope( String scope )
87      {
88          if ( Artifact.SCOPE_COMPILE.equals( scope ) )
89          {
90              systemScope = true;
91              providedScope = true;
92              compileScope = true;
93          }
94          else if ( Artifact.SCOPE_RUNTIME.equals( scope ) )
95          {
96              compileScope = true;
97              runtimeScope = true;
98          }
99          else if ( Artifact.SCOPE_TEST.equals( scope ) )
100         {
101             systemScope = true;
102             providedScope = true;
103             compileScope = true;
104             runtimeScope = true;
105             testScope = true;
106         }
107         else if ( Artifact.SCOPE_PROVIDED.equals( scope ) )
108         {
109             providedScope = true;
110         }
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     public boolean include( Artifact artifact )
117     {
118         if ( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) )
119         {
120             return compileScope;
121         }
122         else if ( Artifact.SCOPE_RUNTIME.equals( artifact.getScope() ) )
123         {
124             return runtimeScope;
125         }
126         else if ( Artifact.SCOPE_TEST.equals( artifact.getScope() ) )
127         {
128             return testScope;
129         }
130         else if ( Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ) )
131         {
132             return providedScope;
133         }
134         else if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) )
135         {
136             return systemScope;
137         }
138         else
139         {
140             return true;
141         }
142     }
143 }