View Javadoc
1   package org.apache.maven.shared.dependency.graph.internal;
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 org.eclipse.aether.collection.DependencyCollectionContext;
23  import org.eclipse.aether.collection.DependencySelector;
24  import org.eclipse.aether.graph.Dependency;
25  
26  /**
27   * A dependency selector that excludes dependencies of an specific Scope which occur beyond level one of the dependency
28   * graph.
29   * 
30   * @see Dependency#getScope()
31   * @author Gabriel Belingueres
32   * @since 3.1.0
33   */
34  public class DirectScopeDependencySelector
35      implements DependencySelector
36  {
37  
38      private final String scope;
39  
40      private final int depth;
41  
42      public DirectScopeDependencySelector( String scope )
43      {
44          this( scope, 0 );
45      }
46  
47      private DirectScopeDependencySelector( String scope, int depth )
48      {
49          if ( scope == null )
50          {
51              throw new IllegalArgumentException( "scope is null!" );
52          }
53          this.scope = scope;
54          this.depth = depth;
55      }
56  
57      /**
58       * Decides whether the specified dependency should be included in the dependency graph.
59       * 
60       * @param dependency The dependency to check, must not be {@code null}.
61       * @return {@code false} if the dependency should be excluded from the children of the current node, {@code true}
62       *         otherwise.
63       */
64      @Override
65      public boolean selectDependency( Dependency dependency )
66      {
67          return depth < 2 || !scope.equals( dependency.getScope() );
68      }
69  
70      /**
71       * Derives a dependency selector for the specified collection context. When calculating the child selector,
72       * implementors are strongly advised to simply return the current instance if nothing changed to help save memory.
73       * 
74       * @param context The dependency collection context, must not be {@code null}.
75       * @return The dependency selector for the target node, must not be {@code null}.
76       */
77      @Override
78      public DependencySelector deriveChildSelector( DependencyCollectionContext context )
79      {
80          if ( depth >= 2 )
81          {
82              return this;
83          }
84  
85          return new DirectScopeDependencySelector( scope, depth + 1 );
86      }
87  
88      @Override
89      public int hashCode()
90      {
91          final int prime = 31;
92          int result = 1;
93          result = prime * result + depth;
94          result = prime * result + ( ( scope == null ) ? 0 : scope.hashCode() );
95          return result;
96      }
97  
98      @Override
99      public boolean equals( Object obj )
100     {
101         if ( this == obj )
102         {
103             return true;
104         }
105         if ( obj == null )
106         {
107             return false;
108         }
109         if ( getClass() != obj.getClass() )
110         {
111             return false;
112         }
113         DirectScopeDependencySelector other = (DirectScopeDependencySelector) obj;
114         if ( depth != other.depth )
115         {
116             return false;
117         }
118         if ( scope == null )
119         {
120             if ( other.scope != null )
121             {
122                 return false;
123             }
124         }
125         else if ( !scope.equals( other.scope ) )
126         {
127             return false;
128         }
129         return true;
130     }
131 
132 }