1
2
3
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
21
22 @Experimental
23 @Generated @ThreadSafe @Immutable
24 public class IdentifiableBase
25 extends TrackableBase
26 implements Serializable, InputLocationTracker
27 {
28
29
30
31 final String id;
32
33 final Map<Object, InputLocation> locations;
34
35
36
37
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
51
52
53
54 public String getId() {
55 return this.id;
56 }
57
58
59
60
61 public InputLocation getLocation(Object key) {
62 return locations != null ? locations.get(key) : null;
63 }
64
65
66
67
68 public Set<Object> getLocationKeys() {
69 return locations != null ? locations.keySet() : null;
70 }
71
72
73
74
75
76
77 @Nonnull
78 public Builder with() {
79 return newBuilder(this);
80 }
81
82
83
84
85
86
87 @Nonnull
88 public IdentifiableBase withId(String id) {
89 return newBuilder(this, true).id(id).build();
90 }
91
92
93
94
95
96
97
98
99 @Nonnull
100 public static IdentifiableBase newInstance() {
101 return newInstance(true);
102 }
103
104
105
106
107
108
109
110
111 @Nonnull
112 public static IdentifiableBase newInstance(boolean withDefaults) {
113 return newBuilder(withDefaults).build();
114 }
115
116
117
118
119
120
121
122
123 @Nonnull
124 public static Builder newBuilder() {
125 return newBuilder(true);
126 }
127
128
129
130
131
132
133
134 @Nonnull
135 public static Builder newBuilder(boolean withDefaults) {
136 return new Builder(withDefaults);
137 }
138
139
140
141
142
143
144
145
146 @Nonnull
147 public static Builder newBuilder(IdentifiableBase from) {
148 return newBuilder(from, false);
149 }
150
151
152
153
154
155
156
157
158 @Nonnull
159 public static Builder newBuilder(IdentifiableBase from, boolean forceCopy) {
160 return new Builder(from, forceCopy);
161 }
162
163
164
165
166
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
220 if (base != null
221 && (id == null || id == base.id)
222 ) {
223 return base;
224 }
225 return new IdentifiableBase(this);
226 }
227 }
228
229 }