View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //   Generated by Maven, any modifications will be overwritten.
3   // ==============================================================
4   package org.apache.maven.api.model;
5   
6   import java.io.Serializable;
7   import java.util.ArrayList;
8   import java.util.Collection;
9   import java.util.Collections;
10  import java.util.HashMap;
11  import java.util.List;
12  import java.util.Map;
13  import org.apache.maven.api.annotations.Experimental;
14  import org.apache.maven.api.annotations.Generated;
15  import org.apache.maven.api.annotations.Immutable;
16  import org.apache.maven.api.annotations.Nonnull;
17  import org.apache.maven.api.annotations.NotThreadSafe;
18  import org.apache.maven.api.annotations.ThreadSafe;
19  
20  /**
21   * Section for management of default dependency information for use in a group of
22   * POMs.
23   */
24  @Experimental
25  @Generated @ThreadSafe @Immutable
26  public class DependencyManagement
27      implements Serializable, InputLocationTracker
28  {
29      /**
30       * The dependencies specified here are not used until they are referenced in a
31       * POM within the group. This allows the specification of a "standard" version for a
32       * particular dependency.
33       */
34      final List<Dependency> dependencies;
35      /** Location of the xml element for this object. */
36      final InputLocation location;
37      /** Location of the xml element for the field dependencies. */
38      final InputLocation dependenciesLocation;
39      /** Other locations */
40      final Map<Object, InputLocation> locations;
41  
42      /**
43        * Constructor for this class, package protected.
44        * @see Builder#build()
45        */
46      DependencyManagement(
47          Collection<Dependency> dependencies,
48          Map<Object, InputLocation> locations,
49          InputLocation location,
50          InputLocation dependenciesLocation
51      )
52      {
53          this.dependencies = ImmutableCollections.copy( dependencies );
54          this.locations = ImmutableCollections.copy( locations );
55          this.location = location;
56          this.dependenciesLocation = dependenciesLocation;
57      }
58  
59      /**
60       * The dependencies specified here are not used until they are referenced in a
61       * POM within the group. This allows the specification of a "standard" version for a
62       * particular dependency.
63       *
64       * @return a {@code List<Dependency>}
65       */
66      @Nonnull
67      public List<Dependency> getDependencies()
68      {
69          return this.dependencies;
70      }
71  
72      /**
73       * Gets the location of the specified field in the input source.
74       */
75      public InputLocation getLocation( Object key )
76      {
77          if ( key instanceof String )
78          {
79              switch ( ( String ) key )
80              {
81                  case "":
82                      return location;
83                  case "dependencies":
84                      return dependenciesLocation;
85              }
86          }
87          return locations != null ? locations.get( key ) : null;
88      }
89  
90      /**
91       * Creates a new builder with this object as the basis.
92       *
93       * @return a {@code Builder}
94       */
95      @Nonnull
96      public Builder with()
97      {
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     {
109         return with().dependencies( dependencies ).build();
110     }
111 
112     /**
113      * Creates a new {@code DependencyManagement} instance.
114      * Equivalent to {@code newInstance( true )}.
115      * @see #newInstance(boolean)
116      *
117      * @return a new {@code DependencyManagement}
118      */
119     @Nonnull
120     public static DependencyManagement newInstance()
121     {
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     {
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     {
148         return newBuilder( true );
149     }
150 
151     /**
152      * Creates a new {@code DependencyManagement} builder instance using default values or not.
153      *
154      * @param withDefaults the boolean indicating whether default values should be used
155      * @return a new {@code Builder}
156      */
157     @Nonnull
158     public static Builder newBuilder( boolean withDefaults )
159     {
160         return new Builder( withDefaults );
161     }
162 
163     /**
164      * Creates a new {@code DependencyManagement} builder instance using the specified object as a basis.
165      * Equivalent to {@code newBuilder( from, false )}.
166      *
167      * @param from the {@code DependencyManagement} instance to use as a basis
168      * @return a new {@code Builder}
169      */
170     @Nonnull
171     public static Builder newBuilder( DependencyManagement from )
172     {
173         return newBuilder( from, false );
174     }
175 
176     /**
177      * Creates a new {@code DependencyManagement} builder instance using the specified object as a basis.
178      *
179      * @param from the {@code DependencyManagement} instance to use as a basis
180      * @param forceCopy the boolean indicating if a copy should be forced
181      * @return a new {@code Builder}
182      */
183     @Nonnull
184     public static Builder newBuilder( DependencyManagement from, boolean forceCopy )
185     {
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 
201         Builder( boolean withDefaults )
202         {
203             if ( withDefaults )
204             {
205             }
206         }
207 
208         Builder( DependencyManagement base, boolean forceCopy )
209         {
210             if ( forceCopy )
211             {
212                 this.dependencies = base.dependencies;
213             }
214             else
215             {
216                 this.base = base;
217             }
218         }
219 
220         @Nonnull
221         public Builder dependencies( Collection<Dependency> dependencies )
222         {
223             this.dependencies = dependencies;
224             return this;
225         }
226 
227 
228         @Nonnull
229         public Builder location( Object key, InputLocation location )
230         {
231             if ( location != null )
232             {
233                 if ( this.locations == null )
234                 {
235                     this.locations = new HashMap<>();
236                 }
237                 this.locations.put( key, location );
238             }
239             return this;
240         }
241 
242         @Nonnull
243         public DependencyManagement build()
244         {
245             if ( base != null
246                     && ( dependencies == null || dependencies == base.dependencies )
247             )
248             {
249                 return base;
250             }
251             Map<Object, InputLocation> locations = null;
252             InputLocation location = null;
253             InputLocation dependenciesLocation = null;
254             if ( this.locations != null )
255             {
256                 locations = this.locations;
257                 location = locations.remove( "" );
258                 dependenciesLocation = locations.remove( "dependencies" );
259             }
260             return new DependencyManagement(
261                 dependencies != null ? dependencies : ( base != null ? base.dependencies : null ),
262                 locations != null ? locations : ( base != null ? base.locations : null ),
263                 location != null ? location : ( base != null ? base.location : null ),
264                 dependenciesLocation != null ? dependenciesLocation : ( base != null ? base.dependenciesLocation : null )
265             );
266         }
267     }
268 
269 }