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 org.apache.maven.api.annotations.Experimental;
15  import org.apache.maven.api.annotations.Generated;
16  import org.apache.maven.api.annotations.Immutable;
17  import org.apache.maven.api.annotations.Nonnull;
18  import org.apache.maven.api.annotations.NotThreadSafe;
19  import org.apache.maven.api.annotations.ThreadSafe;
20  
21  /**
22   * Contains the plugins informations for the project.
23   */
24  @Experimental
25  @Generated @ThreadSafe @Immutable
26  public class PluginContainer
27      implements Serializable, InputLocationTracker
28  {
29      /**
30       * The list of plugins to use.
31       */
32      final List<Plugin> plugins;
33      /** Locations */
34      final Map<Object, InputLocation> locations;
35  
36      /**
37        * Constructor for this class, package protected.
38        * @see Builder#build()
39        */
40      PluginContainer(
41          Collection<Plugin> plugins,
42          Map<Object, InputLocation> locations
43      ) {
44          this.plugins = ImmutableCollections.copy(plugins);
45          this.locations = ImmutableCollections.copy(locations);
46      }
47  
48      /**
49       * The list of plugins to use.
50       *
51       * @return a {@code List<Plugin>}
52       */
53      @Nonnull
54      public List<Plugin> getPlugins() {
55          return this.plugins;
56      }
57  
58      /**
59       * Gets the location of the specified field in the input source.
60       */
61      public InputLocation getLocation(Object key) {
62          return locations != null ? locations.get(key) : null;
63      }
64  
65      /**
66       * Creates a new builder with this object as the basis.
67       *
68       * @return a {@code Builder}
69       */
70      @Nonnull
71      public Builder with() {
72          return newBuilder(this);
73      }
74      /**
75       * Creates a new {@code PluginContainer} instance using the specified plugins.
76       *
77       * @param plugins the new {@code Collection<Plugin>} to use
78       * @return a {@code PluginContainer} with the specified plugins
79       */
80      @Nonnull
81      public PluginContainer withPlugins(Collection<Plugin> plugins) {
82          return newBuilder(this, true).plugins(plugins).build();
83      }
84  
85      /**
86       * Creates a new {@code PluginContainer} instance.
87       * Equivalent to {@code newInstance(true)}.
88       * @see #newInstance(boolean)
89       *
90       * @return a new {@code PluginContainer}
91       */
92      @Nonnull
93      public static PluginContainer newInstance() {
94          return newInstance(true);
95      }
96  
97      /**
98       * Creates a new {@code PluginContainer} instance using default values or not.
99       * Equivalent to {@code newBuilder(withDefaults).build()}.
100      *
101      * @param withDefaults the boolean indicating whether default values should be used
102      * @return a new {@code PluginContainer}
103      */
104     @Nonnull
105     public static PluginContainer newInstance(boolean withDefaults) {
106         return newBuilder(withDefaults).build();
107     }
108 
109     /**
110      * Creates a new {@code PluginContainer} builder instance.
111      * Equivalent to {@code newBuilder(true)}.
112      * @see #newBuilder(boolean)
113      *
114      * @return a new {@code Builder}
115      */
116     @Nonnull
117     public static Builder newBuilder() {
118         return newBuilder(true);
119     }
120 
121     /**
122      * Creates a new {@code PluginContainer} builder instance using default values or not.
123      *
124      * @param withDefaults the boolean indicating whether default values should be used
125      * @return a new {@code Builder}
126      */
127     @Nonnull
128     public static Builder newBuilder(boolean withDefaults) {
129         return new Builder(withDefaults);
130     }
131 
132     /**
133      * Creates a new {@code PluginContainer} builder instance using the specified object as a basis.
134      * Equivalent to {@code newBuilder(from, false)}.
135      *
136      * @param from the {@code PluginContainer} instance to use as a basis
137      * @return a new {@code Builder}
138      */
139     @Nonnull
140     public static Builder newBuilder(PluginContainer from) {
141         return newBuilder(from, false);
142     }
143 
144     /**
145      * Creates a new {@code PluginContainer} builder instance using the specified object as a basis.
146      *
147      * @param from the {@code PluginContainer} instance to use as a basis
148      * @param forceCopy the boolean indicating if a copy should be forced
149      * @return a new {@code Builder}
150      */
151     @Nonnull
152     public static Builder newBuilder(PluginContainer from, boolean forceCopy) {
153         return new Builder(from, forceCopy);
154     }
155 
156     /**
157      * Builder class used to create PluginContainer instances.
158      * @see #with()
159      * @see #newBuilder()
160      */
161     @NotThreadSafe
162     public static class Builder
163     {
164         PluginContainer base;
165         Collection<Plugin> plugins;
166         Map<Object, InputLocation> locations;
167 
168         Builder(boolean withDefaults) {
169             if (withDefaults) {
170             }
171         }
172 
173         Builder(PluginContainer base, boolean forceCopy) {
174             if (forceCopy) {
175                 this.plugins = base.plugins;
176                 this.locations = base.locations;
177             } else {
178                 this.base = base;
179             }
180         }
181 
182         @Nonnull
183         public Builder plugins(Collection<Plugin> plugins) {
184             this.plugins = plugins;
185             return this;
186         }
187 
188 
189         @Nonnull
190         public Builder location(Object key, InputLocation location) {
191             if (location != null) {
192                 if (!(this.locations instanceof HashMap)) {
193                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
194                 }
195                 this.locations.put(key, location);
196             }
197             return this;
198         }
199 
200         @Nonnull
201         public PluginContainer build() {
202             if (base != null
203                     && (plugins == null || plugins == base.plugins)
204             ) {
205                 return base;
206             }
207             Map<Object, InputLocation> newlocs = this.locations != null ? this.locations : Collections.emptyMap();
208             Map<Object, InputLocation> oldlocs = this.base != null && this.base.locations != null ? this.base.locations : Collections.emptyMap();
209             Map<Object, InputLocation> locations = new HashMap<>();
210             locations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
211             locations.put("plugins", newlocs.containsKey("plugins") ? newlocs.get("plugins") : oldlocs.get("plugins"));
212             return new PluginContainer(
213                 plugins != null ? plugins : (base != null ? base.plugins : null),
214                 locations
215             );
216         }
217     }
218 
219 
220             
221     volatile Map<String, Plugin> pluginMap;
222 
223     /**
224      * @return a Map of plugins field with {@code Plugins#getKey()} as key
225      * @see Plugin#getKey()
226      */
227     public Map<String, Plugin> getPluginsAsMap() {
228         if (pluginMap == null) {
229             synchronized (this) {
230                 if (pluginMap == null) {
231                     pluginMap = ImmutableCollections.copy(plugins.stream().collect(
232                             java.util.stream.Collectors.toMap(
233                                 Plugin::getKey, java.util.function.Function.identity())));
234                 }
235             }
236         }
237         return pluginMap;
238     }
239             
240           
241 
242             
243      @Override
244      public String toString()
245      {
246          return "PluginContainer {}";
247      }
248             
249           
250 }