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      /** Location of the xml element for this object. */
37      final InputLocation location;
38      /** Location of the xml element for the field dependencies. */
39      final InputLocation dependenciesLocation;
40      /** Other locations */
41      final Map<Object, InputLocation> locations;
42  
43      /**
44        * Constructor for this class, package protected.
45        * @see Builder#build()
46        */
47      DependencyManagement(
48          Collection<Dependency> dependencies,
49          Map<Object, InputLocation> locations,
50          InputLocation location,
51          InputLocation dependenciesLocation
52      )
53      {
54          this.dependencies = ImmutableCollections.copy( dependencies );
55          this.locations = ImmutableCollections.copy( locations );
56          this.location = location;
57          this.dependenciesLocation = dependenciesLocation;
58      }
59  
60      /**
61       * The dependencies specified here are not used until they are referenced in a
62       * POM within the group. This allows the specification of a "standard" version for a
63       * particular dependency.
64       *
65       * @return a {@code List<Dependency>}
66       */
67      @Nonnull
68      public List<Dependency> getDependencies()
69      {
70          return this.dependencies;
71      }
72  
73      /**
74       * Gets the location of the specified field in the input source.
75       */
76      public InputLocation getLocation( Object key )
77      {
78          if ( key instanceof String )
79          {
80              switch ( ( String ) key )
81              {
82                  case "":
83                      return location;
84                  case "dependencies":
85                      return dependenciesLocation;
86              }
87          }
88          return locations != null ? locations.get( key ) : null;
89      }
90  
91      /**
92       * Creates a new builder with this object as the basis.
93       *
94       * @return a {@code Builder}
95       */
96      @Nonnull
97      public Builder with()
98      {
99          return newBuilder( this );
100     }
101     /**
102      * Creates a new {@code DependencyManagement} instance using the specified dependencies.
103      *
104      * @param dependencies the new {@code Collection<Dependency>} to use
105      * @return a {@code DependencyManagement} with the specified dependencies
106      */
107     @Nonnull
108     public DependencyManagement withDependencies( Collection<Dependency> dependencies )
109     {
110         return with().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     {
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     {
136         return newBuilder( withDefaults ).build();
137     }
138 
139     /**
140      * Creates a new {@code DependencyManagement} builder instance.
141      * Equivalent to {@code newBuilder( true )}.
142      * @see #newBuilder(boolean)
143      *
144      * @return a new {@code Builder}
145      */
146     @Nonnull
147     public static Builder newBuilder()
148     {
149         return newBuilder( true );
150     }
151 
152     /**
153      * Creates a new {@code DependencyManagement} builder instance using default values or not.
154      *
155      * @param withDefaults the boolean indicating whether default values should be used
156      * @return a new {@code Builder}
157      */
158     @Nonnull
159     public static Builder newBuilder( boolean withDefaults )
160     {
161         return new Builder( withDefaults );
162     }
163 
164     /**
165      * Creates a new {@code DependencyManagement} builder instance using the specified object as a basis.
166      * Equivalent to {@code newBuilder( from, false )}.
167      *
168      * @param from the {@code DependencyManagement} instance to use as a basis
169      * @return a new {@code Builder}
170      */
171     @Nonnull
172     public static Builder newBuilder( DependencyManagement from )
173     {
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     {
187         return new Builder( from, forceCopy );
188     }
189 
190     /**
191      * Builder class used to create DependencyManagement instances.
192      * @see #with()
193      * @see #newBuilder()
194      */
195     @NotThreadSafe
196     public static class Builder
197     {
198         DependencyManagement base;
199         Collection<Dependency> dependencies;
200         Map<Object, InputLocation> locations;
201 
202         Builder( boolean withDefaults )
203         {
204             if ( withDefaults )
205             {
206             }
207         }
208 
209         Builder( DependencyManagement base, boolean forceCopy )
210         {
211             if ( forceCopy )
212             {
213                 this.dependencies = base.dependencies;
214             }
215             else
216             {
217                 this.base = base;
218             }
219         }
220 
221         @Nonnull
222         public Builder dependencies( Collection<Dependency> dependencies )
223         {
224             this.dependencies = dependencies;
225             return this;
226         }
227 
228 
229         @Nonnull
230         public Builder location( Object key, InputLocation location )
231         {
232             if ( location != null )
233             {
234                 if ( this.locations == null )
235                 {
236                     this.locations = new HashMap<>();
237                 }
238                 this.locations.put( key, location );
239             }
240             return this;
241         }
242 
243         @Nonnull
244         public DependencyManagement build()
245         {
246             if ( base != null
247                     && ( dependencies == null || dependencies == base.dependencies )
248             )
249             {
250                 return base;
251             }
252             Map<Object, InputLocation> locations = null;
253             InputLocation location = null;
254             InputLocation dependenciesLocation = null;
255             if ( this.locations != null )
256             {
257                 locations = this.locations;
258                 location = locations.remove( "" );
259                 dependenciesLocation = locations.remove( "dependencies" );
260             }
261             return new DependencyManagement(
262                 dependencies != null ? dependencies : ( base != null ? base.dependencies : null ),
263                 locations != null ? locations : ( base != null ? base.locations : null ),
264                 location != null ? location : ( base != null ? base.location : null ),
265                 dependenciesLocation != null ? dependenciesLocation : ( base != null ? base.dependenciesLocation : null )
266             );
267         }
268     }
269 
270 }