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