1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm;
20
21 import java.io.File;
22 import java.io.Serializable;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.Map;
26
27
28
29
30
31
32 public class CommandParameters implements Serializable {
33 private static final long serialVersionUID = -7346070735958137283L;
34
35 private Map<String, Object> parameters = new HashMap<>();
36
37
38
39
40
41
42
43
44
45
46
47
48 public String getString(CommandParameter parameter) throws ScmException {
49 Object object = getObject(String.class, parameter);
50
51 return object.toString();
52 }
53
54
55
56
57
58
59
60
61
62 public String getString(CommandParameter parameter, String defaultValue) throws ScmException {
63 Object object = getObject(String.class, parameter, null);
64
65 if (object == null) {
66 return defaultValue;
67 }
68
69 return object.toString();
70 }
71
72
73
74
75
76
77
78
79 public void setString(CommandParameter parameter, String value) throws ScmException {
80 setObject(parameter, value);
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94 public int getInt(CommandParameter parameter) throws ScmException {
95 return ((Integer) getObject(Integer.class, parameter)).intValue();
96 }
97
98
99
100
101
102
103
104
105
106 public int getInt(CommandParameter parameter, int defaultValue) throws ScmException {
107 Integer value = ((Integer) getObject(Integer.class, parameter, null));
108
109 if (value == null) {
110 return defaultValue;
111 }
112
113 return value.intValue();
114 }
115
116
117
118
119
120
121
122
123 public void setInt(CommandParameter parameter, int value) throws ScmException {
124 setObject(parameter, Integer.valueOf(value));
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138 public Date getDate(CommandParameter parameter) throws ScmException {
139 return (Date) getObject(Date.class, parameter);
140 }
141
142
143
144
145
146
147
148
149
150 public Date getDate(CommandParameter parameter, Date defaultValue) throws ScmException {
151 return (Date) getObject(Date.class, parameter, defaultValue);
152 }
153
154
155
156
157
158
159
160
161 public void setDate(CommandParameter parameter, Date date) throws ScmException {
162 setObject(parameter, date);
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176 public boolean getBoolean(CommandParameter parameter) throws ScmException {
177 return Boolean.valueOf(getString(parameter)).booleanValue();
178 }
179
180
181
182
183
184
185
186
187
188
189 public boolean getBoolean(CommandParameter parameter, boolean defaultValue) throws ScmException {
190 return Boolean.valueOf(getString(parameter, Boolean.toString(defaultValue)))
191 .booleanValue();
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205 public ScmVersion getScmVersion(CommandParameter parameter) throws ScmException {
206 return (ScmVersion) getObject(ScmVersion.class, parameter);
207 }
208
209
210
211
212
213
214
215
216
217 public ScmVersion getScmVersion(CommandParameter parameter, ScmVersion defaultValue) throws ScmException {
218 return (ScmVersion) getObject(ScmVersion.class, parameter, defaultValue);
219 }
220
221
222
223
224
225
226
227
228 public void setScmVersion(CommandParameter parameter, ScmVersion scmVersion) throws ScmException {
229 setObject(parameter, scmVersion);
230 }
231
232
233
234
235
236
237
238
239
240
241 public File[] getFileArray(CommandParameter parameter) throws ScmException {
242 return (File[]) getObject(File[].class, parameter);
243 }
244
245
246
247
248
249
250
251 public File[] getFileArray(CommandParameter parameter, File[] defaultValue) throws ScmException {
252 return (File[]) getObject(File[].class, parameter, defaultValue);
253 }
254
255 public ScmTagParameters getScmTagParameters(CommandParameter parameter) throws ScmException {
256 return (ScmTagParameters) getObject(ScmTagParameters.class, parameter, new ScmTagParameters());
257 }
258
259 public void setScmTagParameters(CommandParameter parameter, ScmTagParameters scmTagParameters) throws ScmException {
260 setObject(parameter, scmTagParameters);
261 }
262
263 public void setScmBranchParameters(CommandParameter parameter, ScmBranchParameters scmBranchParameters)
264 throws ScmException {
265 setObject(parameter, scmBranchParameters);
266 }
267
268 public ScmBranchParameters getScmBranchParameters(CommandParameter parameter) throws ScmException {
269 return (ScmBranchParameters) getObject(ScmBranchParameters.class, parameter, new ScmBranchParameters());
270 }
271
272
273
274
275
276
277
278
279
280
281
282
283
284 private Object getObject(Class<?> clazz, CommandParameter parameter) throws ScmException {
285 Object object = getObject(clazz, parameter, null);
286
287 if (object == null) {
288 throw new ScmException("Missing parameter: '" + parameter.getName() + "'.");
289 }
290
291 return object;
292 }
293
294
295
296
297
298
299
300
301
302
303 private Object getObject(Class<?> clazz, CommandParameter parameter, Object defaultValue) throws ScmException {
304 Object object = parameters.get(parameter.getName());
305
306 if (object == null) {
307 return defaultValue;
308 }
309
310 if (clazz != null && !clazz.isAssignableFrom(object.getClass())) {
311 throw new ScmException("Wrong parameter type for '" + parameter.getName() + ". " + "Expected: "
312 + clazz.getName() + ", got: " + object.getClass().getName());
313 }
314
315 return object;
316 }
317
318
319
320
321
322
323
324
325 private void setObject(CommandParameter parameter, Object value) throws ScmException {
326 Object object = getObject(null, parameter, null);
327
328 if (object != null) {
329 throw new ScmException("The parameter is already set: " + parameter.getName());
330 }
331
332 parameters.put(parameter.getName(), value);
333 }
334
335
336
337
338
339
340 public void remove(CommandParameter parameter) {
341 parameters.remove(parameter.getName());
342 }
343 }