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