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