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