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