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