View Javadoc
1   package org.eclipse.aether.util.graph.selector;
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  import static java.util.Objects.requireNonNull;
27  
28  /**
29   * A dependency selector that excludes optional dependencies which occur beyond level one of the dependency graph.
30   * 
31   * @see Dependency#isOptional()
32   */
33  public final class OptionalDependencySelector
34      implements DependencySelector
35  {
36  
37      private final int depth;
38  
39      /**
40       * Creates a new selector to exclude optional transitive dependencies.
41       */
42      public OptionalDependencySelector()
43      {
44          depth = 0;
45      }
46  
47      private OptionalDependencySelector( int depth )
48      {
49          this.depth = depth;
50      }
51  
52      public boolean selectDependency( Dependency dependency )
53      {
54          requireNonNull( dependency, "dependency cannot be null" );
55          return depth < 2 || !dependency.isOptional();
56      }
57  
58      public DependencySelector deriveChildSelector( DependencyCollectionContext context )
59      {
60          requireNonNull( context, "context cannot be null" );
61          if ( depth >= 2 )
62          {
63              return this;
64          }
65  
66          return new OptionalDependencySelector( depth + 1 );
67      }
68  
69      @Override
70      public boolean equals( Object obj )
71      {
72          if ( this == obj )
73          {
74              return true;
75          }
76          else if ( null == obj || !getClass().equals( obj.getClass() ) )
77          {
78              return false;
79          }
80  
81          OptionalDependencySelector that = (OptionalDependencySelector) obj;
82          return depth == that.depth;
83      }
84  
85      @Override
86      public int hashCode()
87      {
88          int hash = getClass().hashCode();
89          hash = hash * 31 + depth;
90          return hash;
91      }
92  
93      @Override
94      public String toString()
95      {
96          return String.format( "%s(depth: %d)", this.getClass().getSimpleName(), this.depth );
97      }
98  
99  }