📄 abstractcommand.java
字号:
/* * LingPipe v. 3.5 * Copyright (C) 2003-2008 Alias-i * * This program is licensed under the Alias-i Royalty Free License * Version 1 WITHOUT ANY WARRANTY, without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Alias-i * Royalty Free License Version 1 for more details. * * You should have received a copy of the Alias-i Royalty Free License * Version 1 along with this program; if not, visit * http://alias-i.com/lingpipe/licenses/lingpipe-license-1.txt or contact * Alias-i, Inc. at 181 North 11th Street, Suite 401, Brooklyn, NY 11211, * +1 (718) 290-9170. */package com.aliasi.util;import java.io.File;import java.util.Properties;/** * A <code>CommandLineArguments</code> 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. * * <p> Arguments of the form * <code>-<i>Property</i>=<i>Value</i></code> induce the property * value pair specified. Argument values may be empty, in which * case the value will be the empty string. * <p>Arguments of the form <code>-<i>Flag</i></code> not matching the * previous form induce properties <code><i>Flag</i>=yes</code>. Bare * arguments with of the form <code><i>BareArgument</i></code> induce * properties <code>BARE_ARG_<i>i</i>=<i>BareArgument</i></code> so * that bare arguments may be retrieved by position; bare arguments * equal to the empty string are ignored.</p> * * <P>Because commands are based on the {@link #run()} method, they * have been defined to implement the {@link Runnable} interface. * * @author Bob Carpenter * @version 3.6 * @since LingPipe1.0 */public abstract class AbstractCommand implements Runnable { private final long mStartTime; private final Properties mProperties; /** * Counter for bare arguments. */ private int mBareArgCount = 0; /** * The default properties values for this command. */ private Properties mDefaultProperties = null; /** * Constructs a command-line arguments object from the specified * arguments list using no default values. * * @param args Command-line arguments. * @throws IllegalArgumentException If the arguments are not well * formed. */ public AbstractCommand(String[] args) { this(args,new Properties()); } /** * Constructs a command-line arguments object from the specified * arguments using the specified properties as default values. * * @param args Command-line arguments. * @param defaultProperties Default values for properties. * @throws IllegalArgumentException If the arguments are not well * formed. */ public AbstractCommand(String[] args, Properties defaultProperties) { mDefaultProperties = defaultProperties; mProperties = new Properties(defaultProperties); parse(args); mStartTime = System.currentTimeMillis(); } /** * Adds the specified key and value as default arguments. * * @param property Name of property. * @param defaultValue Default value for property. */ public void addDefaultProperty(String property, String defaultValue) { mDefaultProperties.setProperty(property,defaultValue); } /** * Return the elapsed time since the construction of * this command in milliseconds. * * @return The elapsed time since the construction of * this command in milliseconds. */ public long elapsedTimeMillis() { return System.currentTimeMillis() - mStartTime; } /** * Return the time at which this command was constructed. * * @return The time at which this command was constructed. */ public long startTimeMillis() { return mStartTime; } /** * Runs the abstract command. */ public abstract void run(); /** * Returns the number of bare arguments supplied on the command * line. * * @return Number of bare arguments supplied on the command line. */ public int numBareArguments() { return mBareArgCount; } /** * 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 (<code>-</code>) indicating a flag or * property. * * @return The array of bare arguments. */ public String[] bareArguments() { String[] arguments = new String[numBareArguments()]; for (int i = 0; i < arguments.length; ++i) arguments[i] = getBareArgument(i); return arguments; } /** * Returns <code>true</code> if the arguments set the specified * flag. Note that this method returns <code>false</code> for * properties; see {@link #hasProperty(String)}. * * @param arg The argument to test for existence on the command line. * @return <code>true</code> if the argument was specified on the * command line preceded by a <code>'-'</code>. */ public boolean hasFlag(String arg) { String value = mProperties.getProperty(arg); return value != null && value.equals(HAS_PROPERTY_VALUE); } /** * Returns <code>true</code> if the arguments have the * specified property defined. Note that this method * returns false for flags; see {@link #hasFlag(String)}. */ public boolean hasProperty(String arg) { String value = mProperties.getProperty(arg); return value != null && !value.equals(HAS_PROPERTY_VALUE); } /** * Returns the specified bare argument. * * @param n Index of bare argument to return. * @return Value of bare argument in specified position, or * <code>null</code> if not specified. */ public String getBareArgument(int n) { return mProperties.getProperty(bareArgumentProperty(n)); } /** * Returns the value of the command-line argument with the * specified key, or the value of the key in the default * properties, or <code>null</code> if it does not exist. The * property exists in the command-line if it was specified by * <code>"-<i>Arg</i>=<i>Value</i></code>. * * @param key Name of command-line argument to return. * @return Value of command-line argument. */ public String getArgument(String key) { return mProperties.getProperty(key); } /** * 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. * * <p>Flags are included as properties with the flag as key and * value {@link #HAS_PROPERTY_VALUE}. Bare arguments are included * with prefixes consisting of {@link #BARE_ARG_PREFIX} followed * by a number. * * @return The properties representing the arguments. */ public Properties getArguments() { return mProperties; } /** * Returns the same value as {@link #getArgument(String)}, but * throws an exception if the argument does not exist. * * @param key Name of command-line argument to return. * @return Value of command-line argument. * @throws IllegalArgumentException If there is no value specified * for the given key. */ public String getExistingArgument(String key) throws IllegalArgumentException{ String result = mProperties.getProperty(key); if (result == null) illegalPropertyArgument("Require value.", key); return result; } /** * Return the array of string values specified by the * value of the specified key as a comma-separated values list, * as defined in {@link Arrays#csvToArray(String)}. * * @param key Name of command-line argument to parse as * comma-separated values. * @return Array of values specified by key's value. * @throws IllegalArgumentException If the specified key is not * defined. */ public String[] getCSV(String key) { String csv = getExistingArgument(key); return Arrays.csvToArray(csv); } /** * Returns <code>true</code> if there is a command-line * argument specified for the key. * * @return <code>true</code> if there is a command-line argument * specified for the key. */ public boolean hasArgument(String key) { return mProperties.containsKey(key); } /** * Returns the value of the argument with the specified key * converted to an integer value. * * @param key Name of argument to convert to an integer and * return. * @return 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. */ public int getArgumentInt(String key) { String valString = getExistingArgument(key); try { return Integer.parseInt(valString); } catch (NumberFormatException e) { illegalPropertyArgument("Required integer.", key); // required for compiler; doen't know catch always throws return -1; } } /** * Returns the value of the argument with the specified key * converted to a long value. * * @param key Name of argument to convert to a long and * return. * @return 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. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -