1
2
3
4 package org.apache.maven.api.model;
5
6 import java.io.Serializable;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.Map;
10 import org.apache.maven.api.annotations.Experimental;
11 import org.apache.maven.api.annotations.Generated;
12 import org.apache.maven.api.annotations.Immutable;
13 import org.apache.maven.api.annotations.Nonnull;
14 import org.apache.maven.api.annotations.NotThreadSafe;
15 import org.apache.maven.api.annotations.ThreadSafe;
16
17
18
19
20 @Experimental
21 @Generated @ThreadSafe @Immutable
22 public class Prerequisites
23 implements Serializable, InputLocationTracker
24 {
25
26
27
28
29
30
31
32
33 final String maven;
34
35 final InputLocation location;
36
37 final InputLocation mavenLocation;
38
39 final Map<Object, InputLocation> locations;
40
41
42
43
44
45 Prerequisites(
46 String maven,
47 Map<Object, InputLocation> locations,
48 InputLocation location,
49 InputLocation mavenLocation
50 )
51 {
52 this.maven = maven;
53 this.locations = ImmutableCollections.copy( locations );
54 this.location = location;
55 this.mavenLocation = mavenLocation;
56 }
57
58
59
60
61
62
63
64
65
66
67
68 public String getMaven()
69 {
70 return this.maven;
71 }
72
73
74
75
76 public InputLocation getLocation( Object key )
77 {
78 if ( key instanceof String )
79 {
80 switch ( ( String ) key )
81 {
82 case "":
83 return location;
84 case "maven":
85 return mavenLocation;
86 }
87 }
88 return locations != null ? locations.get( key ) : null;
89 }
90
91
92
93
94
95
96 @Nonnull
97 public Builder with()
98 {
99 return newBuilder( this );
100 }
101
102
103
104
105
106
107 @Nonnull
108 public Prerequisites withMaven( String maven )
109 {
110 return with().maven( maven ).build();
111 }
112
113
114
115
116
117
118
119
120 @Nonnull
121 public static Prerequisites newInstance()
122 {
123 return newInstance( true );
124 }
125
126
127
128
129
130
131
132
133 @Nonnull
134 public static Prerequisites newInstance( boolean withDefaults )
135 {
136 return newBuilder( withDefaults ).build();
137 }
138
139
140
141
142
143
144
145
146 @Nonnull
147 public static Builder newBuilder()
148 {
149 return newBuilder( true );
150 }
151
152
153
154
155
156
157
158 @Nonnull
159 public static Builder newBuilder( boolean withDefaults )
160 {
161 return new Builder( withDefaults );
162 }
163
164
165
166
167
168
169
170
171 @Nonnull
172 public static Builder newBuilder( Prerequisites from )
173 {
174 return newBuilder( from, false );
175 }
176
177
178
179
180
181
182
183
184 @Nonnull
185 public static Builder newBuilder( Prerequisites from, boolean forceCopy )
186 {
187 return new Builder( from, forceCopy );
188 }
189
190
191
192
193
194
195 @NotThreadSafe
196 public static class Builder
197 {
198 Prerequisites base;
199 String maven;
200 Map<Object, InputLocation> locations;
201
202 Builder( boolean withDefaults )
203 {
204 if ( withDefaults )
205 {
206 this.maven = "2.0";
207 }
208 }
209
210 Builder( Prerequisites base, boolean forceCopy )
211 {
212 if ( forceCopy )
213 {
214 this.maven = base.maven;
215 }
216 else
217 {
218 this.base = base;
219 }
220 }
221
222 @Nonnull
223 public Builder maven( String maven )
224 {
225 this.maven = maven;
226 return this;
227 }
228
229
230 @Nonnull
231 public Builder location( Object key, InputLocation location )
232 {
233 if ( location != null )
234 {
235 if ( this.locations == null )
236 {
237 this.locations = new HashMap<>();
238 }
239 this.locations.put( key, location );
240 }
241 return this;
242 }
243
244 @Nonnull
245 public Prerequisites build()
246 {
247 if ( base != null
248 && ( maven == null || maven == base.maven )
249 )
250 {
251 return base;
252 }
253 Map<Object, InputLocation> locations = null;
254 InputLocation location = null;
255 InputLocation mavenLocation = null;
256 if ( this.locations != null )
257 {
258 locations = this.locations;
259 location = locations.remove( "" );
260 mavenLocation = locations.remove( "maven" );
261 }
262 return new Prerequisites(
263 maven != null ? maven : ( base != null ? base.maven : null ),
264 locations != null ? locations : ( base != null ? base.locations : null ),
265 location != null ? location : ( base != null ? base.location : null ),
266 mavenLocation != null ? mavenLocation : ( base != null ? base.mavenLocation : null )
267 );
268 }
269 }
270
271 }