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