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   * Common base class that contains code to track the source for this instance.
25   */
26  @Experimental
27  @Generated @ThreadSafe @Immutable
28  public class TrackableBase
29      implements Serializable, InputLocationTracker
30  {
31      /** Locations */
32      final Map<Object, InputLocation> locations;
33      /** Location tracking */
34      final InputLocation importedFrom;
35  
36      /**
37        * Constructor for this class, to be called from its subclasses and {@link Builder}.
38        * @see Builder#build()
39        */
40      protected TrackableBase(Builder builder) {
41          this.locations = Map.of();
42          this.importedFrom = builder.importedFrom;
43      }
44  
45      /**
46       * Gets the location of the specified field in the input source.
47       */
48      public InputLocation getLocation(Object key) {
49          return null;
50      }
51  
52      /**
53       * Gets the keys of the locations of the input source.
54       */
55      public Set<Object> getLocationKeys() {
56          return Set.of();
57      }
58  
59      protected Stream<Object> getLocationKeyStream() {
60          return Stream.empty();
61      }
62  
63      /**
64       * Gets the input location that caused this model to be read.
65       */
66      public InputLocation getImportedFrom()
67      {
68          return importedFrom;
69      }
70  
71      /**
72       * Creates a new builder with this object as the basis.
73       *
74       * @return a {@code Builder}
75       */
76      @Nonnull
77      public Builder with() {
78          return newBuilder(this);
79      }
80  
81      /**
82       * Creates a new {@code TrackableBase} instance.
83       * Equivalent to {@code newInstance(true)}.
84       * @see #newInstance(boolean)
85       *
86       * @return a new {@code TrackableBase}
87       */
88      @Nonnull
89      public static TrackableBase newInstance() {
90          return newInstance(true);
91      }
92  
93      /**
94       * Creates a new {@code TrackableBase} instance using default values or not.
95       * Equivalent to {@code newBuilder(withDefaults).build()}.
96       *
97       * @param withDefaults the boolean indicating whether default values should be used
98       * @return a new {@code TrackableBase}
99       */
100     @Nonnull
101     public static TrackableBase newInstance(boolean withDefaults) {
102         return newBuilder(withDefaults).build();
103     }
104 
105     /**
106      * Creates a new {@code TrackableBase} builder instance.
107      * Equivalent to {@code newBuilder(true)}.
108      * @see #newBuilder(boolean)
109      *
110      * @return a new {@code Builder}
111      */
112     @Nonnull
113     public static Builder newBuilder() {
114         return newBuilder(true);
115     }
116 
117     /**
118      * Creates a new {@code TrackableBase} builder instance using default values or not.
119      *
120      * @param withDefaults the boolean indicating whether default values should be used
121      * @return a new {@code Builder}
122      */
123     @Nonnull
124     public static Builder newBuilder(boolean withDefaults) {
125         return new Builder(withDefaults);
126     }
127 
128     /**
129      * Creates a new {@code TrackableBase} builder instance using the specified object as a basis.
130      * Equivalent to {@code newBuilder(from, false)}.
131      *
132      * @param from the {@code TrackableBase} instance to use as a basis
133      * @return a new {@code Builder}
134      */
135     @Nonnull
136     public static Builder newBuilder(TrackableBase from) {
137         return newBuilder(from, false);
138     }
139 
140     /**
141      * Creates a new {@code TrackableBase} builder instance using the specified object as a basis.
142      *
143      * @param from the {@code TrackableBase} instance to use as a basis
144      * @param forceCopy the boolean indicating if a copy should be forced
145      * @return a new {@code Builder}
146      */
147     @Nonnull
148     public static Builder newBuilder(TrackableBase from, boolean forceCopy) {
149         return new Builder(from, forceCopy);
150     }
151 
152     /**
153      * Builder class used to create TrackableBase instances.
154      * @see #with()
155      * @see #newBuilder()
156      */
157     @NotThreadSafe
158     public static class Builder
159     {
160         TrackableBase base;
161         Map<Object, InputLocation> locations;
162         InputLocation importedFrom;
163 
164         protected Builder(boolean withDefaults) {
165             if (withDefaults) {
166             }
167         }
168 
169         protected Builder(TrackableBase base, boolean forceCopy) {
170             if (forceCopy) {
171                 this.locations = base.locations;
172                 this.importedFrom = base.importedFrom;
173             } else {
174                 this.base = base;
175             }
176         }
177 
178 
179         @Nonnull
180         public Builder location(Object key, InputLocation location) {
181             if (location != null) {
182                 if (!(this.locations instanceof HashMap)) {
183                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
184                 }
185                 this.locations.put(key, location);
186             }
187             return this;
188         }
189 
190         @Nonnull
191         public Builder importedFrom(InputLocation importedFrom) {
192             this.importedFrom = importedFrom;
193             return this;
194         }
195 
196         @Nonnull
197         public TrackableBase build() {
198             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
199             if (base != null
200             ) {
201                 return base;
202             }
203             return new TrackableBase(this);
204         }
205 
206         Map<Object, InputLocation> computeLocations() {
207             return Map.of();
208         }
209     }
210 
211 }