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.valueOf(getString(parameter, Boolean.toString(defaultValue)))
191                .booleanValue();
192    }
193
194    // ----------------------------------------------------------------------
195    // ScmVersion
196    // ----------------------------------------------------------------------
197
198    /**
199     * Return the parameter value as ScmVersion.
200     *
201     * @param parameter The parameter
202     * @return The parameter value as a ScmVersion
203     * @throws ScmException if the parameter doesn't exist
204     */
205    public ScmVersion getScmVersion(CommandParameter parameter) throws ScmException {
206        return (ScmVersion) getObject(ScmVersion.class, parameter);
207    }
208
209    /**
210     * Return the parameter value as ScmVersion or the default value.
211     *
212     * @param parameter    The parameter
213     * @param defaultValue The default value
214     * @return The parameter value as a ScmVersion
215     * @throws ScmException if the parameter doesn't exist
216     */
217    public ScmVersion getScmVersion(CommandParameter parameter, ScmVersion defaultValue) throws ScmException {
218        return (ScmVersion) getObject(ScmVersion.class, parameter, defaultValue);
219    }
220
221    /**
222     * Set a parameter value.
223     *
224     * @param parameter  The parameter name
225     * @param scmVersion The tbranch/tag/revision
226     * @throws ScmException if the parameter already exist
227     */
228    public void setScmVersion(CommandParameter parameter, ScmVersion scmVersion) throws ScmException {
229        setObject(parameter, scmVersion);
230    }
231
232    // ----------------------------------------------------------------------
233    // File[]
234    // ----------------------------------------------------------------------
235
236    /**
237     * @param parameter not null
238     * @return an array of files
239     * @throws ScmException if any
240     */
241    public File[] getFileArray(CommandParameter parameter) throws ScmException {
242        return (File[]) getObject(File[].class, parameter);
243    }
244
245    /**
246     * @param parameter    not null
247     * @param defaultValue could be null
248     * @return an array of files
249     * @throws ScmException if any
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     * Return the value object.
278     *
279     * @param clazz     The type of the parameter value
280     * @param parameter The parameter
281     * @return The parameter value
282     * @throws ScmException if the parameter doesn't exist
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     * Return the value object or the default value if it doesn't exist.
296     *
297     * @param clazz        The type of the parameter value
298     * @param parameter    The parameter
299     * @param defaultValue The defaultValue
300     * @return The parameter value
301     * @throws ScmException if the defaultValue is in the wrong type
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     * Set the parameter value.
320     *
321     * @param parameter The parameter
322     * @param value     The parameter value
323     * @throws ScmException if the parameter already exist
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     * Removes a parameter, silent if it didn't exist.
337     *
338     * @param parameter to remove
339     */
340    public void remove(CommandParameter parameter) {
341        parameters.remove(parameter.getName());
342    }
343}