View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //  Generated by Modello Velocity from model.vm
3   //  template, any modifications will be overwritten.
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   * Contains the plugins information for the project.
24   */
25  @Experimental
26  @Generated @ThreadSafe @Immutable
27  public class PluginContainer
28      implements Serializable, InputLocationTracker
29  {
30      /**
31       * The list of plugins to use.
32       */
33      final List<Plugin> plugins;
34      /** Locations (this potentially hides the same name field from the super class) */
35      final Map<Object, InputLocation> locations;
36      /** Location tracking */
37      final InputLocation importedFrom;
38  
39      /**
40        * Constructor for this class, to be called from its subclasses and {@link Builder}.
41        * @see Builder#build()
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       * The list of plugins to use.
56       *
57       * @return a {@code List<Plugin>}
58       */
59      @Nonnull
60      public List<Plugin> getPlugins() {
61          return this.plugins;
62      }
63  
64      /**
65       * Gets the location of the specified field in the input source.
66       */
67      public InputLocation getLocation(Object key) {
68          return locations != null ? locations.get(key) : null;
69      }
70  
71      /**
72      * Gets the keys of the locations of the input source.
73      */
74      public Set<Object> getLocationKeys() {
75          return locations != null ? locations.keySet() : null;
76      }
77  
78      /**
79       * Gets the input location that caused this model to be read.
80       */
81      public InputLocation getImportedFrom()
82      {
83          return importedFrom;
84      }
85  
86      /**
87       * Creates a new builder with this object as the basis.
88       *
89       * @return a {@code Builder}
90       */
91      @Nonnull
92      public Builder with() {
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         return newBuilder(this, true).plugins(plugins).build();
104     }
105 
106     /**
107      * Creates a new {@code PluginContainer} instance.
108      * Equivalent to {@code newInstance(true)}.
109      * @see #newInstance(boolean)
110      *
111      * @return a new {@code PluginContainer}
112      */
113     @Nonnull
114     public static PluginContainer newInstance() {
115         return newInstance(true);
116     }
117 
118     /**
119      * Creates a new {@code PluginContainer} instance using default values or not.
120      * Equivalent to {@code newBuilder(withDefaults).build()}.
121      *
122      * @param withDefaults the boolean indicating whether default values should be used
123      * @return a new {@code PluginContainer}
124      */
125     @Nonnull
126     public static PluginContainer newInstance(boolean withDefaults) {
127         return newBuilder(withDefaults).build();
128     }
129 
130     /**
131      * Creates a new {@code PluginContainer} builder instance.
132      * Equivalent to {@code newBuilder(true)}.
133      * @see #newBuilder(boolean)
134      *
135      * @return a new {@code Builder}
136      */
137     @Nonnull
138     public static Builder newBuilder() {
139         return newBuilder(true);
140     }
141 
142     /**
143      * Creates a new {@code PluginContainer} builder instance using default values or not.
144      *
145      * @param withDefaults the boolean indicating whether default values should be used
146      * @return a new {@code Builder}
147      */
148     @Nonnull
149     public static Builder newBuilder(boolean withDefaults) {
150         return new Builder(withDefaults);
151     }
152 
153     /**
154      * Creates a new {@code PluginContainer} builder instance using the specified object as a basis.
155      * Equivalent to {@code newBuilder(from, false)}.
156      *
157      * @param from the {@code PluginContainer} instance to use as a basis
158      * @return a new {@code Builder}
159      */
160     @Nonnull
161     public static Builder newBuilder(PluginContainer from) {
162         return newBuilder(from, false);
163     }
164 
165     /**
166      * Creates a new {@code PluginContainer} builder instance using the specified object as a basis.
167      *
168      * @param from the {@code PluginContainer} instance to use as a basis
169      * @param forceCopy the boolean indicating if a copy should be forced
170      * @return a new {@code Builder}
171      */
172     @Nonnull
173     public static Builder newBuilder(PluginContainer from, boolean forceCopy) {
174         return new Builder(from, forceCopy);
175     }
176 
177     /**
178      * Builder class used to create PluginContainer instances.
179      * @see #with()
180      * @see #newBuilder()
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             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
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      * @return a Map of plugins field with {@code Plugins#getKey()} as key
247      * @see Plugin#getKey()
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 }