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