1
2
3
4
5 package org.apache.maven.api.model;
6
7 import java.io.Serializable;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Objects;
15 import java.util.Optional;
16 import java.util.Set;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19 import org.apache.maven.api.annotations.Experimental;
20 import org.apache.maven.api.annotations.Generated;
21 import org.apache.maven.api.annotations.Immutable;
22 import org.apache.maven.api.annotations.Nonnull;
23 import org.apache.maven.api.annotations.NotThreadSafe;
24 import org.apache.maven.api.annotations.ThreadSafe;
25
26
27
28
29
30 @Experimental
31 @Generated @ThreadSafe @Immutable
32 public class DependencyManagement
33 implements Serializable, InputLocationTracker
34 {
35
36
37
38
39
40 final List<Dependency> dependencies;
41
42 final Map<Object, InputLocation> locations;
43
44 final InputLocation importedFrom;
45
46
47
48
49
50 protected DependencyManagement(Builder builder) {
51 this.dependencies = ImmutableCollections.copy(builder.dependencies != null ? builder.dependencies : (builder.base != null ? builder.base.dependencies : null));
52 this.locations = builder.computeLocations();
53 this.importedFrom = builder.importedFrom;
54 }
55
56
57
58
59
60
61
62
63 @Nonnull
64 public List<Dependency> getDependencies() {
65 return this.dependencies;
66 }
67
68
69
70
71
72
73
74
75 public InputLocation getLocation(Object key) {
76 Objects.requireNonNull(key, "key");
77 return locations.get(key);
78 }
79
80
81
82
83 public Set<Object> getLocationKeys() {
84 return locations.keySet();
85 }
86
87 protected Stream<Object> getLocationKeyStream() {
88 return locations.keySet().stream();
89 }
90
91
92
93
94 public InputLocation getImportedFrom() {
95 return importedFrom;
96 }
97
98
99
100
101
102
103 @Nonnull
104 public Builder with() {
105 return newBuilder(this);
106 }
107
108
109
110
111
112
113 @Nonnull
114 public DependencyManagement withDependencies(Collection<Dependency> dependencies) {
115 return newBuilder(this, true).dependencies(dependencies).build();
116 }
117
118
119
120
121
122
123
124
125 @Nonnull
126 public static DependencyManagement newInstance() {
127 return newInstance(true);
128 }
129
130
131
132
133
134
135
136
137 @Nonnull
138 public static DependencyManagement newInstance(boolean withDefaults) {
139 return newBuilder(withDefaults).build();
140 }
141
142
143
144
145
146
147
148
149 @Nonnull
150 public static Builder newBuilder() {
151 return newBuilder(true);
152 }
153
154
155
156
157
158
159
160 @Nonnull
161 public static Builder newBuilder(boolean withDefaults) {
162 return new Builder(withDefaults);
163 }
164
165
166
167
168
169
170
171
172 @Nonnull
173 public static Builder newBuilder(DependencyManagement from) {
174 return newBuilder(from, false);
175 }
176
177
178
179
180
181
182
183
184 @Nonnull
185 public static Builder newBuilder(DependencyManagement from, boolean forceCopy) {
186 return new Builder(from, forceCopy);
187 }
188
189
190
191
192
193
194 @NotThreadSafe
195 public static class Builder
196 {
197 DependencyManagement base;
198 Collection<Dependency> dependencies;
199 Map<Object, InputLocation> locations;
200 InputLocation importedFrom;
201
202 protected Builder(boolean withDefaults) {
203 if (withDefaults) {
204 }
205 }
206
207 protected Builder(DependencyManagement base, boolean forceCopy) {
208 if (forceCopy) {
209 this.dependencies = base.dependencies;
210 this.locations = base.locations;
211 this.importedFrom = base.importedFrom;
212 } else {
213 this.base = base;
214 }
215 }
216
217 @Nonnull
218 public Builder dependencies(Collection<Dependency> dependencies) {
219 this.dependencies = dependencies;
220 return this;
221 }
222
223
224 @Nonnull
225 public Builder location(Object key, InputLocation location) {
226 if (location != null) {
227 if (!(this.locations instanceof HashMap)) {
228 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
229 }
230 this.locations.put(key, location);
231 }
232 return this;
233 }
234
235 @Nonnull
236 public Builder importedFrom(InputLocation importedFrom) {
237 this.importedFrom = importedFrom;
238 return this;
239 }
240
241 @Nonnull
242 public DependencyManagement build() {
243
244 if (base != null
245 && (dependencies == null || dependencies == base.dependencies)
246 ) {
247 return base;
248 }
249 return new DependencyManagement(this);
250 }
251
252 Map<Object, InputLocation> computeLocations() {
253 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
254 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
255 if (newlocs.isEmpty()) {
256 return Map.copyOf(oldlocs);
257 }
258 if (oldlocs.isEmpty()) {
259 return Map.copyOf(newlocs);
260 }
261 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
262
263 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
264 }
265 }
266
267 }