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