1 package org.eclipse.aether.util.graph.selector;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
30
31
32
33 public final class OptionalDependencySelector
34 implements DependencySelector
35 {
36
37 private final int depth;
38
39
40
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 }