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.parseBoolean(getString(parameter, Boolean.toString(defaultValue)));
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204 public ScmVersion getScmVersion(CommandParameter parameter) throws ScmException {
205 return (ScmVersion) getObject(ScmVersion.class, parameter);
206 }
207
208
209
210
211
212
213
214
215
216 public ScmVersion getScmVersion(CommandParameter parameter, ScmVersion defaultValue) throws ScmException {
217 return (ScmVersion) getObject(ScmVersion.class, parameter, defaultValue);
218 }
219
220
221
222
223
224
225
226
227 public void setScmVersion(CommandParameter parameter, ScmVersion scmVersion) throws ScmException {
228 setObject(parameter, scmVersion);
229 }
230
231
232
233
234
235
236
237
238
239
240 public File[] getFileArray(CommandParameter parameter) throws ScmException {
241 return (File[]) getObject(File[].class, parameter);
242 }
243
244
245
246
247
248
249
250 public File[] getFileArray(CommandParameter parameter, File[] defaultValue) throws ScmException {
251 return (File[]) getObject(File[].class, parameter, defaultValue);
252 }
253
254 public ScmTagParameters getScmTagParameters(CommandParameter parameter) throws ScmException {
255 return (ScmTagParameters) getObject(ScmTagParameters.class, parameter, new ScmTagParameters());
256 }
257
258 public void setScmTagParameters(CommandParameter parameter, ScmTagParameters scmTagParameters) throws ScmException {
259 setObject(parameter, scmTagParameters);
260 }
261
262 public void setScmBranchParameters(CommandParameter parameter, ScmBranchParameters scmBranchParameters)
263 throws ScmException {
264 setObject(parameter, scmBranchParameters);
265 }
266
267 public ScmBranchParameters getScmBranchParameters(CommandParameter parameter) throws ScmException {
268 return (ScmBranchParameters) getObject(ScmBranchParameters.class, parameter, new ScmBranchParameters());
269 }
270
271
272
273
274
275
276
277
278
279
280
281
282
283 private Object getObject(Class<?> clazz, CommandParameter parameter) throws ScmException {
284 Object object = getObject(clazz, parameter, null);
285
286 if (object == null) {
287 throw new ScmException("Missing parameter: '" + parameter.getName() + "'.");
288 }
289
290 return object;
291 }
292
293
294
295
296
297
298
299
300
301
302 private Object getObject(Class<?> clazz, CommandParameter parameter, Object defaultValue) throws ScmException {
303 Object object = parameters.get(parameter.getName());
304
305 if (object == null) {
306 return defaultValue;
307 }
308
309 if (clazz != null && !clazz.isAssignableFrom(object.getClass())) {
310 throw new ScmException("Wrong parameter type for '" + parameter.getName() + ". " + "Expected: "
311 + clazz.getName() + ", got: " + object.getClass().getName());
312 }
313
314 return object;
315 }
316
317
318
319
320
321
322
323
324 private void setObject(CommandParameter parameter, Object value) throws ScmException {
325 Object object = getObject(null, parameter, null);
326
327 if (object != null) {
328 throw new ScmException("The parameter is already set: " + parameter.getName());
329 }
330
331 parameters.put(parameter.getName(), value);
332 }
333
334
335
336
337
338
339 public void remove(CommandParameter parameter) {
340 parameters.remove(parameter.getName());
341 }
342 }