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 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
20
21 @Experimental
22 @Generated @ThreadSafe @Immutable
23 public class IdentifiableBase
24 extends TrackableBase
25 implements Serializable, InputLocationTracker
26 {
27
28
29
30 final String id;
31
32
33
34
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
48
49
50
51 public String getId() {
52 return this.id;
53 }
54
55
56
57
58
59
60 @Nonnull
61 public Builder with() {
62 return newBuilder(this);
63 }
64
65
66
67
68
69
70 @Nonnull
71 public IdentifiableBase withId(String id) {
72 return newBuilder(this, true).id(id).build();
73 }
74
75
76
77
78
79
80
81
82 @Nonnull
83 public static IdentifiableBase newInstance() {
84 return newInstance(true);
85 }
86
87
88
89
90
91
92
93
94 @Nonnull
95 public static IdentifiableBase newInstance(boolean withDefaults) {
96 return newBuilder(withDefaults).build();
97 }
98
99
100
101
102
103
104
105
106 @Nonnull
107 public static Builder newBuilder() {
108 return newBuilder(true);
109 }
110
111
112
113
114
115
116
117 @Nonnull
118 public static Builder newBuilder(boolean withDefaults) {
119 return new Builder(withDefaults);
120 }
121
122
123
124
125
126
127
128
129 @Nonnull
130 public static Builder newBuilder(IdentifiableBase from) {
131 return newBuilder(from, false);
132 }
133
134
135
136
137
138
139
140
141 @Nonnull
142 public static Builder newBuilder(IdentifiableBase from, boolean forceCopy) {
143 return new Builder(from, forceCopy);
144 }
145
146
147
148
149
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 }