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