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.Objects;
15  import java.util.Optional;
16  import java.util.Set;
17  import java.util.stream.Collectors;
18  import java.util.stream.Stream;
19  import org.apache.maven.api.annotations.Experimental;
20  import org.apache.maven.api.annotations.Generated;
21  import org.apache.maven.api.annotations.Immutable;
22  import org.apache.maven.api.annotations.Nonnull;
23  import org.apache.maven.api.annotations.NotThreadSafe;
24  import org.apache.maven.api.annotations.ThreadSafe;
25  
26  /**
27   * Contains the plugins information for the project.
28   */
29  @Experimental
30  @Generated @ThreadSafe @Immutable
31  public class PluginContainer
32      implements Serializable, InputLocationTracker
33  {
34      /**
35       * The list of plugins to use.
36       */
37      final List<Plugin> plugins;
38      /** Locations */
39      final Map<Object, InputLocation> locations;
40      /** Location tracking */
41      final InputLocation importedFrom;
42  
43      /**
44        * Constructor for this class, to be called from its subclasses and {@link Builder}.
45        * @see Builder#build()
46        */
47      protected PluginContainer(Builder builder) {
48          this.plugins = ImmutableCollections.copy(builder.plugins != null ? builder.plugins : (builder.base != null ? builder.base.plugins : null));
49          this.locations = builder.computeLocations();
50          this.importedFrom = builder.importedFrom;
51      }
52  
53      /**
54       * The list of plugins to use.
55       *
56       * @return a {@code List<Plugin>}
57       */
58      @Nonnull
59      public List<Plugin> getPlugins() {
60          return this.plugins;
61      }
62  
63      /**
64       * Gets the location of the specified field in the input source.
65       *
66       * @param key the key of the field, must not be {@code null}
67       * @return the location of the field in the input source or {@code null} if unknown
68       * @throws NullPointerException if {@code key} is {@code null}
69       */
70      public InputLocation getLocation(Object key) {
71          Objects.requireNonNull(key, "key");
72          return locations.get(key);
73      }
74  
75      /**
76       * Gets the keys of the locations of the input source.
77       */
78      public Set<Object> getLocationKeys() {
79          return locations.keySet();
80      }
81  
82      protected Stream<Object> getLocationKeyStream() {
83          return locations.keySet().stream();
84      }
85  
86      /**
87       * Gets the input location that caused this model to be read.
88       */
89      public InputLocation getImportedFrom() {
90          return importedFrom;
91      }
92  
93      /**
94       * Creates a new builder with this object as the basis.
95       *
96       * @return a {@code Builder}
97       */
98      @Nonnull
99      public Builder with() {
100         return newBuilder(this);
101     }
102     /**
103      * Creates a new {@code PluginContainer} instance using the specified plugins.
104      *
105      * @param plugins the new {@code Collection<Plugin>} to use
106      * @return a {@code PluginContainer} with the specified plugins
107      */
108     @Nonnull
109     public PluginContainer withPlugins(Collection<Plugin> plugins) {
110         return newBuilder(this, true).plugins(plugins).build();
111     }
112 
113     /**
114      * Creates a new {@code PluginContainer} instance.
115      * Equivalent to {@code newInstance(true)}.
116      * @see #newInstance(boolean)
117      *
118      * @return a new {@code PluginContainer}
119      */
120     @Nonnull
121     public static PluginContainer newInstance() {
122         return newInstance(true);
123     }
124 
125     /**
126      * Creates a new {@code PluginContainer} instance using default values or not.
127      * Equivalent to {@code newBuilder(withDefaults).build()}.
128      *
129      * @param withDefaults the boolean indicating whether default values should be used
130      * @return a new {@code PluginContainer}
131      */
132     @Nonnull
133     public static PluginContainer newInstance(boolean withDefaults) {
134         return newBuilder(withDefaults).build();
135     }
136 
137     /**
138      * Creates a new {@code PluginContainer} builder instance.
139      * Equivalent to {@code newBuilder(true)}.
140      * @see #newBuilder(boolean)
141      *
142      * @return a new {@code Builder}
143      */
144     @Nonnull
145     public static Builder newBuilder() {
146         return newBuilder(true);
147     }
148 
149     /**
150      * Creates a new {@code PluginContainer} builder instance using default values or not.
151      *
152      * @param withDefaults the boolean indicating whether default values should be used
153      * @return a new {@code Builder}
154      */
155     @Nonnull
156     public static Builder newBuilder(boolean withDefaults) {
157         return new Builder(withDefaults);
158     }
159 
160     /**
161      * Creates a new {@code PluginContainer} builder instance using the specified object as a basis.
162      * Equivalent to {@code newBuilder(from, false)}.
163      *
164      * @param from the {@code PluginContainer} instance to use as a basis
165      * @return a new {@code Builder}
166      */
167     @Nonnull
168     public static Builder newBuilder(PluginContainer from) {
169         return newBuilder(from, false);
170     }
171 
172     /**
173      * Creates a new {@code PluginContainer} builder instance using the specified object as a basis.
174      *
175      * @param from the {@code PluginContainer} instance to use as a basis
176      * @param forceCopy the boolean indicating if a copy should be forced
177      * @return a new {@code Builder}
178      */
179     @Nonnull
180     public static Builder newBuilder(PluginContainer from, boolean forceCopy) {
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         InputLocation importedFrom;
196 
197         protected Builder(boolean withDefaults) {
198             if (withDefaults) {
199             }
200         }
201 
202         protected Builder(PluginContainer base, boolean forceCopy) {
203             if (forceCopy) {
204                 this.plugins = base.plugins;
205                 this.locations = base.locations;
206                 this.importedFrom = base.importedFrom;
207             } else {
208                 this.base = base;
209             }
210         }
211 
212         @Nonnull
213         public Builder plugins(Collection<Plugin> plugins) {
214             this.plugins = plugins;
215             return this;
216         }
217 
218 
219         @Nonnull
220         public Builder location(Object key, InputLocation location) {
221             if (location != null) {
222                 if (!(this.locations instanceof HashMap)) {
223                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
224                 }
225                 this.locations.put(key, location);
226             }
227             return this;
228         }
229 
230         @Nonnull
231         public Builder importedFrom(InputLocation importedFrom) {
232             this.importedFrom = importedFrom;
233             return this;
234         }
235 
236         @Nonnull
237         public PluginContainer build() {
238             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
239             if (base != null
240                     && (plugins == null || plugins == base.plugins)
241             ) {
242                 return base;
243             }
244             return new PluginContainer(this);
245         }
246 
247         Map<Object, InputLocation> computeLocations() {
248             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
249             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
250             if (newlocs.isEmpty()) {
251                 return Map.copyOf(oldlocs);
252             }
253             if (oldlocs.isEmpty()) {
254                 return Map.copyOf(newlocs);
255             }
256             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
257                     // Keep value from newlocs in case of duplicates
258                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
259         }
260     }
261 
262 
263             
264     volatile Map<String, Plugin> pluginMap;
265 
266     /**
267      * @return a Map of plugins field with {@code Plugins#getKey()} as key
268      * @see Plugin#getKey()
269      */
270     public Map<String, Plugin> getPluginsAsMap() {
271         if (pluginMap == null) {
272             synchronized (this) {
273                 if (pluginMap == null) {
274                     pluginMap = ImmutableCollections.copy(plugins.stream().collect(
275                             java.util.stream.Collectors.toMap(
276                                 Plugin::getKey, java.util.function.Function.identity())));
277                 }
278             }
279         }
280         return pluginMap;
281     }
282             
283           
284 
285             
286      @Override
287      public String toString()
288      {
289          return "PluginContainer {}";
290      }
291             
292           
293 }