View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //   Generated by Maven, any modifications will be overwritten.
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   * Contains the plugins informations for the project.
22   */
23  @Experimental
24  @Generated @ThreadSafe @Immutable
25  public class PluginContainer
26      implements Serializable, InputLocationTracker
27  {
28      /**
29       * The list of plugins to use.
30       */
31      final List<Plugin> plugins;
32      /** Location of the xml element for this object. */
33      final InputLocation location;
34      /** Location of the xml element for the field plugins. */
35      final InputLocation pluginsLocation;
36      /** Other locations */
37      final Map<Object, InputLocation> locations;
38  
39      /**
40        * Constructor for this class, package protected.
41        * @see Builder#build()
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       * The list of plugins to use.
58       *
59       * @return a {@code List<Plugin>}
60       */
61      @Nonnull
62      public List<Plugin> getPlugins()
63      {
64          return this.plugins;
65      }
66  
67      /**
68       * Gets the location of the specified field in the input source.
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       * Creates a new builder with this object as the basis.
87       *
88       * @return a {@code Builder}
89       */
90      @Nonnull
91      public Builder with()
92      {
93          return newBuilder( this );
94      }
95      /**
96       * Creates a new {@code PluginContainer} instance using the specified plugins.
97       *
98       * @param plugins the new {@code Collection<Plugin>} to use
99       * @return a {@code PluginContainer} with the specified plugins
100      */
101     @Nonnull
102     public PluginContainer withPlugins( Collection<Plugin> plugins )
103     {
104         return with().plugins( plugins ).build();
105     }
106 
107     /**
108      * Creates a new {@code PluginContainer} instance.
109      * Equivalent to {@code newInstance( true )}.
110      * @see #newInstance(boolean)
111      *
112      * @return a new {@code PluginContainer}
113      */
114     @Nonnull
115     public static PluginContainer newInstance()
116     {
117         return newInstance( true );
118     }
119 
120     /**
121      * Creates a new {@code PluginContainer} instance using default values or not.
122      * Equivalent to {@code newBuilder( withDefaults ).build()}.
123      *
124      * @param withDefaults the boolean indicating whether default values should be used
125      * @return a new {@code PluginContainer}
126      */
127     @Nonnull
128     public static PluginContainer newInstance( boolean withDefaults )
129     {
130         return newBuilder( withDefaults ).build();
131     }
132 
133     /**
134      * Creates a new {@code PluginContainer} builder instance.
135      * Equivalent to {@code newBuilder( true )}.
136      * @see #newBuilder(boolean)
137      *
138      * @return a new {@code Builder}
139      */
140     @Nonnull
141     public static Builder newBuilder()
142     {
143         return newBuilder( true );
144     }
145 
146     /**
147      * Creates a new {@code PluginContainer} builder instance using default values or not.
148      *
149      * @param withDefaults the boolean indicating whether default values should be used
150      * @return a new {@code Builder}
151      */
152     @Nonnull
153     public static Builder newBuilder( boolean withDefaults )
154     {
155         return new Builder( withDefaults );
156     }
157 
158     /**
159      * Creates a new {@code PluginContainer} builder instance using the specified object as a basis.
160      * Equivalent to {@code newBuilder( from, false )}.
161      *
162      * @param from the {@code PluginContainer} instance to use as a basis
163      * @return a new {@code Builder}
164      */
165     @Nonnull
166     public static Builder newBuilder( PluginContainer from )
167     {
168         return newBuilder( from, false );
169     }
170 
171     /**
172      * Creates a new {@code PluginContainer} builder instance using the specified object as a basis.
173      *
174      * @param from the {@code PluginContainer} instance to use as a basis
175      * @param forceCopy the boolean indicating if a copy should be forced
176      * @return a new {@code Builder}
177      */
178     @Nonnull
179     public static Builder newBuilder( PluginContainer from, boolean forceCopy )
180     {
181         return new Builder( from, forceCopy );
182     }
183 
184     /**
185      * Builder class used to create PluginContainer instances.
186      * @see #with()
187      * @see #newBuilder()
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      * Reset the {@code pluginsMap} field to {@code null}
270      */
271     public synchronized void flushPluginMap()
272     {
273         this.pluginMap = null;
274     }
275 
276     /**
277      * @return a Map of plugins field with {@code Plugins#getKey()} as key
278      * @see Plugin#getKey()
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 }