Class ArgumentBuilder


  • public final class ArgumentBuilder
    extends java.lang.Object
    Utility class to build an array containing method arguments, as received from a command-line invocation of a tool.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.List<java.lang.String> arguments  
      private static char DASH  
      private java.lang.Object lock  
      private static int NOT_FOUND  
    • Constructor Summary

      Constructors 
      Constructor Description
      ArgumentBuilder()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String[] build()
      Retrieves all arguments as a string array, usable by a method accepting a String[] for argument.
      private int getIndexForFlag​(java.lang.String name)  
      private boolean updateValueForNamedArgument​(java.lang.String name, java.lang.String newValue)  
      ArgumentBuilder withFlag​(boolean addFlag, java.lang.String flag)
      Adds a flag on the form -someflag to the list of arguments contained within this ArgumentBuilder.
      ArgumentBuilder withNamedArgument​(boolean addNamedArgument, java.lang.String name, java.lang.String value)
      Adds a name and an argument on the form -name value to the list of arguments contained within this ArgumentBuilder.
      ArgumentBuilder withNamedArgument​(java.lang.String name, java.lang.String value)
      Convenience form for the withNamedArgument method, where a named argument is only added if the value is non-null and non-empty after trimming.
      ArgumentBuilder withPreCompiledArguments​(java.util.List<java.lang.String> preCompiledArguments)
      Adds the supplied pre-compiled arguments in the same order as they were given.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • lock

        private final java.lang.Object lock
      • arguments

        private java.util.List<java.lang.String> arguments
    • Constructor Detail

      • ArgumentBuilder

        public ArgumentBuilder()
    • Method Detail

      • build

        public java.lang.String[] build()

        Retrieves all arguments as a string array, usable by a method accepting a String[] for argument. This would be true of public static void main(String[] args), as well as the entry points for both the XJC and the Schemagen tools.

        Returns:
        an array holding all arguments in this ArgumentBuilder.
      • withFlag

        public ArgumentBuilder withFlag​(boolean addFlag,
                                        java.lang.String flag)

        Adds a flag on the form -someflag to the list of arguments contained within this ArgumentBuilder. If the flag argument does not start with a dash ('-'), one will be prepended.

        Typical usage:

        
             argumentBuilder
              .withFlag(someBooleanParameter, "foobar")
              .withFlag(someOtherBooleanParameter, "gnat")
              .withFlag(someThirdBooleanParameter, "gnu")
              ....
         
        Parameters:
        addFlag - if true, the flag will be added to the underlying list of arguments within this ArgumentBuilder.
        flag - The flag/argument to add. The flag must be a complete word, implying it cannot contain whitespace.
        Returns:
        This ArgumentBuilder, for chaining.
      • withNamedArgument

        public ArgumentBuilder withNamedArgument​(boolean addNamedArgument,
                                                 java.lang.String name,
                                                 java.lang.String value)

        Adds a name and an argument on the form -name value to the list of arguments contained within this ArgumentBuilder. The two parts will yield 2 elements in the underlying argument list. If the name argument does not start with a dash ('-'), one will be prepended.

        Typical usage:

        
             // These values should be calculated as part of the business logic
             final boolean addFooBar = true;
             final boolean addGnat = true;
             final boolean addGnu = false;
        
             // Add all relevant arguments
             argumentBuilder
              .withNamedArgument(addFooBar, "foobar", "foobarValue")
              .withNamedArgument(addGnat, "-gnat", "gnatValue")
              .withNamedArgument(addGnu, "gnu", "gnuValue")
              ....
         
        Parameters:
        addNamedArgument - if true, the named argument (name and value) will be added to the underlying list of arguments within this ArgumentBuilder.
        name - The name of the namedArgument to add. Cannot be empty.
        value - The value of the namedArgument to add.
        Returns:
        This ArgumentBuilder, for chaining.
      • withNamedArgument

        public ArgumentBuilder withNamedArgument​(java.lang.String name,
                                                 java.lang.String value)
        Convenience form for the withNamedArgument method, where a named argument is only added if the value is non-null and non-empty after trimming.
        Parameters:
        name - The name of the namedArgument to add. Cannot be empty.
        value - The value of the namedArgument to add.
        Returns:
        This ArgumentBuilder, for chaining.
        See Also:
        withNamedArgument(boolean, String, String)
      • withPreCompiledArguments

        public ArgumentBuilder withPreCompiledArguments​(java.util.List<java.lang.String> preCompiledArguments)
        Adds the supplied pre-compiled arguments in the same order as they were given.
        Parameters:
        preCompiledArguments - A non-null List holding pre-compiled arguments.
        Returns:
        This ArgumentBuilder, for chaining.
      • getIndexForFlag

        private int getIndexForFlag​(java.lang.String name)
      • updateValueForNamedArgument

        private boolean updateValueForNamedArgument​(java.lang.String name,
                                                    java.lang.String newValue)