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