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