001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm;
020
021import java.io.File;
022import java.io.Serializable;
023import java.util.Date;
024import java.util.HashMap;
025import java.util.Map;
026
027/**
028 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
029 * @author Olivier Lamy
030 *
031 */
032public class CommandParameters implements Serializable {
033    private static final long serialVersionUID = -7346070735958137283L;
034
035    private Map<String, Object> parameters = new HashMap<>();
036
037    // ----------------------------------------------------------------------
038    // String
039    // ----------------------------------------------------------------------
040
041    /**
042     * Return the parameter value as String.
043     *
044     * @param parameter The parameter
045     * @return The parameter value as a String
046     * @throws ScmException if the parameter doesn't exist
047     */
048    public String getString(CommandParameter parameter) throws ScmException {
049        Object object = getObject(String.class, parameter);
050
051        return object.toString();
052    }
053
054    /**
055     * Return the parameter value or the default value if it doesn't exist.
056     *
057     * @param parameter    The parameter
058     * @param defaultValue The default value
059     * @return The parameter value as a String
060     * @throws ScmException if the value is in the wrong type
061     */
062    public String getString(CommandParameter parameter, String defaultValue) throws ScmException {
063        Object object = getObject(String.class, parameter, null);
064
065        if (object == null) {
066            return defaultValue;
067        }
068
069        return object.toString();
070    }
071
072    /**
073     * Set a parameter value.
074     *
075     * @param parameter The parameter name
076     * @param value     The value of the parameter
077     * @throws ScmException if the parameter already exist
078     */
079    public void setString(CommandParameter parameter, String value) throws ScmException {
080        setObject(parameter, value);
081    }
082
083    // ----------------------------------------------------------------------
084    // Int
085    // ----------------------------------------------------------------------
086
087    /**
088     * Return the parameter value as int.
089     *
090     * @param parameter The parameter
091     * @return The parameter value as a String
092     * @throws ScmException if the parameter doesn't exist
093     */
094    public int getInt(CommandParameter parameter) throws ScmException {
095        return ((Integer) getObject(Integer.class, parameter)).intValue();
096    }
097
098    /**
099     * Return the parameter value as int or the default value if it doesn't exist.
100     *
101     * @param parameter    The parameter
102     * @param defaultValue The defaultValue
103     * @return The parameter value as a int
104     * @throws ScmException if the value is in the wrong type
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     * Set a parameter value.
118     *
119     * @param parameter The parameter name
120     * @param value     The value of the parameter
121     * @throws ScmException if the parameter already exist
122     */
123    public void setInt(CommandParameter parameter, int value) throws ScmException {
124        setObject(parameter, Integer.valueOf(value));
125    }
126
127    // ----------------------------------------------------------------------
128    // Date
129    // ----------------------------------------------------------------------
130
131    /**
132     * Return the parameter value as Date.
133     *
134     * @param parameter The parameter
135     * @return The parameter value as a Date
136     * @throws ScmException if the parameter doesn't exist
137     */
138    public Date getDate(CommandParameter parameter) throws ScmException {
139        return (Date) getObject(Date.class, parameter);
140    }
141
142    /**
143     * Return the parameter value as String or the default value if it doesn't exist.
144     *
145     * @param parameter    The parameter
146     * @param defaultValue The defaultValue
147     * @return The parameter value as a Date
148     * @throws ScmException if the value is in the wrong type
149     */
150    public Date getDate(CommandParameter parameter, Date defaultValue) throws ScmException {
151        return (Date) getObject(Date.class, parameter, defaultValue);
152    }
153
154    /**
155     * Set a parameter value.
156     *
157     * @param parameter The parameter name
158     * @param date      The value of the parameter
159     * @throws ScmException if the parameter already exist
160     */
161    public void setDate(CommandParameter parameter, Date date) throws ScmException {
162        setObject(parameter, date);
163    }
164
165    // ----------------------------------------------------------------------
166    // Boolean
167    // ----------------------------------------------------------------------
168
169    /**
170     * Return the parameter value as boolean.
171     *
172     * @param parameter The parameter
173     * @return The parameter value as a boolean
174     * @throws ScmException if the parameter doesn't exist
175     */
176    public boolean getBoolean(CommandParameter parameter) throws ScmException {
177        return Boolean.valueOf(getString(parameter)).booleanValue();
178    }
179
180    /**
181     * Return the parameter value as boolean.
182     *
183     * @since 1.7
184     * @param parameter    The parameter
185     * @param defaultValue default value if parameter not exists
186     * @return The parameter value as a boolean
187     * @throws ScmException if the parameter doesn't exist
188     */
189    public boolean getBoolean(CommandParameter parameter, boolean defaultValue) throws ScmException {
190        return Boolean.parseBoolean(getString(parameter, Boolean.toString(defaultValue)));
191    }
192
193    // ----------------------------------------------------------------------
194    // ScmVersion
195    // ----------------------------------------------------------------------
196
197    /**
198     * Return the parameter value as ScmVersion.
199     *
200     * @param parameter The parameter
201     * @return The parameter value as a ScmVersion
202     * @throws ScmException if the parameter doesn't exist
203     */
204    public ScmVersion getScmVersion(CommandParameter parameter) throws ScmException {
205        return (ScmVersion) getObject(ScmVersion.class, parameter);
206    }
207
208    /**
209     * Return the parameter value as ScmVersion or the default value.
210     *
211     * @param parameter    The parameter
212     * @param defaultValue The default value
213     * @return The parameter value as a ScmVersion
214     * @throws ScmException if the parameter doesn't exist
215     */
216    public ScmVersion getScmVersion(CommandParameter parameter, ScmVersion defaultValue) throws ScmException {
217        return (ScmVersion) getObject(ScmVersion.class, parameter, defaultValue);
218    }
219
220    /**
221     * Set a parameter value.
222     *
223     * @param parameter  The parameter name
224     * @param scmVersion The tbranch/tag/revision
225     * @throws ScmException if the parameter already exist
226     */
227    public void setScmVersion(CommandParameter parameter, ScmVersion scmVersion) throws ScmException {
228        setObject(parameter, scmVersion);
229    }
230
231    // ----------------------------------------------------------------------
232    // File[]
233    // ----------------------------------------------------------------------
234
235    /**
236     * @param parameter not null
237     * @return an array of files
238     * @throws ScmException if any
239     */
240    public File[] getFileArray(CommandParameter parameter) throws ScmException {
241        return (File[]) getObject(File[].class, parameter);
242    }
243
244    /**
245     * @param parameter    not null
246     * @param defaultValue could be null
247     * @return an array of files
248     * @throws ScmException if any
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     * Return the value object.
277     *
278     * @param clazz     The type of the parameter value
279     * @param parameter The parameter
280     * @return The parameter value
281     * @throws ScmException if the parameter doesn't exist
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     * Return the value object or the default value if it doesn't exist.
295     *
296     * @param clazz        The type of the parameter value
297     * @param parameter    The parameter
298     * @param defaultValue The defaultValue
299     * @return The parameter value
300     * @throws ScmException if the defaultValue is in the wrong type
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     * Set the parameter value.
319     *
320     * @param parameter The parameter
321     * @param value     The parameter value
322     * @throws ScmException if the parameter already exist
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     * Removes a parameter, silent if it didn't exist.
336     *
337     * @param parameter to remove
338     */
339    public void remove(CommandParameter parameter) {
340        parameters.remove(parameter.getName());
341    }
342}