Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
6914 dpurdie 1
package com.erggroup.buildtool.utilities;
2
/**
3
 * This class is used to create commands by building up a command line piece by piece.
4
 * The full command can them be extracted either as a string or as an array of strings
5
 */
6
import java.util.ArrayList;
7
 
8
public class CommandBuilder {
9
 
10
    /**
11
     * An array of command being accumulated
12
     */
13
    ArrayList<String> cmd = new ArrayList<String>();
14
 
15
 
16
    /** Re-init the command list
17
     *  Discards the current Array List and start again
18
     *  
19
     *  @param argList           - Variable number of string arguments
20
     */
7333 dpurdie 21
    public CommandBuilder init(String... argList)
6914 dpurdie 22
    {
23
        cmd.clear();
24
        add(argList);
7333 dpurdie 25
        return this;
6914 dpurdie 26
    }
27
 
28
    /** Add one or more strings to the command
29
     * 
30
     * @param argList           - Variable number of string arguments
31
     */
7333 dpurdie 32
    public CommandBuilder add( String... argList )
6914 dpurdie 33
    {
34
        for(String c: argList)
35
        {
36
            cmd.add(c);
37
        }
7333 dpurdie 38
        return this;
6914 dpurdie 39
    }
40
 
41
    /** Conditionally add one or more strings to the command
42
     * 
43
     * @param enable            - If true then add the commands
44
     * @param argList           - Variable number of string arguments
45
     */
7333 dpurdie 46
    public CommandBuilder add( Boolean enable, String... argList )
6914 dpurdie 47
    {
48
        if (enable)
49
        {
50
            add(argList);
51
        }
7333 dpurdie 52
        return this;
6914 dpurdie 53
    }
54
 
55
    /** Convert the command fragments into a string
56
     *  The argument are space-separated
57
     */
58
    public String toString()
59
    {
60
        String commandString = "";
61
        for(String c: cmd)
62
        {
63
            commandString += c + " ";
64
        }
65
        return commandString;
66
    }
67
 
68
    /** Convert the command fragments into a string array
69
     */
70
    public String[] getArray()
71
    {
72
        String[] rv = cmd.toArray(new String[0]);
73
        return rv;
74
    }
75
}