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.
34       */
35      final String maven;
36      /** Locations */
37      final Map<Object, InputLocation> locations;
38      /** Location tracking */
39      final InputLocation importedFrom;
40  
41      /**
42        * Constructor for this class, to be called from its subclasses and {@link Builder}.
43        * @see Builder#build()
44        */
45      protected Prerequisites(Builder builder) {
46          this.maven = builder.maven != null ? builder.maven : (builder.base != null ? builder.base.maven : null);
47          this.locations = builder.computeLocations();
48          this.importedFrom = builder.importedFrom;
49      }
50  
51      /**
52       * For a plugin project (packaging is {@code maven-plugin}), the minimum version of
53       * Maven required to use the resulting plugin.
54       *
55       * @return a {@code String}
56       */
57      public String getMaven() {
58          return this.maven;
59      }
60  
61      /**
62       * Gets the location of the specified field in the input source.
63       *
64       * @param key the key of the field, must not be {@code null}
65       * @return the location of the field in the input source or {@code null} if unknown
66       * @throws NullPointerException if {@code key} is {@code null}
67       */
68      public InputLocation getLocation(Object key) {
69          Objects.requireNonNull(key, "key");
70          return locations.get(key);
71      }
72  
73      /**
74       * Gets the keys of the locations of the input source.
75       */
76      public Set<Object> getLocationKeys() {
77          return locations.keySet();
78      }
79  
80      protected Stream<Object> getLocationKeyStream() {
81          return locations.keySet().stream();
82      }
83  
84      /**
85       * Gets the input location that caused this model to be read.
86       */
87      public InputLocation getImportedFrom() {
88          return importedFrom;
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          return newBuilder(this);
99      }
100     /**
101      * Creates a new {@code Prerequisites} instance using the specified maven.
102      *
103      * @param maven the new {@code String} to use
104      * @return a {@code Prerequisites} with the specified maven
105      */
106     @Nonnull
107     public Prerequisites withMaven(String maven) {
108         return newBuilder(this, true).maven(maven).build();
109     }
110 
111     /**
112      * Creates a new {@code Prerequisites} instance.
113      * Equivalent to {@code newInstance(true)}.
114      * @see #newInstance(boolean)
115      *
116      * @return a new {@code Prerequisites}
117      */
118     @Nonnull
119     public static Prerequisites newInstance() {
120         return newInstance(true);
121     }
122 
123     /**
124      * Creates a new {@code Prerequisites} instance using default values or not.
125      * Equivalent to {@code newBuilder(withDefaults).build()}.
126      *
127      * @param withDefaults the boolean indicating whether default values should be used
128      * @return a new {@code Prerequisites}
129      */
130     @Nonnull
131     public static Prerequisites newInstance(boolean withDefaults) {
132         return newBuilder(withDefaults).build();
133     }
134 
135     /**
136      * Creates a new {@code Prerequisites} builder instance.
137      * Equivalent to {@code newBuilder(true)}.
138      * @see #newBuilder(boolean)
139      *
140      * @return a new {@code Builder}
141      */
142     @Nonnull
143     public static Builder newBuilder() {
144         return newBuilder(true);
145     }
146 
147     /**
148      * Creates a new {@code Prerequisites} builder instance using default values or not.
149      *
150      * @param withDefaults the boolean indicating whether default values should be used
151      * @return a new {@code Builder}
152      */
153     @Nonnull
154     public static Builder newBuilder(boolean withDefaults) {
155         return new Builder(withDefaults);
156     }
157 
158     /**
159      * Creates a new {@code Prerequisites} builder instance using the specified object as a basis.
160      * Equivalent to {@code newBuilder(from, false)}.
161      *
162      * @param from the {@code Prerequisites} instance to use as a basis
163      * @return a new {@code Builder}
164      */
165     @Nonnull
166     public static Builder newBuilder(Prerequisites from) {
167         return newBuilder(from, false);
168     }
169 
170     /**
171      * Creates a new {@code Prerequisites} builder instance using the specified object as a basis.
172      *
173      * @param from the {@code Prerequisites} instance to use as a basis
174      * @param forceCopy the boolean indicating if a copy should be forced
175      * @return a new {@code Builder}
176      */
177     @Nonnull
178     public static Builder newBuilder(Prerequisites from, boolean forceCopy) {
179         return new Builder(from, forceCopy);
180     }
181 
182     /**
183      * Builder class used to create Prerequisites instances.
184      * @see #with()
185      * @see #newBuilder()
186      */
187     @NotThreadSafe
188     public static class Builder
189     {
190         Prerequisites base;
191         String maven;
192         Map<Object, InputLocation> locations;
193         InputLocation importedFrom;
194 
195         protected Builder(boolean withDefaults) {
196             if (withDefaults) {
197                 this.maven = "2.0";
198             }
199         }
200 
201         protected Builder(Prerequisites base, boolean forceCopy) {
202             if (forceCopy) {
203                 this.maven = base.maven;
204                 this.locations = base.locations;
205                 this.importedFrom = base.importedFrom;
206             } else {
207                 this.base = base;
208             }
209         }
210 
211         @Nonnull
212         public Builder maven(String maven) {
213             this.maven = maven;
214             return this;
215         }
216 
217 
218         @Nonnull
219         public Builder location(Object key, InputLocation location) {
220             if (location != null) {
221                 if (!(this.locations instanceof HashMap)) {
222                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
223                 }
224                 this.locations.put(key, location);
225             }
226             return this;
227         }
228 
229         @Nonnull
230         public Builder importedFrom(InputLocation importedFrom) {
231             this.importedFrom = importedFrom;
232             return this;
233         }
234 
235         @Nonnull
236         public Prerequisites build() {
237             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
238             if (base != null
239                     && (maven == null || maven == base.maven)
240             ) {
241                 return base;
242             }
243             return new Prerequisites(this);
244         }
245 
246         Map<Object, InputLocation> computeLocations() {
247             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
248             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
249             if (newlocs.isEmpty()) {
250                 return Map.copyOf(oldlocs);
251             }
252             if (oldlocs.isEmpty()) {
253                 return Map.copyOf(newlocs);
254             }
255             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
256                     // Keep value from newlocs in case of duplicates
257                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
258         }
259     }
260 
261 }