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.Collections;
9   import java.util.HashMap;
10  import java.util.Map;
11  import java.util.Objects;
12  import java.util.Optional;
13  import java.util.Set;
14  import java.util.stream.Collectors;
15  import java.util.stream.Stream;
16  import org.apache.maven.api.annotations.Experimental;
17  import org.apache.maven.api.annotations.Generated;
18  import org.apache.maven.api.annotations.Immutable;
19  import org.apache.maven.api.annotations.Nonnull;
20  import org.apache.maven.api.annotations.NotThreadSafe;
21  import org.apache.maven.api.annotations.ThreadSafe;
22  
23  /**
24   * Describes the prerequisites a project can have.
25   */
26  @Experimental
27  @Generated @ThreadSafe @Immutable
28  public class Prerequisites
29      implements Serializable, InputLocationTracker
30  {
31      /**
32       * For a plugin project (packaging is {@code maven-plugin}), the minimum version of
33       * Maven required to use the resulting plugin.<br>
34       * In Maven 2, this was also specifying the minimum version of Maven required to build a
35       * project, but this usage is <b>deprecated</b> in Maven 3 and not checked any more: use
36       * the <a href="https://maven.apache.org/enforcer/enforcer-rules/requireMavenVersion.html">Maven Enforcer Plugin's
37       * {@code requireMavenVersion} rule</a> instead.
38       */
39      final String maven;
40      /** Locations */
41      final Map<Object, InputLocation> locations;
42      /** Location tracking */
43      final InputLocation importedFrom;
44  
45      /**
46        * Constructor for this class, to be called from its subclasses and {@link Builder}.
47        * @see Builder#build()
48        */
49      protected Prerequisites(Builder builder) {
50          this.maven = builder.maven != null ? builder.maven : (builder.base != null ? builder.base.maven : null);
51          this.locations = builder.computeLocations();
52          this.importedFrom = builder.importedFrom;
53      }
54  
55      /**
56       * For a plugin project (packaging is {@code maven-plugin}), the minimum version of
57       * Maven required to use the resulting plugin.<br>
58       * In Maven 2, this was also specifying the minimum version of Maven required to build a
59       * project, but this usage is <b>deprecated</b> in Maven 3 and not checked any more: use
60       * the <a href="https://maven.apache.org/enforcer/enforcer-rules/requireMavenVersion.html">Maven Enforcer Plugin's
61       * {@code requireMavenVersion} rule</a> instead.
62       *
63       * @return a {@code String}
64       */
65      public String getMaven() {
66          return this.maven;
67      }
68  
69      /**
70       * Gets the location of the specified field in the input source.
71       */
72      public InputLocation getLocation(Object key) {
73          return locations.get(key);
74      }
75  
76      /**
77       * Gets the keys of the locations of the input source.
78       */
79      public Set<Object> getLocationKeys() {
80          return locations.keySet();
81      }
82  
83      protected Stream<Object> getLocationKeyStream() {
84          return locations.keySet().stream();
85      }
86  
87      /**
88       * Gets the input location that caused this model to be read.
89       */
90      public InputLocation getImportedFrom()
91      {
92          return importedFrom;
93      }
94  
95      /**
96       * Creates a new builder with this object as the basis.
97       *
98       * @return a {@code Builder}
99       */
100     @Nonnull
101     public Builder with() {
102         return newBuilder(this);
103     }
104     /**
105      * Creates a new {@code Prerequisites} instance using the specified maven.
106      *
107      * @param maven the new {@code String} to use
108      * @return a {@code Prerequisites} with the specified maven
109      */
110     @Nonnull
111     public Prerequisites withMaven(String maven) {
112         return newBuilder(this, true).maven(maven).build();
113     }
114 
115     /**
116      * Creates a new {@code Prerequisites} instance.
117      * Equivalent to {@code newInstance(true)}.
118      * @see #newInstance(boolean)
119      *
120      * @return a new {@code Prerequisites}
121      */
122     @Nonnull
123     public static Prerequisites newInstance() {
124         return newInstance(true);
125     }
126 
127     /**
128      * Creates a new {@code Prerequisites} instance using default values or not.
129      * Equivalent to {@code newBuilder(withDefaults).build()}.
130      *
131      * @param withDefaults the boolean indicating whether default values should be used
132      * @return a new {@code Prerequisites}
133      */
134     @Nonnull
135     public static Prerequisites newInstance(boolean withDefaults) {
136         return newBuilder(withDefaults).build();
137     }
138 
139     /**
140      * Creates a new {@code Prerequisites} 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         return newBuilder(true);
149     }
150 
151     /**
152      * Creates a new {@code Prerequisites} 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         return new Builder(withDefaults);
160     }
161 
162     /**
163      * Creates a new {@code Prerequisites} builder instance using the specified object as a basis.
164      * Equivalent to {@code newBuilder(from, false)}.
165      *
166      * @param from the {@code Prerequisites} instance to use as a basis
167      * @return a new {@code Builder}
168      */
169     @Nonnull
170     public static Builder newBuilder(Prerequisites from) {
171         return newBuilder(from, false);
172     }
173 
174     /**
175      * Creates a new {@code Prerequisites} builder instance using the specified object as a basis.
176      *
177      * @param from the {@code Prerequisites} instance to use as a basis
178      * @param forceCopy the boolean indicating if a copy should be forced
179      * @return a new {@code Builder}
180      */
181     @Nonnull
182     public static Builder newBuilder(Prerequisites from, boolean forceCopy) {
183         return new Builder(from, forceCopy);
184     }
185 
186     /**
187      * Builder class used to create Prerequisites instances.
188      * @see #with()
189      * @see #newBuilder()
190      */
191     @NotThreadSafe
192     public static class Builder
193     {
194         Prerequisites base;
195         String maven;
196         Map<Object, InputLocation> locations;
197         InputLocation importedFrom;
198 
199         protected Builder(boolean withDefaults) {
200             if (withDefaults) {
201                 this.maven = "2.0";
202             }
203         }
204 
205         protected Builder(Prerequisites base, boolean forceCopy) {
206             if (forceCopy) {
207                 this.maven = base.maven;
208                 this.locations = base.locations;
209                 this.importedFrom = base.importedFrom;
210             } else {
211                 this.base = base;
212             }
213         }
214 
215         @Nonnull
216         public Builder maven(String maven) {
217             this.maven = maven;
218             return this;
219         }
220 
221 
222         @Nonnull
223         public Builder location(Object key, InputLocation location) {
224             if (location != null) {
225                 if (!(this.locations instanceof HashMap)) {
226                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
227                 }
228                 this.locations.put(key, location);
229             }
230             return this;
231         }
232 
233         @Nonnull
234         public Builder importedFrom(InputLocation importedFrom) {
235             this.importedFrom = importedFrom;
236             return this;
237         }
238 
239         @Nonnull
240         public Prerequisites build() {
241             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
242             if (base != null
243                     && (maven == null || maven == base.maven)
244             ) {
245                 return base;
246             }
247             return new Prerequisites(this);
248         }
249 
250         Map<Object, InputLocation> computeLocations() {
251             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
252             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
253             if (newlocs.isEmpty()) {
254                 return Map.copyOf(oldlocs);
255             }
256             if (oldlocs.isEmpty()) {
257                 return Map.copyOf(newlocs);
258             }
259             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
260                     // Keep value from newlocs in case of duplicates
261                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
262         }
263     }
264 
265 }