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.settings;
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   * This is the file specification used to activate a profile. The missing value will be the location
25   * of a file that needs to exist, and if it doesn't the profile must run.  On the other hand exists will test
26   * for the existence of the file and if it is there will run the profile.
27   */
28  @Experimental
29  @Generated @ThreadSafe @Immutable
30  public class ActivationFile
31      implements Serializable, InputLocationTracker
32  {
33      /**
34       * The name of the file that should be missing to activate a profile. Please note, that missing and exists
35       * fields cannot be used together. Only one of them should be used at any one time.
36       */
37      final String missing;
38      /**
39       * The name of the file that should exist to activate a profile. Please note, that missing and exists
40       * fields cannot be used together. Only one of them should be used at any one time.
41       */
42      final String exists;
43      /** Locations */
44      final Map<Object, InputLocation> locations;
45      /** Location tracking */
46      final InputLocation importedFrom;
47  
48      /**
49        * Constructor for this class, to be called from its subclasses and {@link Builder}.
50        * @see Builder#build()
51        */
52      protected ActivationFile(Builder builder) {
53          this.missing = builder.missing != null ? builder.missing : (builder.base != null ? builder.base.missing : null);
54          this.exists = builder.exists != null ? builder.exists : (builder.base != null ? builder.base.exists : null);
55          this.locations = builder.computeLocations();
56          this.importedFrom = builder.importedFrom;
57      }
58  
59      /**
60       * The name of the file that should be missing to activate a profile. Please note, that missing and exists
61       * fields cannot be used together. Only one of them should be used at any one time.
62       *
63       * @return a {@code String}
64       */
65      public String getMissing() {
66          return this.missing;
67      }
68  
69      /**
70       * The name of the file that should exist to activate a profile. Please note, that missing and exists
71       * fields cannot be used together. Only one of them should be used at any one time.
72       *
73       * @return a {@code String}
74       */
75      public String getExists() {
76          return this.exists;
77      }
78  
79      /**
80       * Gets the location of the specified field in the input source.
81       */
82      public InputLocation getLocation(Object key) {
83          return locations.get(key);
84      }
85  
86      /**
87       * Gets the keys of the locations of the input source.
88       */
89      public Set<Object> getLocationKeys() {
90          return locations.keySet();
91      }
92  
93      protected Stream<Object> getLocationKeyStream() {
94          return locations.keySet().stream();
95      }
96  
97      /**
98       * Gets the input location that caused this model to be read.
99       */
100     public InputLocation getImportedFrom()
101     {
102         return importedFrom;
103     }
104 
105     /**
106      * Creates a new builder with this object as the basis.
107      *
108      * @return a {@code Builder}
109      */
110     @Nonnull
111     public Builder with() {
112         return newBuilder(this);
113     }
114     /**
115      * Creates a new {@code ActivationFile} instance using the specified missing.
116      *
117      * @param missing the new {@code String} to use
118      * @return a {@code ActivationFile} with the specified missing
119      */
120     @Nonnull
121     public ActivationFile withMissing(String missing) {
122         return newBuilder(this, true).missing(missing).build();
123     }
124     /**
125      * Creates a new {@code ActivationFile} instance using the specified exists.
126      *
127      * @param exists the new {@code String} to use
128      * @return a {@code ActivationFile} with the specified exists
129      */
130     @Nonnull
131     public ActivationFile withExists(String exists) {
132         return newBuilder(this, true).exists(exists).build();
133     }
134 
135     /**
136      * Creates a new {@code ActivationFile} instance.
137      * Equivalent to {@code newInstance(true)}.
138      * @see #newInstance(boolean)
139      *
140      * @return a new {@code ActivationFile}
141      */
142     @Nonnull
143     public static ActivationFile newInstance() {
144         return newInstance(true);
145     }
146 
147     /**
148      * Creates a new {@code ActivationFile} instance using default values or not.
149      * Equivalent to {@code newBuilder(withDefaults).build()}.
150      *
151      * @param withDefaults the boolean indicating whether default values should be used
152      * @return a new {@code ActivationFile}
153      */
154     @Nonnull
155     public static ActivationFile newInstance(boolean withDefaults) {
156         return newBuilder(withDefaults).build();
157     }
158 
159     /**
160      * Creates a new {@code ActivationFile} builder instance.
161      * Equivalent to {@code newBuilder(true)}.
162      * @see #newBuilder(boolean)
163      *
164      * @return a new {@code Builder}
165      */
166     @Nonnull
167     public static Builder newBuilder() {
168         return newBuilder(true);
169     }
170 
171     /**
172      * Creates a new {@code ActivationFile} builder instance using default values or not.
173      *
174      * @param withDefaults the boolean indicating whether default values should be used
175      * @return a new {@code Builder}
176      */
177     @Nonnull
178     public static Builder newBuilder(boolean withDefaults) {
179         return new Builder(withDefaults);
180     }
181 
182     /**
183      * Creates a new {@code ActivationFile} builder instance using the specified object as a basis.
184      * Equivalent to {@code newBuilder(from, false)}.
185      *
186      * @param from the {@code ActivationFile} instance to use as a basis
187      * @return a new {@code Builder}
188      */
189     @Nonnull
190     public static Builder newBuilder(ActivationFile from) {
191         return newBuilder(from, false);
192     }
193 
194     /**
195      * Creates a new {@code ActivationFile} builder instance using the specified object as a basis.
196      *
197      * @param from the {@code ActivationFile} instance to use as a basis
198      * @param forceCopy the boolean indicating if a copy should be forced
199      * @return a new {@code Builder}
200      */
201     @Nonnull
202     public static Builder newBuilder(ActivationFile from, boolean forceCopy) {
203         return new Builder(from, forceCopy);
204     }
205 
206     /**
207      * Builder class used to create ActivationFile instances.
208      * @see #with()
209      * @see #newBuilder()
210      */
211     @NotThreadSafe
212     public static class Builder
213     {
214         ActivationFile base;
215         String missing;
216         String exists;
217         Map<Object, InputLocation> locations;
218         InputLocation importedFrom;
219 
220         protected Builder(boolean withDefaults) {
221             if (withDefaults) {
222             }
223         }
224 
225         protected Builder(ActivationFile base, boolean forceCopy) {
226             if (forceCopy) {
227                 this.missing = base.missing;
228                 this.exists = base.exists;
229                 this.locations = base.locations;
230                 this.importedFrom = base.importedFrom;
231             } else {
232                 this.base = base;
233             }
234         }
235 
236         @Nonnull
237         public Builder missing(String missing) {
238             this.missing = missing;
239             return this;
240         }
241 
242         @Nonnull
243         public Builder exists(String exists) {
244             this.exists = exists;
245             return this;
246         }
247 
248 
249         @Nonnull
250         public Builder location(Object key, InputLocation location) {
251             if (location != null) {
252                 if (!(this.locations instanceof HashMap)) {
253                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
254                 }
255                 this.locations.put(key, location);
256             }
257             return this;
258         }
259 
260         @Nonnull
261         public Builder importedFrom(InputLocation importedFrom) {
262             this.importedFrom = importedFrom;
263             return this;
264         }
265 
266         @Nonnull
267         public ActivationFile build() {
268             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
269             if (base != null
270                     && (missing == null || missing == base.missing)
271                     && (exists == null || exists == base.exists)
272             ) {
273                 return base;
274             }
275             return new ActivationFile(this);
276         }
277 
278         Map<Object, InputLocation> computeLocations() {
279             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
280             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
281             if (newlocs.isEmpty()) {
282                 return Map.copyOf(oldlocs);
283             }
284             if (oldlocs.isEmpty()) {
285                 return Map.copyOf(newlocs);
286             }
287             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
288                     // Keep value from newlocs in case of duplicates
289                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
290         }
291     }
292 
293 }