com.aliasi.util
Class AbstractCommand

java.lang.Object
  extended by com.aliasi.util.AbstractCommand
All Implemented Interfaces:
Runnable

public abstract class AbstractCommand
extends Object
implements Runnable

A CommandLineArguments object represents a the command-line arguments. It is constructed with an array of command-line arguments and an optional properties value specifying default values.

Arguments of the form -Property=Value induce the property value pair specified. Argument values may be empty, in which case the value will be the empty string.

Arguments of the form -Flag not matching the previous form induce properties Flag=yes. Bare arguments with of the form BareArgument induce properties BARE_ARG_i=BareArgument so that bare arguments may be retrieved by position; bare arguments equal to the empty string are ignored.

Because commands are based on the run() method, they have been defined to implement the Runnable interface.

Since:
LingPipe1.0
Version:
3.6
Author:
Bob Carpenter

Field Summary
static String BARE_ARG_PREFIX
          The string prefixed before a number to indicate bare arguments.
static String HAS_PROPERTY_VALUE
          The value assigned to arguments beginning with '-' and not containing an '='.
 
Constructor Summary
AbstractCommand(String[] args)
          Constructs a command-line arguments object from the specified arguments list using no default values.
AbstractCommand(String[] args, Properties defaultProperties)
          Constructs a command-line arguments object from the specified arguments using the specified properties as default values.
 
Method Summary
 void addDefaultProperty(String property, String defaultValue)
          Adds the specified key and value as default arguments.
 String[] bareArguments()
          Returns the array of bare arguments, in order, supplied on the command line.
 void checkParameterImplication(String ifParam, String thenParam)
          Throws an illegal argument exception if the first parameter is defined in the command the second parameter is not.
 long elapsedTimeMillis()
          Return the elapsed time since the construction of this command in milliseconds.
 String getArgument(String key)
          Returns the value of the command-line argument with the specified key, or the value of the key in the default properties, or null if it does not exist.
protected  File getArgumentCreatableFile(String fileParam)
          Returns the model file specified by the command line after guaranteeing that the file can be created.
 File getArgumentDirectory(String key)
          Returns the value of the argument with the specified key converted to a directory.
 double getArgumentDouble(String key)
          Returns the value of the argument with the specified key converted to a double value.
 File getArgumentExistingNormalFile(String key)
          Returns the existing normal file named by the value of the specified key.
 File getArgumentFile(String key)
          Returns the value of the argument with the specified key converted to a file.
 int getArgumentInt(String key)
          Returns the value of the argument with the specified key converted to an integer value.
 long getArgumentLong(String key)
          Returns the value of the argument with the specified key converted to a long value.
 Properties getArguments()
          Returns the complete list of command-line parameters for this abstract command.
 String getBareArgument(int n)
          Returns the specified bare argument.
 String[] getCSV(String key)
          Deprecated. Use String.split(String) instead.
 String getExistingArgument(String key)
          Returns the same value as getArgument(String), but throws an exception if the argument does not exist.
 File getOrCreateArgumentDirectory(String key)
          Returns the value of the argument with the specified key converted to a directory.
 boolean hasArgument(String key)
          Returns true if there is a command-line argument specified for the key.
 boolean hasFlag(String arg)
          Returns true if the arguments set the specified flag.
 boolean hasProperty(String arg)
          Returns true if the arguments have the specified property defined.
protected  void illegalArgument(String msg)
          Throw an illegal argument exception with the specified message.
protected  void illegalArgument(String msg, Exception e)
          Throw an illegal argument exception with the specified message with a source provided by the specified exception.
protected  void illegalArgument(String msg, String arg)
          Throw an illegal argument exception with the specified message and a report of the argument found.
protected  void illegalPropertyArgument(String msg, String key)
          Throw an illegal argument message with the specified message and a report of the specified key and its value.
 int numBareArguments()
          Returns the number of bare arguments supplied on the command line.
abstract  void run()
          Runs the abstract command.
 long startTimeMillis()
          Return the time at which this command was constructed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HAS_PROPERTY_VALUE

public static final String HAS_PROPERTY_VALUE
The value assigned to arguments beginning with '-' and not containing an '='.

See Also:
Constant Field Values

BARE_ARG_PREFIX

public static final String BARE_ARG_PREFIX
The string prefixed before a number to indicate bare arguments. For instance, the fifth bare argument will be the value of the property BARE_ARG_PREFIX + 5.

See Also:
Constant Field Values
Constructor Detail

AbstractCommand

public AbstractCommand(String[] args)
Constructs a command-line arguments object from the specified arguments list using no default values.

Parameters:
args - Command-line arguments.
Throws:
IllegalArgumentException - If the arguments are not well formed.

AbstractCommand

public AbstractCommand(String[] args,
                       Properties defaultProperties)
Constructs a command-line arguments object from the specified arguments using the specified properties as default values.

Parameters:
args - Command-line arguments.
defaultProperties - Default values for properties.
Throws:
IllegalArgumentException - If the arguments are not well formed.
Method Detail

addDefaultProperty

public void addDefaultProperty(String property,
                               String defaultValue)
Adds the specified key and value as default arguments.

Parameters:
property - Name of property.
defaultValue - Default value for property.

elapsedTimeMillis

public long elapsedTimeMillis()
Return the elapsed time since the construction of this command in milliseconds.

Returns:
The elapsed time since the construction of this command in milliseconds.

startTimeMillis

public long startTimeMillis()
Return the time at which this command was constructed.

Returns:
The time at which this command was constructed.

run

public abstract void run()
Runs the abstract command.

Specified by:
run in interface Runnable

numBareArguments

public int numBareArguments()
Returns the number of bare arguments supplied on the command line.

Returns:
Number of bare arguments supplied on the command line.

bareArguments

public String[] bareArguments()
Returns the array of bare arguments, in order, supplied on the command line. Bare arguments are arguments that do not begin with a minus sign (-) indicating a flag or property.

Returns:
The array of bare arguments.

hasFlag

public boolean hasFlag(String arg)
Returns true if the arguments set the specified flag. Note that this method returns false for properties; see hasProperty(String).

Parameters:
arg - The argument to test for existence on the command line.
Returns:
true if the argument was specified on the command line preceded by a '-'.

hasProperty

public boolean hasProperty(String arg)
Returns true if the arguments have the specified property defined. Note that this method returns false for flags; see hasFlag(String).


getBareArgument

public String getBareArgument(int n)
Returns the specified bare argument.

Parameters:
n - Index of bare argument to return.
Returns:
Value of bare argument in specified position, or null if not specified.

getArgument

public String getArgument(String key)
Returns the value of the command-line argument with the specified key, or the value of the key in the default properties, or null if it does not exist. The property exists in the command-line if it was specified by "-Arg=Value.

Parameters:
key - Name of command-line argument to return.
Returns:
Value of command-line argument.

getArguments

public Properties getArguments()
Returns the complete list of command-line parameters for this abstract command. The returned result encapsulates all of the argument parameters and default parameters with values.

Flags are included as properties with the flag as key and value HAS_PROPERTY_VALUE. Bare arguments are included with prefixes consisting of BARE_ARG_PREFIX followed by a number.

Returns:
The properties representing the arguments.

getExistingArgument

public String getExistingArgument(String key)
                           throws IllegalArgumentException
Returns the same value as getArgument(String), but throws an exception if the argument does not exist.

Parameters:
key - Name of command-line argument to return.
Returns:
Value of command-line argument.
Throws:
IllegalArgumentException - If there is no value specified for the given key.

getCSV

@Deprecated
public String[] getCSV(String key)
Deprecated. Use String.split(String) instead.

Return the array of string values specified by the value of the specified key as a comma-separated values list, as defined in Arrays.csvToArray(String).

Parameters:
key - Name of command-line argument to parse as comma-separated values.
Returns:
Array of values specified by key's value.
Throws:
IllegalArgumentException - If the specified key is not defined.

hasArgument

public boolean hasArgument(String key)
Returns true if there is a command-line argument specified for the key.

Returns:
true if there is a command-line argument specified for the key.

getArgumentInt

public int getArgumentInt(String key)
Returns the value of the argument with the specified key converted to an integer value.

Parameters:
key - Name of argument to convert to an integer and return.
Returns:
Integer value of specified command-line argument.
Throws:
IllegalArgumentException - If there is a number format exception converting the argument to an integer, or if there is no value supplied.

getArgumentLong

public long getArgumentLong(String key)
Returns the value of the argument with the specified key converted to a long value.

Parameters:
key - Name of argument to convert to a long and return.
Returns:
Long value of specified command-line argument.
Throws:
IllegalArgumentException - If there is a number format exception converting the argument to an integer, or if there is no value supplied.

getArgumentDouble

public double getArgumentDouble(String key)
Returns the value of the argument with the specified key converted to a double value.

Parameters:
key - Name of argument to convert to a double and return.
Returns:
Double value of command-line argument.
Throws:
IllegalArgumentException - If there is a number format exception converting the argument to a double, or if there is no value supplied.

getArgumentFile

public File getArgumentFile(String key)
Returns the value of the argument with the specified key converted to a file. The argument must be specified.

Parameters:
key - Name of argument to convert to a file and return.
Returns:
File specified by value of argument specified by key.
Throws:
IllegalArgumentException - If the argument is not specified.

getArgumentExistingNormalFile

public File getArgumentExistingNormalFile(String key)
Returns the existing normal file named by the value of the specified key. The argument must be specified.

Parameters:
key - Name of argument to convert to file, check for existence, and return.
Returns:
File named by the value of the specified key.
Throws:
IllegalArgumentException - If the argument has no value or is not an existing file.

getArgumentDirectory

public File getArgumentDirectory(String key)
Returns the value of the argument with the specified key converted to a directory. The directory must exist and be a directory.

Parameters:
key - Name of argument to convert to a directory and return.
Returns:
Directory specified by value of argument specified by key.
Throws:
IllegalArgumentException - If the argument is not specified, or is not an existing directory.

getOrCreateArgumentDirectory

public File getOrCreateArgumentDirectory(String key)
Returns the value of the argument with the specified key converted to a directory. The directory must either exist or be creatable. The return value will be an existing directory.

Parameters:
key - Name of argument to convert to a directory and return.
Returns:
Directory specified by value of argument specified by key.
Throws:
IllegalArgumentException - If the argument is not specified, and is not an existing or creatable directory.

getArgumentCreatableFile

protected File getArgumentCreatableFile(String fileParam)
Returns the model file specified by the command line after guaranteeing that the file can be created.

Returns:
Normal file containing the model.
Throws:
IllegalArgumentException - If the model is not specified on the command line or is not a file that can be created.

illegalPropertyArgument

protected void illegalPropertyArgument(String msg,
                                       String key)
Throw an illegal argument message with the specified message and a report of the specified key and its value.

Parameters:
msg - Message to display.
key - Key required or found on the command line.
Throws:
IllegalArgumentException - Always, with specified message and a report of the found argument.

illegalArgument

protected void illegalArgument(String msg,
                               String arg)
Throw an illegal argument exception with the specified message and a report of the argument found.

Parameters:
msg - Message to display.
arg - The argument found on the command line.
Throws:
IllegalArgumentException - Always, with specified message and a report of the found argument.

illegalArgument

protected void illegalArgument(String msg,
                               Exception e)
Throw an illegal argument exception with the specified message with a source provided by the specified exception.

Parameters:
msg - Message to display.
e - Exception causing the illegal argument exception.
Throws:
IllegalArgumentException - Always, with specified message and a report of embedded exception.

illegalArgument

protected void illegalArgument(String msg)
Throw an illegal argument exception with the specified message.

Parameters:
msg - Message to include in the exception.
Throws:
IllegalArgumentException - Always, with the specified message.

checkParameterImplication

public void checkParameterImplication(String ifParam,
                                      String thenParam)
Throws an illegal argument exception if the first parameter is defined in the command the second parameter is not. This is used to check implicational constraints between parameters.

Parameters:
ifParam - Parameter to test for definedness.
thenParam - Parameter to test for definedness.
Throws:
IllegalArgumentException - If the first parameter is defined and the second is not.