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