Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CommandParameters |
|
| 1.6521739130434783;1,652 |
1 | package org.apache.maven.scm; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.io.File; | |
23 | import java.io.Serializable; | |
24 | import java.util.Date; | |
25 | import java.util.HashMap; | |
26 | import java.util.Map; | |
27 | ||
28 | /** | |
29 | * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> | |
30 | * @author Olivier Lamy | |
31 | * @version $Id: CommandParameters.java 1306858 2012-03-29 13:41:29Z olamy $ | |
32 | */ | |
33 | 0 | public class CommandParameters |
34 | implements Serializable | |
35 | { | |
36 | private static final long serialVersionUID = -7346070735958137283L; | |
37 | ||
38 | 0 | private Map<String, Object> parameters = new HashMap<String, Object>(); |
39 | ||
40 | // ---------------------------------------------------------------------- | |
41 | // String | |
42 | // ---------------------------------------------------------------------- | |
43 | ||
44 | /** | |
45 | * Return the parameter value as String. | |
46 | * | |
47 | * @param parameter The parameter | |
48 | * @return The parameter value as a String | |
49 | * @throws ScmException if the parameter doesn't exist | |
50 | */ | |
51 | public String getString( CommandParameter parameter ) | |
52 | throws ScmException | |
53 | { | |
54 | 0 | Object object = getObject( String.class, parameter ); |
55 | ||
56 | 0 | if ( object == null ) |
57 | { | |
58 | 0 | throw new ScmException( "Missing parameter: '" + parameter.getName() + "'." ); |
59 | } | |
60 | ||
61 | 0 | return object.toString(); |
62 | } | |
63 | ||
64 | /** | |
65 | * Return the parameter value or the default value if it doesn't exist. | |
66 | * | |
67 | * @param parameter The parameter | |
68 | * @param defaultValue The default value | |
69 | * @return The parameter value as a String | |
70 | * @throws ScmException if the value is in the wrong type | |
71 | */ | |
72 | public String getString( CommandParameter parameter, String defaultValue ) | |
73 | throws ScmException | |
74 | { | |
75 | 0 | Object object = getObject( String.class, parameter, null ); |
76 | ||
77 | 0 | if ( object == null ) |
78 | { | |
79 | 0 | return defaultValue; |
80 | } | |
81 | ||
82 | 0 | return object.toString(); |
83 | } | |
84 | ||
85 | /** | |
86 | * Set a parameter value. | |
87 | * | |
88 | * @param parameter The parameter name | |
89 | * @param value The value of the parameter | |
90 | * @throws ScmException if the parameter already exist | |
91 | */ | |
92 | public void setString( CommandParameter parameter, String value ) | |
93 | throws ScmException | |
94 | { | |
95 | 0 | setObject( parameter, value ); |
96 | 0 | } |
97 | ||
98 | // ---------------------------------------------------------------------- | |
99 | // Int | |
100 | // ---------------------------------------------------------------------- | |
101 | ||
102 | /** | |
103 | * Return the parameter value as int. | |
104 | * | |
105 | * @param parameter The parameter | |
106 | * @return The parameter value as a String | |
107 | * @throws ScmException if the parameter doesn't exist | |
108 | */ | |
109 | public int getInt( CommandParameter parameter ) | |
110 | throws ScmException | |
111 | { | |
112 | 0 | return ( (Integer) getObject( Integer.class, parameter ) ).intValue(); |
113 | } | |
114 | ||
115 | /** | |
116 | * Return the parameter value as int or the default value if it doesn't exist. | |
117 | * | |
118 | * @param parameter The parameter | |
119 | * @param defaultValue The defaultValue | |
120 | * @return The parameter value as a int | |
121 | * @throws ScmException if the value is in the wrong type | |
122 | */ | |
123 | public int getInt( CommandParameter parameter, int defaultValue ) | |
124 | throws ScmException | |
125 | { | |
126 | 0 | Integer value = ( (Integer) getObject( Integer.class, parameter, null ) ); |
127 | ||
128 | 0 | if ( value == null ) |
129 | { | |
130 | 0 | return defaultValue; |
131 | } | |
132 | ||
133 | 0 | return value.intValue(); |
134 | } | |
135 | ||
136 | /** | |
137 | * Set a parameter value. | |
138 | * | |
139 | * @param parameter The parameter name | |
140 | * @param value The value of the parameter | |
141 | * @throws ScmException if the parameter already exist | |
142 | */ | |
143 | public void setInt( CommandParameter parameter, int value ) | |
144 | throws ScmException | |
145 | { | |
146 | 0 | setObject( parameter, Integer.valueOf( value ) ); |
147 | 0 | } |
148 | ||
149 | // ---------------------------------------------------------------------- | |
150 | // Date | |
151 | // ---------------------------------------------------------------------- | |
152 | ||
153 | /** | |
154 | * Return the parameter value as Date. | |
155 | * | |
156 | * @param parameter The parameter | |
157 | * @return The parameter value as a Date | |
158 | * @throws ScmException if the parameter doesn't exist | |
159 | */ | |
160 | public Date getDate( CommandParameter parameter ) | |
161 | throws ScmException | |
162 | { | |
163 | 0 | return (Date) getObject( Date.class, parameter ); |
164 | } | |
165 | ||
166 | /** | |
167 | * Return the parameter value as String or the default value if it doesn't exist. | |
168 | * | |
169 | * @param parameter The parameter | |
170 | * @param defaultValue The defaultValue | |
171 | * @return The parameter value as a Date | |
172 | * @throws ScmException if the value is in the wrong type | |
173 | */ | |
174 | public Date getDate( CommandParameter parameter, Date defaultValue ) | |
175 | throws ScmException | |
176 | { | |
177 | 0 | return (Date) getObject( Date.class, parameter, defaultValue ); |
178 | } | |
179 | ||
180 | /** | |
181 | * Set a parameter value. | |
182 | * | |
183 | * @param parameter The parameter name | |
184 | * @param date The value of the parameter | |
185 | * @throws ScmException if the parameter already exist | |
186 | */ | |
187 | public void setDate( CommandParameter parameter, Date date ) | |
188 | throws ScmException | |
189 | { | |
190 | 0 | setObject( parameter, date ); |
191 | 0 | } |
192 | ||
193 | // ---------------------------------------------------------------------- | |
194 | // Boolean | |
195 | // ---------------------------------------------------------------------- | |
196 | ||
197 | /** | |
198 | * Return the parameter value as boolean. | |
199 | * | |
200 | * @param parameter The parameter | |
201 | * @return The parameter value as a boolean | |
202 | * @throws ScmException if the parameter doesn't exist | |
203 | */ | |
204 | public boolean getBoolean( CommandParameter parameter ) | |
205 | throws ScmException | |
206 | { | |
207 | 0 | return Boolean.valueOf( getString( parameter ) ).booleanValue(); |
208 | } | |
209 | ||
210 | /** | |
211 | * Return the parameter value as boolean. | |
212 | * | |
213 | * @since 1.7 | |
214 | * @param parameter The parameter | |
215 | * @param defaultValue default value if parameter not exists | |
216 | * @return The parameter value as a boolean | |
217 | */ | |
218 | public boolean getBoolean( CommandParameter parameter, boolean defaultValue ) | |
219 | throws ScmException | |
220 | { | |
221 | 0 | return Boolean.valueOf( getString( parameter, Boolean.toString( defaultValue ) ) ).booleanValue(); |
222 | } | |
223 | ||
224 | // ---------------------------------------------------------------------- | |
225 | // ScmVersion | |
226 | // ---------------------------------------------------------------------- | |
227 | ||
228 | /** | |
229 | * Return the parameter value as ScmVersion. | |
230 | * | |
231 | * @param parameter The parameter | |
232 | * @return The parameter value as a ScmVersion | |
233 | * @throws ScmException if the parameter doesn't exist | |
234 | */ | |
235 | public ScmVersion getScmVersion( CommandParameter parameter ) | |
236 | throws ScmException | |
237 | { | |
238 | 0 | return (ScmVersion) getObject( ScmVersion.class, parameter ); |
239 | } | |
240 | ||
241 | /** | |
242 | * Return the parameter value as ScmVersion or the default value. | |
243 | * | |
244 | * @param parameter The parameter | |
245 | * @param defaultValue The default value | |
246 | * @return The parameter value as a ScmVersion | |
247 | * @throws ScmException if the parameter doesn't exist | |
248 | */ | |
249 | public ScmVersion getScmVersion( CommandParameter parameter, ScmVersion defaultValue ) | |
250 | throws ScmException | |
251 | { | |
252 | 0 | return (ScmVersion) getObject( ScmVersion.class, parameter, defaultValue ); |
253 | } | |
254 | ||
255 | /** | |
256 | * Set a parameter value. | |
257 | * | |
258 | * @param parameter The parameter name | |
259 | * @param scmVersion The tbranch/tag/revision | |
260 | * @throws ScmException if the parameter already exist | |
261 | */ | |
262 | public void setScmVersion( CommandParameter parameter, ScmVersion scmVersion ) | |
263 | throws ScmException | |
264 | { | |
265 | 0 | setObject( parameter, scmVersion ); |
266 | 0 | } |
267 | ||
268 | // ---------------------------------------------------------------------- | |
269 | // File[] | |
270 | // ---------------------------------------------------------------------- | |
271 | ||
272 | /** | |
273 | * @param parameter not null | |
274 | * @return an array of files | |
275 | * @throws ScmException if any | |
276 | */ | |
277 | public File[] getFileArray( CommandParameter parameter ) | |
278 | throws ScmException | |
279 | { | |
280 | 0 | return (File[]) getObject( File[].class, parameter ); |
281 | } | |
282 | ||
283 | /** | |
284 | * @param parameter not null | |
285 | * @param defaultValue could be null | |
286 | * @return an array of files | |
287 | * @throws ScmException if any | |
288 | */ | |
289 | public File[] getFileArray( CommandParameter parameter, File[] defaultValue ) | |
290 | throws ScmException | |
291 | { | |
292 | 0 | return (File[]) getObject( File[].class, parameter, defaultValue ); |
293 | } | |
294 | ||
295 | ||
296 | public ScmTagParameters getScmTagParameters( CommandParameter parameter ) | |
297 | throws ScmException | |
298 | { | |
299 | 0 | return (ScmTagParameters) getObject( ScmTagParameters.class, parameter, new ScmTagParameters() ); |
300 | } | |
301 | ||
302 | public void setScmTagParameters( CommandParameter parameter, ScmTagParameters scmTagParameters ) | |
303 | throws ScmException | |
304 | { | |
305 | 0 | setObject( parameter, scmTagParameters ); |
306 | 0 | } |
307 | ||
308 | public void setScmBranchParameters( CommandParameter parameter, ScmBranchParameters scmBranchParameters ) | |
309 | throws ScmException | |
310 | { | |
311 | 0 | setObject( parameter, scmBranchParameters ); |
312 | 0 | } |
313 | ||
314 | public ScmBranchParameters getScmBranchParameters( CommandParameter parameter ) | |
315 | throws ScmException | |
316 | { | |
317 | 0 | return (ScmBranchParameters) getObject( ScmBranchParameters.class, parameter, new ScmBranchParameters() ); |
318 | } | |
319 | ||
320 | // ---------------------------------------------------------------------- | |
321 | // | |
322 | // ---------------------------------------------------------------------- | |
323 | ||
324 | /** | |
325 | * Return the value object. | |
326 | * | |
327 | * @param clazz The type of the parameter value | |
328 | * @param parameter The parameter | |
329 | * @return The parameter value | |
330 | * @throws ScmException if the parameter doesn't exist | |
331 | */ | |
332 | private Object getObject( Class<?> clazz, CommandParameter parameter ) | |
333 | throws ScmException | |
334 | { | |
335 | 0 | Object object = getObject( clazz, parameter, null ); |
336 | ||
337 | 0 | if ( object == null ) |
338 | { | |
339 | 0 | throw new ScmException( "Missing parameter: '" + parameter.getName() + "'." ); |
340 | } | |
341 | ||
342 | 0 | return object; |
343 | } | |
344 | ||
345 | /** | |
346 | * Return the value object or the default value if it doesn't exist. | |
347 | * | |
348 | * @param clazz The type of the parameter value | |
349 | * @param parameter The parameter | |
350 | * @param defaultValue The defaultValue | |
351 | * @return The parameter value | |
352 | * @throws ScmException if the defaultValue is in the wrong type | |
353 | */ | |
354 | private Object getObject( Class<?> clazz, CommandParameter parameter, Object defaultValue ) | |
355 | throws ScmException | |
356 | { | |
357 | 0 | Object object = parameters.get( parameter.getName() ); |
358 | ||
359 | 0 | if ( object == null ) |
360 | { | |
361 | 0 | return defaultValue; |
362 | } | |
363 | ||
364 | 0 | if ( clazz != null && !clazz.isAssignableFrom( object.getClass() ) ) |
365 | { | |
366 | 0 | throw new ScmException( |
367 | "Wrong parameter type for '" + parameter.getName() + ". " + "Expected: " + clazz.getName() + ", got: " | |
368 | + object.getClass().getName() ); | |
369 | } | |
370 | ||
371 | 0 | return object; |
372 | } | |
373 | ||
374 | /** | |
375 | * Set the parameter value. | |
376 | * | |
377 | * @param parameter The parameter | |
378 | * @param value The parameter value | |
379 | * @throws ScmException if the parameter already exist | |
380 | */ | |
381 | private void setObject( CommandParameter parameter, Object value ) | |
382 | throws ScmException | |
383 | { | |
384 | 0 | Object object = getObject( null, parameter, null ); |
385 | ||
386 | 0 | if ( object != null ) |
387 | { | |
388 | 0 | throw new ScmException( "The parameter is already set: " + parameter.getName() ); |
389 | } | |
390 | ||
391 | 0 | parameters.put( parameter.getName(), value ); |
392 | 0 | } |
393 | } |