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