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 org.apache.maven.api.annotations.Experimental;
15 import org.apache.maven.api.annotations.Generated;
16 import org.apache.maven.api.annotations.Immutable;
17 import org.apache.maven.api.annotations.Nonnull;
18 import org.apache.maven.api.annotations.NotThreadSafe;
19 import org.apache.maven.api.annotations.ThreadSafe;
20
21
22
23
24 @Experimental
25 @Generated @ThreadSafe @Immutable
26 public class PluginContainer
27 implements Serializable, InputLocationTracker
28 {
29
30
31
32 final List<Plugin> plugins;
33
34 final InputLocation location;
35
36 final InputLocation pluginsLocation;
37
38 final Map<Object, InputLocation> locations;
39
40
41
42
43
44 PluginContainer(
45 Collection<Plugin> plugins,
46 Map<Object, InputLocation> locations,
47 InputLocation location,
48 InputLocation pluginsLocation
49 )
50 {
51 this.plugins = ImmutableCollections.copy( plugins );
52 this.locations = ImmutableCollections.copy( locations );
53 this.location = location;
54 this.pluginsLocation = pluginsLocation;
55 }
56
57
58
59
60
61
62 @Nonnull
63 public List<Plugin> getPlugins()
64 {
65 return this.plugins;
66 }
67
68
69
70
71 public InputLocation getLocation( Object key )
72 {
73 if ( key instanceof String )
74 {
75 switch ( ( String ) key )
76 {
77 case "":
78 return location;
79 case "plugins":
80 return pluginsLocation;
81 }
82 }
83 return locations != null ? locations.get( key ) : null;
84 }
85
86
87
88
89
90
91 @Nonnull
92 public Builder with()
93 {
94 return newBuilder( this );
95 }
96
97
98
99
100
101
102 @Nonnull
103 public PluginContainer withPlugins( Collection<Plugin> plugins )
104 {
105 return with().plugins( plugins ).build();
106 }
107
108
109
110
111
112
113
114
115 @Nonnull
116 public static PluginContainer newInstance()
117 {
118 return newInstance( true );
119 }
120
121
122
123
124
125
126
127
128 @Nonnull
129 public static PluginContainer newInstance( boolean withDefaults )
130 {
131 return newBuilder( withDefaults ).build();
132 }
133
134
135
136
137
138
139
140
141 @Nonnull
142 public static Builder newBuilder()
143 {
144 return newBuilder( true );
145 }
146
147
148
149
150
151
152
153 @Nonnull
154 public static Builder newBuilder( boolean withDefaults )
155 {
156 return new Builder( withDefaults );
157 }
158
159
160
161
162
163
164
165
166 @Nonnull
167 public static Builder newBuilder( PluginContainer from )
168 {
169 return newBuilder( from, false );
170 }
171
172
173
174
175
176
177
178
179 @Nonnull
180 public static Builder newBuilder( PluginContainer from, boolean forceCopy )
181 {
182 return new Builder( from, forceCopy );
183 }
184
185
186
187
188
189
190 @NotThreadSafe
191 public static class Builder
192 {
193 PluginContainer base;
194 Collection<Plugin> plugins;
195 Map<Object, InputLocation> locations;
196
197 Builder( boolean withDefaults )
198 {
199 if ( withDefaults )
200 {
201 }
202 }
203
204 Builder( PluginContainer base, boolean forceCopy )
205 {
206 if ( forceCopy )
207 {
208 this.plugins = base.plugins;
209 }
210 else
211 {
212 this.base = base;
213 }
214 }
215
216 @Nonnull
217 public Builder plugins( Collection<Plugin> plugins )
218 {
219 this.plugins = plugins;
220 return this;
221 }
222
223
224 @Nonnull
225 public Builder location( Object key, InputLocation location )
226 {
227 if ( location != null )
228 {
229 if ( this.locations == null )
230 {
231 this.locations = new HashMap<>();
232 }
233 this.locations.put( key, location );
234 }
235 return this;
236 }
237
238 @Nonnull
239 public PluginContainer build()
240 {
241 if ( base != null
242 && ( plugins == null || plugins == base.plugins )
243 )
244 {
245 return base;
246 }
247 Map<Object, InputLocation> locations = null;
248 InputLocation location = null;
249 InputLocation pluginsLocation = null;
250 if ( this.locations != null )
251 {
252 locations = this.locations;
253 location = locations.remove( "" );
254 pluginsLocation = locations.remove( "plugins" );
255 }
256 return new PluginContainer(
257 plugins != null ? plugins : ( base != null ? base.plugins : null ),
258 locations != null ? locations : ( base != null ? base.locations : null ),
259 location != null ? location : ( base != null ? base.location : null ),
260 pluginsLocation != null ? pluginsLocation : ( base != null ? base.pluginsLocation : null )
261 );
262 }
263 }
264
265
266
267 Map<String, Plugin> pluginMap;
268
269
270
271
272 public synchronized void flushPluginMap()
273 {
274 this.pluginMap = null;
275 }
276
277
278
279
280
281 public synchronized Map<String, Plugin> getPluginsAsMap()
282 {
283 if ( pluginMap == null )
284 {
285 pluginMap = new java.util.LinkedHashMap<String, Plugin>();
286 if ( getPlugins() != null )
287 {
288 for ( java.util.Iterator<Plugin> it = getPlugins().iterator(); it.hasNext(); )
289 {
290 Plugin plugin = (Plugin) it.next();
291 pluginMap.put( plugin.getKey(), plugin );
292 }
293 }
294 }
295
296 return pluginMap;
297 }
298
299
300
301
302 @Override
303 public String toString()
304 {
305 return "PluginContainer {}";
306 }
307
308
309 }