001/*
002 * (C) Copyright 2006-2015 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat
016 *     Vincent Dutat
017 *     Vladimir Pasquier
018 *     Julien Carsique
019 *     Florent Guillaume
020 */
021package org.nuxeo.ecm.platform.commandline.executor.api;
022
023import java.io.File;
024import java.io.Serializable;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029/**
030 * Wraps command parameters (String or File path, or a list of values that will be expanded as separate parameters).
031 */
032public class CmdParameters implements Serializable {
033
034    private static final long serialVersionUID = 1L;
035
036    private final Map<String, ParameterValue> params = new HashMap<>();
037
038    /**
039     * It is recommended to use the CmdParameters instance returned by
040     * {@link CommandLineExecutorService#getDefaultCmdParameters()} which is initialized with a few common parameters.
041     *
042     * @see CommandLineExecutorService#getDefaultCmdParameters()
043     */
044    public CmdParameters() {
045    }
046
047    public void addNamedParameter(String name, String value) {
048        params.put(name, new ParameterValue(value));
049    }
050
051    public void addNamedParameter(String name, File file) {
052        addNamedParameter(name, file.getAbsolutePath());
053    }
054
055    /**
056     * @since 7.10
057     */
058    public void addNamedParameter(String name, List<String> values) {
059        params.put(name, new ParameterValue(values));
060    }
061
062    /**
063     * @since 7.10
064     */
065    public String getParameter(String name) {
066        ParameterValue param = params.get(name);
067        return param == null ? null : param.getValue();
068    }
069
070    /**
071     * @since 7.1
072     */
073    public Map<String, ParameterValue> getParameters() {
074        return params;
075    }
076
077    /**
078     * A parameter value holding either a single String or a list of Strings.
079     *
080     * @since 7.10
081     */
082    public static class ParameterValue {
083
084        private final String value;
085
086        private final List<String> values;
087
088        public ParameterValue(String value) {
089            this.value = value;
090            values = null;
091        }
092
093        /**
094         * @since 7.10
095         */
096        public ParameterValue(List<String> values) {
097            this.values = values;
098            value = null;
099        }
100
101        public String getValue() {
102            return value;
103        }
104
105        public List<String> getValues() {
106            return values;
107        }
108
109        /**
110         * Checks whether this is multi-valued.
111         *
112         * @since 7.10
113         */
114        public boolean isMulti() {
115            return values != null;
116        }
117    }
118
119}