1
2
3
4
5 package org.apache.maven.api.model;
6
7 import java.io.Serializable;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.Map;
11 import java.util.Objects;
12 import java.util.Optional;
13 import java.util.Set;
14 import java.util.stream.Collectors;
15 import java.util.stream.Stream;
16 import org.apache.maven.api.annotations.Experimental;
17 import org.apache.maven.api.annotations.Generated;
18 import org.apache.maven.api.annotations.Immutable;
19 import org.apache.maven.api.annotations.Nonnull;
20 import org.apache.maven.api.annotations.NotThreadSafe;
21 import org.apache.maven.api.annotations.ThreadSafe;
22
23
24
25
26 @Experimental
27 @Generated @ThreadSafe @Immutable
28 public class Prerequisites
29 implements Serializable, InputLocationTracker
30 {
31
32
33
34
35 final String maven;
36
37 final Map<Object, InputLocation> locations;
38
39 final InputLocation importedFrom;
40
41
42
43
44
45 protected Prerequisites(Builder builder) {
46 this.maven = builder.maven != null ? builder.maven : (builder.base != null ? builder.base.maven : null);
47 this.locations = builder.computeLocations();
48 this.importedFrom = builder.importedFrom;
49 }
50
51
52
53
54
55
56
57 public String getMaven() {
58 return this.maven;
59 }
60
61
62
63
64
65
66
67
68 public InputLocation getLocation(Object key) {
69 Objects.requireNonNull(key, "key");
70 return locations.get(key);
71 }
72
73
74
75
76 public Set<Object> getLocationKeys() {
77 return locations.keySet();
78 }
79
80 protected Stream<Object> getLocationKeyStream() {
81 return locations.keySet().stream();
82 }
83
84
85
86
87 public InputLocation getImportedFrom() {
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 Prerequisites withMaven(String maven) {
108 return newBuilder(this, true).maven(maven).build();
109 }
110
111
112
113
114
115
116
117
118 @Nonnull
119 public static Prerequisites newInstance() {
120 return newInstance(true);
121 }
122
123
124
125
126
127
128
129
130 @Nonnull
131 public static Prerequisites 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(Prerequisites from) {
167 return newBuilder(from, false);
168 }
169
170
171
172
173
174
175
176
177 @Nonnull
178 public static Builder newBuilder(Prerequisites 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 Prerequisites base;
191 String maven;
192 Map<Object, InputLocation> locations;
193 InputLocation importedFrom;
194
195 protected Builder(boolean withDefaults) {
196 if (withDefaults) {
197 this.maven = "2.0";
198 }
199 }
200
201 protected Builder(Prerequisites base, boolean forceCopy) {
202 if (forceCopy) {
203 this.maven = base.maven;
204 this.locations = base.locations;
205 this.importedFrom = base.importedFrom;
206 } else {
207 this.base = base;
208 }
209 }
210
211 @Nonnull
212 public Builder maven(String maven) {
213 this.maven = maven;
214 return this;
215 }
216
217
218 @Nonnull
219 public Builder location(Object key, InputLocation location) {
220 if (location != null) {
221 if (!(this.locations instanceof HashMap)) {
222 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
223 }
224 this.locations.put(key, location);
225 }
226 return this;
227 }
228
229 @Nonnull
230 public Builder importedFrom(InputLocation importedFrom) {
231 this.importedFrom = importedFrom;
232 return this;
233 }
234
235 @Nonnull
236 public Prerequisites build() {
237
238 if (base != null
239 && (maven == null || maven == base.maven)
240 ) {
241 return base;
242 }
243 return new Prerequisites(this);
244 }
245
246 Map<Object, InputLocation> computeLocations() {
247 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
248 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
249 if (newlocs.isEmpty()) {
250 return Map.copyOf(oldlocs);
251 }
252 if (oldlocs.isEmpty()) {
253 return Map.copyOf(newlocs);
254 }
255 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
256
257 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
258 }
259 }
260
261 }