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 @Experimental
30 @Generated @ThreadSafe @Immutable
31 public class PluginContainer
32 implements Serializable, InputLocationTracker
33 {
34
35
36
37 final List<Plugin> plugins;
38
39 final Map<Object, InputLocation> locations;
40
41 final InputLocation importedFrom;
42
43
44
45
46
47 protected PluginContainer(Builder builder) {
48 this.plugins = ImmutableCollections.copy(builder.plugins != null ? builder.plugins : (builder.base != null ? builder.base.plugins : null));
49 this.locations = builder.computeLocations();
50 this.importedFrom = builder.importedFrom;
51 }
52
53
54
55
56
57
58 @Nonnull
59 public List<Plugin> getPlugins() {
60 return this.plugins;
61 }
62
63
64
65
66 public InputLocation getLocation(Object key) {
67 return locations.get(key);
68 }
69
70
71
72
73 public Set<Object> getLocationKeys() {
74 return locations.keySet();
75 }
76
77 protected Stream<Object> getLocationKeyStream() {
78 return locations.keySet().stream();
79 }
80
81
82
83
84 public InputLocation getImportedFrom() {
85 return importedFrom;
86 }
87
88
89
90
91
92
93 @Nonnull
94 public Builder with() {
95 return newBuilder(this);
96 }
97
98
99
100
101
102
103 @Nonnull
104 public PluginContainer withPlugins(Collection<Plugin> plugins) {
105 return newBuilder(this, true).plugins(plugins).build();
106 }
107
108
109
110
111
112
113
114
115 @Nonnull
116 public static PluginContainer newInstance() {
117 return newInstance(true);
118 }
119
120
121
122
123
124
125
126
127 @Nonnull
128 public static PluginContainer newInstance(boolean withDefaults) {
129 return newBuilder(withDefaults).build();
130 }
131
132
133
134
135
136
137
138
139 @Nonnull
140 public static Builder newBuilder() {
141 return newBuilder(true);
142 }
143
144
145
146
147
148
149
150 @Nonnull
151 public static Builder newBuilder(boolean withDefaults) {
152 return new Builder(withDefaults);
153 }
154
155
156
157
158
159
160
161
162 @Nonnull
163 public static Builder newBuilder(PluginContainer from) {
164 return newBuilder(from, false);
165 }
166
167
168
169
170
171
172
173
174 @Nonnull
175 public static Builder newBuilder(PluginContainer from, boolean forceCopy) {
176 return new Builder(from, forceCopy);
177 }
178
179
180
181
182
183
184 @NotThreadSafe
185 public static class Builder
186 {
187 PluginContainer base;
188 Collection<Plugin> plugins;
189 Map<Object, InputLocation> locations;
190 InputLocation importedFrom;
191
192 protected Builder(boolean withDefaults) {
193 if (withDefaults) {
194 }
195 }
196
197 protected Builder(PluginContainer base, boolean forceCopy) {
198 if (forceCopy) {
199 this.plugins = base.plugins;
200 this.locations = base.locations;
201 this.importedFrom = base.importedFrom;
202 } else {
203 this.base = base;
204 }
205 }
206
207 @Nonnull
208 public Builder plugins(Collection<Plugin> plugins) {
209 this.plugins = plugins;
210 return this;
211 }
212
213
214 @Nonnull
215 public Builder location(Object key, InputLocation location) {
216 if (location != null) {
217 if (!(this.locations instanceof HashMap)) {
218 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
219 }
220 this.locations.put(key, location);
221 }
222 return this;
223 }
224
225 @Nonnull
226 public Builder importedFrom(InputLocation importedFrom) {
227 this.importedFrom = importedFrom;
228 return this;
229 }
230
231 @Nonnull
232 public PluginContainer build() {
233
234 if (base != null
235 && (plugins == null || plugins == base.plugins)
236 ) {
237 return base;
238 }
239 return new PluginContainer(this);
240 }
241
242 Map<Object, InputLocation> computeLocations() {
243 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
244 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
245 if (newlocs.isEmpty()) {
246 return Map.copyOf(oldlocs);
247 }
248 if (oldlocs.isEmpty()) {
249 return Map.copyOf(newlocs);
250 }
251 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
252
253 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
254 }
255 }
256
257
258
259 volatile Map<String, Plugin> pluginMap;
260
261
262
263
264
265 public Map<String, Plugin> getPluginsAsMap() {
266 if (pluginMap == null) {
267 synchronized (this) {
268 if (pluginMap == null) {
269 pluginMap = ImmutableCollections.copy(plugins.stream().collect(
270 java.util.stream.Collectors.toMap(
271 Plugin::getKey, java.util.function.Function.identity())));
272 }
273 }
274 }
275 return pluginMap;
276 }
277
278
279
280
281 @Override
282 public String toString()
283 {
284 return "PluginContainer {}";
285 }
286
287
288 }