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