1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.util.graph.selector;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Objects;
26 import java.util.TreeSet;
27
28 import org.eclipse.aether.collection.DependencyCollectionContext;
29 import org.eclipse.aether.collection.DependencySelector;
30 import org.eclipse.aether.graph.Dependency;
31
32 import static java.util.Objects.requireNonNull;
33
34
35
36
37
38
39
40
41
42 @Deprecated
43 public final class ScopeDependencySelector implements DependencySelector {
44
45 private final boolean transitive;
46
47 private final Collection<String> included;
48
49 private final Collection<String> excluded;
50
51
52
53
54
55
56
57 public ScopeDependencySelector(Collection<String> included, Collection<String> excluded) {
58 transitive = false;
59 this.included = clone(included);
60 this.excluded = clone(excluded);
61 }
62
63 private static Collection<String> clone(Collection<String> scopes) {
64 Collection<String> copy;
65 if (scopes == null || scopes.isEmpty()) {
66
67 copy = null;
68 } else {
69 copy = new HashSet<>(scopes);
70 if (copy.size() <= 2) {
71
72 copy = new ArrayList<>(new TreeSet<>(copy));
73 }
74 }
75 return copy;
76 }
77
78
79
80
81
82
83 public ScopeDependencySelector(String... excluded) {
84 this(null, (excluded != null) ? Arrays.asList(excluded) : null);
85 }
86
87 private ScopeDependencySelector(boolean transitive, Collection<String> included, Collection<String> excluded) {
88 this.transitive = transitive;
89 this.included = included;
90 this.excluded = excluded;
91 }
92
93 public boolean selectDependency(Dependency dependency) {
94 requireNonNull(dependency, "dependency cannot be null");
95 if (!transitive) {
96 return true;
97 }
98
99 String scope = dependency.getScope();
100 return (included == null || included.contains(scope)) && (excluded == null || !excluded.contains(scope));
101 }
102
103 public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
104 requireNonNull(context, "context cannot be null");
105 if (this.transitive || context.getDependency() == null) {
106 return this;
107 }
108
109 return new ScopeDependencySelector(true, included, excluded);
110 }
111
112 @Override
113 public boolean equals(Object obj) {
114 if (this == obj) {
115 return true;
116 } else if (null == obj || !getClass().equals(obj.getClass())) {
117 return false;
118 }
119
120 ScopeDependencySelector that = (ScopeDependencySelector) obj;
121 return transitive == that.transitive
122 && Objects.equals(included, that.included)
123 && Objects.equals(excluded, that.excluded);
124 }
125
126 @Override
127 public int hashCode() {
128 int hash = 17;
129 hash = hash * 31 + (transitive ? 1 : 0);
130 hash = hash * 31 + (included != null ? included.hashCode() : 0);
131 hash = hash * 31 + (excluded != null ? excluded.hashCode() : 0);
132 return hash;
133 }
134
135 @Override
136 public String toString() {
137 return String.format(
138 "%s(included: %s, excluded: %s, transitive: %s)",
139 getClass().getSimpleName(), included, excluded, transitive);
140 }
141 }