001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 *     Vincent Dutat
019 *     Vladimir Pasquier
020 *     Julien Carsique
021 *     Florent Guillaume
022 */
023package org.nuxeo.ecm.platform.commandline.executor.api;
024
025import java.io.File;
026import java.io.Serializable;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030
031/**
032 * Wraps command parameters (String or File path, or a list of values that will be expanded as separate parameters).
033 */
034public class CmdParameters implements Serializable {
035
036    private static final long serialVersionUID = 1L;
037
038    private final Map<String, ParameterValue> params = new HashMap<>();
039
040    /**
041     * It is recommended to use the CmdParameters instance returned by
042     * {@link CommandLineExecutorService#getDefaultCmdParameters()} which is initialized with a few common parameters.
043     *
044     * @see CommandLineExecutorService#getDefaultCmdParameters()
045     */
046    public CmdParameters() {
047    }
048
049    public void addNamedParameter(String name, String value) {
050        params.put(name, new ParameterValue(value));
051    }
052
053    public void addNamedParameter(String name, File file) {
054        addNamedParameter(name, file.getAbsolutePath());
055    }
056
057    /**
058     * @since 7.10
059     */
060    public void addNamedParameter(String name, List<String> values) {
061        params.put(name, new ParameterValue(values));
062    }
063
064    /**
065     * @since 7.10
066     */
067    public String getParameter(String name) {
068        ParameterValue param = params.get(name);
069        return param == null ? null : param.getValue();
070    }
071
072    /**
073     * @since 7.1
074     */
075    public Map<String, ParameterValue> getParameters() {
076        return params;
077    }
078
079    /**
080     * A parameter value holding either a single String or a list of Strings.
081     *
082     * @since 7.10
083     */
084    public static class ParameterValue {
085
086        private final String value;
087
088        private final List<String> values;
089
090        public ParameterValue(String value) {
091            this.value = value;
092            values = null;
093        }
094
095        /**
096         * @since 7.10
097         */
098        public ParameterValue(List<String> values) {
099            this.values = values;
100            value = null;
101        }
102
103        public String getValue() {
104            return value;
105        }
106
107        public List<String> getValues() {
108            return values;
109        }
110
111        /**
112         * Checks whether this is multi-valued.
113         *
114         * @since 7.10
115         */
116        public boolean isMulti() {
117            return values != null;
118        }
119    }
120
121}