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 * Base class for {@code Mirror}, {@code Profile}, {@code Proxy} and {@code Server}.
25 */
26 @Experimental
27 @Generated @ThreadSafe @Immutable
28 public class IdentifiableBase
29 extends TrackableBase
30 implements Serializable, InputLocationTracker
31 {
32 /**
33 * Item identifier.
34 */
35 final String id;
36
37 /**
38 * Constructor for this class, to be called from its subclasses and {@link Builder}.
39 * @see Builder#build()
40 */
41 protected IdentifiableBase(Builder builder) {
42 super(builder);
43 this.id = builder.id != null ? builder.id : (builder.base != null ? builder.base.id : null);
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 protected Builder(boolean withDefaults) {
159 super(withDefaults);
160 if (withDefaults) {
161 this.id = "default";
162 }
163 }
164
165 protected Builder(IdentifiableBase base, boolean forceCopy) {
166 super(base, forceCopy);
167 if (forceCopy) {
168 this.id = base.id;
169 this.locations = base.locations;
170 this.importedFrom = base.importedFrom;
171 } else {
172 this.base = base;
173 }
174 }
175
176 @Nonnull
177 public Builder id(String id) {
178 this.id = id;
179 return this;
180 }
181
182
183 @Nonnull
184 public Builder location(Object key, InputLocation location) {
185 if (location != null) {
186 if (!(this.locations instanceof HashMap)) {
187 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
188 }
189 this.locations.put(key, location);
190 }
191 return this;
192 }
193
194 @Nonnull
195 public Builder importedFrom(InputLocation importedFrom) {
196 this.importedFrom = importedFrom;
197 return this;
198 }
199
200 @Nonnull
201 public IdentifiableBase build() {
202 // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
203 if (base != null
204 && (id == null || id == base.id)
205 ) {
206 return base;
207 }
208 return new IdentifiableBase(this);
209 }
210
211 }
212
213 }