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