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