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   * Section for management of default dependency information for use in a group of
24   * POMs.
25   */
26  @Experimental
27  @Generated @ThreadSafe @Immutable
28  public class DependencyManagement
29      implements Serializable, InputLocationTracker
30  {
31      /**
32       * The dependencies specified here are not used until they are referenced in a
33       * POM within the group. This allows the specification of a "standard" version for a
34       * particular dependency.
35       */
36      final List<Dependency> dependencies;
37      /** Locations (this potentially hides the same name field from the super class) */
38      final Map<Object, InputLocation> locations;
39      /** Location tracking */
40      final InputLocation importedFrom;
41  
42      /**
43        * Constructor for this class, to be called from its subclasses and {@link Builder}.
44        * @see Builder#build()
45        */
46      protected DependencyManagement(Builder builder) {
47          this.dependencies = ImmutableCollections.copy(builder.dependencies != null ? builder.dependencies : (builder.base != null ? builder.base.dependencies : null));
48          Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
49          Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
50          Map<Object, InputLocation> mutableLocations = new HashMap<>();
51          this.importedFrom = builder.importedFrom;
52          mutableLocations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
53          mutableLocations.put("dependencies", newlocs.containsKey("dependencies") ? newlocs.get("dependencies") : oldlocs.get("dependencies"));
54          this.locations = Collections.unmodifiableMap(mutableLocations);
55      }
56  
57      /**
58       * The dependencies specified here are not used until they are referenced in a
59       * POM within the group. This allows the specification of a "standard" version for a
60       * particular dependency.
61       *
62       * @return a {@code List<Dependency>}
63       */
64      @Nonnull
65      public List<Dependency> getDependencies() {
66          return this.dependencies;
67      }
68  
69      /**
70       * Gets the location of the specified field in the input source.
71       */
72      public InputLocation getLocation(Object key) {
73          return locations != null ? locations.get(key) : null;
74      }
75  
76      /**
77      * Gets the keys of the locations of the input source.
78      */
79      public Set<Object> getLocationKeys() {
80          return locations != null ? locations.keySet() : null;
81      }
82  
83      /**
84       * Gets the input location that caused this model to be read.
85       */
86      public InputLocation getImportedFrom()
87      {
88          return importedFrom;
89      }
90  
91      /**
92       * Creates a new builder with this object as the basis.
93       *
94       * @return a {@code Builder}
95       */
96      @Nonnull
97      public Builder with() {
98          return newBuilder(this);
99      }
100     /**
101      * Creates a new {@code DependencyManagement} instance using the specified dependencies.
102      *
103      * @param dependencies the new {@code Collection<Dependency>} to use
104      * @return a {@code DependencyManagement} with the specified dependencies
105      */
106     @Nonnull
107     public DependencyManagement withDependencies(Collection<Dependency> dependencies) {
108         return newBuilder(this, true).dependencies(dependencies).build();
109     }
110 
111     /**
112      * Creates a new {@code DependencyManagement} instance.
113      * Equivalent to {@code newInstance(true)}.
114      * @see #newInstance(boolean)
115      *
116      * @return a new {@code DependencyManagement}
117      */
118     @Nonnull
119     public static DependencyManagement newInstance() {
120         return newInstance(true);
121     }
122 
123     /**
124      * Creates a new {@code DependencyManagement} instance using default values or not.
125      * Equivalent to {@code newBuilder(withDefaults).build()}.
126      *
127      * @param withDefaults the boolean indicating whether default values should be used
128      * @return a new {@code DependencyManagement}
129      */
130     @Nonnull
131     public static DependencyManagement newInstance(boolean withDefaults) {
132         return newBuilder(withDefaults).build();
133     }
134 
135     /**
136      * Creates a new {@code DependencyManagement} builder instance.
137      * Equivalent to {@code newBuilder(true)}.
138      * @see #newBuilder(boolean)
139      *
140      * @return a new {@code Builder}
141      */
142     @Nonnull
143     public static Builder newBuilder() {
144         return newBuilder(true);
145     }
146 
147     /**
148      * Creates a new {@code DependencyManagement} builder instance using default values or not.
149      *
150      * @param withDefaults the boolean indicating whether default values should be used
151      * @return a new {@code Builder}
152      */
153     @Nonnull
154     public static Builder newBuilder(boolean withDefaults) {
155         return new Builder(withDefaults);
156     }
157 
158     /**
159      * Creates a new {@code DependencyManagement} builder instance using the specified object as a basis.
160      * Equivalent to {@code newBuilder(from, false)}.
161      *
162      * @param from the {@code DependencyManagement} instance to use as a basis
163      * @return a new {@code Builder}
164      */
165     @Nonnull
166     public static Builder newBuilder(DependencyManagement from) {
167         return newBuilder(from, false);
168     }
169 
170     /**
171      * Creates a new {@code DependencyManagement} builder instance using the specified object as a basis.
172      *
173      * @param from the {@code DependencyManagement} instance to use as a basis
174      * @param forceCopy the boolean indicating if a copy should be forced
175      * @return a new {@code Builder}
176      */
177     @Nonnull
178     public static Builder newBuilder(DependencyManagement from, boolean forceCopy) {
179         return new Builder(from, forceCopy);
180     }
181 
182     /**
183      * Builder class used to create DependencyManagement instances.
184      * @see #with()
185      * @see #newBuilder()
186      */
187     @NotThreadSafe
188     public static class Builder
189     {
190         DependencyManagement base;
191         Collection<Dependency> dependencies;
192         Map<Object, InputLocation> locations;
193         InputLocation importedFrom;
194 
195         protected Builder(boolean withDefaults) {
196             if (withDefaults) {
197             }
198         }
199 
200         protected Builder(DependencyManagement base, boolean forceCopy) {
201             if (forceCopy) {
202                 this.dependencies = base.dependencies;
203                 this.locations = base.locations;
204                 this.importedFrom = base.importedFrom;
205             } else {
206                 this.base = base;
207             }
208         }
209 
210         @Nonnull
211         public Builder dependencies(Collection<Dependency> dependencies) {
212             this.dependencies = dependencies;
213             return this;
214         }
215 
216 
217         @Nonnull
218         public Builder location(Object key, InputLocation location) {
219             if (location != null) {
220                 if (!(this.locations instanceof HashMap)) {
221                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
222                 }
223                 this.locations.put(key, location);
224             }
225             return this;
226         }
227 
228         @Nonnull
229         public Builder importedFrom(InputLocation importedFrom) {
230             this.importedFrom = importedFrom;
231             return this;
232         }
233 
234         @Nonnull
235         public DependencyManagement build() {
236             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
237             if (base != null
238                     && (dependencies == null || dependencies == base.dependencies)
239             ) {
240                 return base;
241             }
242             return new DependencyManagement(this);
243         }
244     }
245 
246 }