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