Subversion Repositories DevTools

Rev

Rev 6914 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.erggroup.buildtool.utilities;
/**
 * This class is used to create commands by building up a command line piece by piece.
 * The full command can them be extracted either as a string or as an array of strings
 */
import java.util.ArrayList;
  
public class CommandBuilder {
    
    /**
     * An array of command being accumulated
     */
    ArrayList<String> cmd = new ArrayList<String>();
    
    
    /** Re-init the command list
     *  Discards the current Array List and start again
     *  
     *  @param argList           - Variable number of string arguments
     */
    public void init(String... argList)
    {
        cmd.clear();
        add(argList);
    }
    
    /** Add one or more strings to the command
     * 
     * @param argList           - Variable number of string arguments
     */
    public void add( String... argList )
    {
        for(String c: argList)
        {
            cmd.add(c);
        }
    }
    
    /** Conditionally add one or more strings to the command
     * 
     * @param enable            - If true then add the commands
     * @param argList           - Variable number of string arguments
     */
    public void add( Boolean enable, String... argList )
    {
        if (enable)
        {
            add(argList);
        }
    }
    
    /** Convert the command fragments into a string
     *  The argument are space-separated
     */
    public String toString()
    {
        String commandString = "";
        for(String c: cmd)
        {
            commandString += c + " ";
        }
        return commandString;
    }
  
    /** Convert the command fragments into a string array
     */
    public String[] getArray()
    {
        String[] rv = cmd.toArray(new String[0]);
        return rv;
    }
}