View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl.scope;
20  
21  import java.util.Objects;
22  
23  import org.eclipse.aether.collection.DependencyCollectionContext;
24  import org.eclipse.aether.collection.DependencySelector;
25  import org.eclipse.aether.graph.Dependency;
26  
27  import static java.util.Objects.requireNonNull;
28  
29  /**
30   * A dependency selector that excludes optional dependencies which occur beyond given level.
31   * <p>
32   * <em>Important note:</em> equals/hashCode must factor in starting state, as instances of this class
33   * (potentially differentially configured) are used now in session, but are kept in a set.
34   *
35   * @see Dependency#isOptional()
36   */
37  public final class OptionalDependencySelector implements DependencySelector {
38      /**
39       * Excludes optional dependencies always (from root).
40       */
41      public static OptionalDependencySelector fromRoot() {
42          return from(1);
43      }
44  
45      /**
46       * Excludes optional transitive dependencies of direct dependencies.
47       */
48      public static OptionalDependencySelector fromDirect() {
49          return from(2);
50      }
51  
52      /**
53       * Excludes optional transitive dependencies from given depth (1=root, 2=direct, 3=transitives of direct ones...).
54       */
55      public static OptionalDependencySelector from(int applyFrom) {
56          if (applyFrom < 1) {
57              throw new IllegalArgumentException("applyFrom must be non-zero and positive");
58          }
59          return new OptionalDependencySelector(Objects.hash(applyFrom), 0, applyFrom);
60      }
61  
62      private final int seed;
63      private final int depth;
64      private final int applyFrom;
65  
66      private OptionalDependencySelector(int seed, int depth, int applyFrom) {
67          this.seed = seed;
68          this.depth = depth;
69          this.applyFrom = applyFrom;
70      }
71  
72      @Override
73      public boolean selectDependency(Dependency dependency) {
74          requireNonNull(dependency, "dependency cannot be null");
75          return depth < applyFrom || !dependency.isOptional();
76      }
77  
78      @Override
79      public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
80          requireNonNull(context, "context cannot be null");
81          return new OptionalDependencySelector(seed, depth + 1, applyFrom);
82      }
83  
84      @Override
85      public boolean equals(Object obj) {
86          if (this == obj) {
87              return true;
88          } else if (null == obj || !getClass().equals(obj.getClass())) {
89              return false;
90          }
91  
92          OptionalDependencySelector that = (OptionalDependencySelector) obj;
93          return seed == that.seed && depth == that.depth && applyFrom == that.applyFrom;
94      }
95  
96      @Override
97      public int hashCode() {
98          int hash = getClass().hashCode();
99          hash = hash * 31 + seed;
100         hash = hash * 31 + depth;
101         hash = hash * 31 + applyFrom;
102         return hash;
103     }
104 
105     @Override
106     public String toString() {
107         return String.format("%s(applied: %s)", this.getClass().getSimpleName(), depth >= applyFrom);
108     }
109 }