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      public InputLocation getLocation(Object key) {
67          return locations.get(key);
68      }
69  
70      /**
71       * Gets the keys of the locations of the input source.
72       */
73      public Set<Object> getLocationKeys() {
74          return locations.keySet();
75      }
76  
77      protected Stream<Object> getLocationKeyStream() {
78          return locations.keySet().stream();
79      }
80  
81      /**
82       * Gets the input location that caused this model to be read.
83       */
84      public InputLocation getImportedFrom() {
85          return importedFrom;
86      }
87  
88      /**
89       * Creates a new builder with this object as the basis.
90       *
91       * @return a {@code Builder}
92       */
93      @Nonnull
94      public Builder with() {
95          return newBuilder(this);
96      }
97      /**
98       * Creates a new {@code PluginContainer} instance using the specified plugins.
99       *
100      * @param plugins the new {@code Collection<Plugin>} to use
101      * @return a {@code PluginContainer} with the specified plugins
102      */
103     @Nonnull
104     public PluginContainer withPlugins(Collection<Plugin> plugins) {
105         return newBuilder(this, true).plugins(plugins).build();
106     }
107 
108     /**
109      * Creates a new {@code PluginContainer} instance.
110      * Equivalent to {@code newInstance(true)}.
111      * @see #newInstance(boolean)
112      *
113      * @return a new {@code PluginContainer}
114      */
115     @Nonnull
116     public static PluginContainer newInstance() {
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         return newBuilder(withDefaults).build();
130     }
131 
132     /**
133      * Creates a new {@code PluginContainer} builder instance.
134      * Equivalent to {@code newBuilder(true)}.
135      * @see #newBuilder(boolean)
136      *
137      * @return a new {@code Builder}
138      */
139     @Nonnull
140     public static Builder newBuilder() {
141         return newBuilder(true);
142     }
143 
144     /**
145      * Creates a new {@code PluginContainer} builder instance using default values or not.
146      *
147      * @param withDefaults the boolean indicating whether default values should be used
148      * @return a new {@code Builder}
149      */
150     @Nonnull
151     public static Builder newBuilder(boolean withDefaults) {
152         return new Builder(withDefaults);
153     }
154 
155     /**
156      * Creates a new {@code PluginContainer} builder instance using the specified object as a basis.
157      * Equivalent to {@code newBuilder(from, false)}.
158      *
159      * @param from the {@code PluginContainer} instance to use as a basis
160      * @return a new {@code Builder}
161      */
162     @Nonnull
163     public static Builder newBuilder(PluginContainer from) {
164         return newBuilder(from, false);
165     }
166 
167     /**
168      * Creates a new {@code PluginContainer} builder instance using the specified object as a basis.
169      *
170      * @param from the {@code PluginContainer} instance to use as a basis
171      * @param forceCopy the boolean indicating if a copy should be forced
172      * @return a new {@code Builder}
173      */
174     @Nonnull
175     public static Builder newBuilder(PluginContainer from, boolean forceCopy) {
176         return new Builder(from, forceCopy);
177     }
178 
179     /**
180      * Builder class used to create PluginContainer instances.
181      * @see #with()
182      * @see #newBuilder()
183      */
184     @NotThreadSafe
185     public static class Builder
186     {
187         PluginContainer base;
188         Collection<Plugin> plugins;
189         Map<Object, InputLocation> locations;
190         InputLocation importedFrom;
191 
192         protected Builder(boolean withDefaults) {
193             if (withDefaults) {
194             }
195         }
196 
197         protected Builder(PluginContainer base, boolean forceCopy) {
198             if (forceCopy) {
199                 this.plugins = base.plugins;
200                 this.locations = base.locations;
201                 this.importedFrom = base.importedFrom;
202             } else {
203                 this.base = base;
204             }
205         }
206 
207         @Nonnull
208         public Builder plugins(Collection<Plugin> plugins) {
209             this.plugins = plugins;
210             return this;
211         }
212 
213 
214         @Nonnull
215         public Builder location(Object key, InputLocation location) {
216             if (location != null) {
217                 if (!(this.locations instanceof HashMap)) {
218                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
219                 }
220                 this.locations.put(key, location);
221             }
222             return this;
223         }
224 
225         @Nonnull
226         public Builder importedFrom(InputLocation importedFrom) {
227             this.importedFrom = importedFrom;
228             return this;
229         }
230 
231         @Nonnull
232         public PluginContainer build() {
233             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
234             if (base != null
235                     && (plugins == null || plugins == base.plugins)
236             ) {
237                 return base;
238             }
239             return new PluginContainer(this);
240         }
241 
242         Map<Object, InputLocation> computeLocations() {
243             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
244             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
245             if (newlocs.isEmpty()) {
246                 return Map.copyOf(oldlocs);
247             }
248             if (oldlocs.isEmpty()) {
249                 return Map.copyOf(newlocs);
250             }
251             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
252                     // Keep value from newlocs in case of duplicates
253                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
254         }
255     }
256 
257 
258             
259     volatile Map<String, Plugin> pluginMap;
260 
261     /**
262      * @return a Map of plugins field with {@code Plugins#getKey()} as key
263      * @see Plugin#getKey()
264      */
265     public Map<String, Plugin> getPluginsAsMap() {
266         if (pluginMap == null) {
267             synchronized (this) {
268                 if (pluginMap == null) {
269                     pluginMap = ImmutableCollections.copy(plugins.stream().collect(
270                             java.util.stream.Collectors.toMap(
271                                 Plugin::getKey, java.util.function.Function.identity())));
272                 }
273             }
274         }
275         return pluginMap;
276     }
277             
278           
279 
280             
281      @Override
282      public String toString()
283      {
284          return "PluginContainer {}";
285      }
286             
287           
288 }