1
2
3
4
5 package org.apache.maven.api.model;
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 Prerequisites
24 implements Serializable, InputLocationTracker
25 {
26
27
28
29
30
31
32
33
34 final String maven;
35
36 final Map<Object, InputLocation> locations;
37
38
39
40
41
42 Prerequisites(
43 String maven,
44 Map<Object, InputLocation> locations
45 ) {
46 this.maven = maven;
47 this.locations = ImmutableCollections.copy(locations);
48 }
49
50
51
52
53
54
55
56
57
58
59
60 public String getMaven() {
61 return this.maven;
62 }
63
64
65
66
67 public InputLocation getLocation(Object key) {
68 return locations != null ? locations.get(key) : null;
69 }
70
71
72
73
74
75
76 @Nonnull
77 public Builder with() {
78 return newBuilder(this);
79 }
80
81
82
83
84
85
86 @Nonnull
87 public Prerequisites withMaven(String maven) {
88 return newBuilder(this, true).maven(maven).build();
89 }
90
91
92
93
94
95
96
97
98 @Nonnull
99 public static Prerequisites newInstance() {
100 return newInstance(true);
101 }
102
103
104
105
106
107
108
109
110 @Nonnull
111 public static Prerequisites newInstance(boolean withDefaults) {
112 return newBuilder(withDefaults).build();
113 }
114
115
116
117
118
119
120
121
122 @Nonnull
123 public static Builder newBuilder() {
124 return newBuilder(true);
125 }
126
127
128
129
130
131
132
133 @Nonnull
134 public static Builder newBuilder(boolean withDefaults) {
135 return new Builder(withDefaults);
136 }
137
138
139
140
141
142
143
144
145 @Nonnull
146 public static Builder newBuilder(Prerequisites from) {
147 return newBuilder(from, false);
148 }
149
150
151
152
153
154
155
156
157 @Nonnull
158 public static Builder newBuilder(Prerequisites from, boolean forceCopy) {
159 return new Builder(from, forceCopy);
160 }
161
162
163
164
165
166
167 @NotThreadSafe
168 public static class Builder
169 {
170 Prerequisites base;
171 String maven;
172 Map<Object, InputLocation> locations;
173
174 Builder(boolean withDefaults) {
175 if (withDefaults) {
176 this.maven = "2.0";
177 }
178 }
179
180 Builder(Prerequisites base, boolean forceCopy) {
181 if (forceCopy) {
182 this.maven = base.maven;
183 this.locations = base.locations;
184 } else {
185 this.base = base;
186 }
187 }
188
189 @Nonnull
190 public Builder maven(String maven) {
191 this.maven = maven;
192 return this;
193 }
194
195
196 @Nonnull
197 public Builder location(Object key, InputLocation location) {
198 if (location != null) {
199 if (!(this.locations instanceof HashMap)) {
200 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
201 }
202 this.locations.put(key, location);
203 }
204 return this;
205 }
206
207 @Nonnull
208 public Prerequisites build() {
209 if (base != null
210 && (maven == null || maven == base.maven)
211 ) {
212 return base;
213 }
214 Map<Object, InputLocation> newlocs = this.locations != null ? this.locations : Collections.emptyMap();
215 Map<Object, InputLocation> oldlocs = this.base != null && this.base.locations != null ? this.base.locations : Collections.emptyMap();
216 Map<Object, InputLocation> locations = new HashMap<>();
217 locations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
218 locations.put("maven", newlocs.containsKey("maven") ? newlocs.get("maven") : oldlocs.get("maven"));
219 return new Prerequisites(
220 maven != null ? maven : (base != null ? base.maven : null),
221 locations
222 );
223 }
224 }
225
226 }