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