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 public InputLocation getLocation(Object key) {
72 return locations.get(key);
73 }
74
75
76
77
78 public Set<Object> getLocationKeys() {
79 return locations.keySet();
80 }
81
82 protected Stream<Object> getLocationKeyStream() {
83 return locations.keySet().stream();
84 }
85
86
87
88
89 public InputLocation getImportedFrom()
90 {
91 return importedFrom;
92 }
93
94
95
96
97
98
99 @Nonnull
100 public Builder with() {
101 return newBuilder(this);
102 }
103
104
105
106
107
108
109 @Nonnull
110 public DependencyManagement withDependencies(Collection<Dependency> dependencies) {
111 return newBuilder(this, true).dependencies(dependencies).build();
112 }
113
114
115
116
117
118
119
120
121 @Nonnull
122 public static DependencyManagement newInstance() {
123 return newInstance(true);
124 }
125
126
127
128
129
130
131
132
133 @Nonnull
134 public static DependencyManagement newInstance(boolean withDefaults) {
135 return newBuilder(withDefaults).build();
136 }
137
138
139
140
141
142
143
144
145 @Nonnull
146 public static Builder newBuilder() {
147 return newBuilder(true);
148 }
149
150
151
152
153
154
155
156 @Nonnull
157 public static Builder newBuilder(boolean withDefaults) {
158 return new Builder(withDefaults);
159 }
160
161
162
163
164
165
166
167
168 @Nonnull
169 public static Builder newBuilder(DependencyManagement from) {
170 return newBuilder(from, false);
171 }
172
173
174
175
176
177
178
179
180 @Nonnull
181 public static Builder newBuilder(DependencyManagement from, boolean forceCopy) {
182 return new Builder(from, forceCopy);
183 }
184
185
186
187
188
189
190 @NotThreadSafe
191 public static class Builder
192 {
193 DependencyManagement base;
194 Collection<Dependency> dependencies;
195 Map<Object, InputLocation> locations;
196 InputLocation importedFrom;
197
198 protected Builder(boolean withDefaults) {
199 if (withDefaults) {
200 }
201 }
202
203 protected Builder(DependencyManagement base, boolean forceCopy) {
204 if (forceCopy) {
205 this.dependencies = base.dependencies;
206 this.locations = base.locations;
207 this.importedFrom = base.importedFrom;
208 } else {
209 this.base = base;
210 }
211 }
212
213 @Nonnull
214 public Builder dependencies(Collection<Dependency> dependencies) {
215 this.dependencies = dependencies;
216 return this;
217 }
218
219
220 @Nonnull
221 public Builder location(Object key, InputLocation location) {
222 if (location != null) {
223 if (!(this.locations instanceof HashMap)) {
224 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
225 }
226 this.locations.put(key, location);
227 }
228 return this;
229 }
230
231 @Nonnull
232 public Builder importedFrom(InputLocation importedFrom) {
233 this.importedFrom = importedFrom;
234 return this;
235 }
236
237 @Nonnull
238 public DependencyManagement build() {
239
240 if (base != null
241 && (dependencies == null || dependencies == base.dependencies)
242 ) {
243 return base;
244 }
245 return new DependencyManagement(this);
246 }
247
248 Map<Object, InputLocation> computeLocations() {
249 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
250 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
251 if (newlocs.isEmpty()) {
252 return Map.copyOf(oldlocs);
253 }
254 if (oldlocs.isEmpty()) {
255 return Map.copyOf(newlocs);
256 }
257 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
258
259 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
260 }
261 }
262
263 }