package com.minres.rdl.generator; /** * This class holds the information for a set of options. A set can hold any number of * OptionData instances which are checked together to determine success or failure. *

* The approach to use this class looks like this: *

*

    *
  1. The user uses any of the Options.addSet() (e. g. {@link Options#addSet(String)}) to create * any number of sets required (or just relies on the default set, if only one set is required) *
  2. The user adds all required option definitions to each set *
  3. Using any of the Options.check() methods, each set can be checked whether the options * that were specified on the command line satisfy its requirements *
  4. If the check was successful for a given set, several data items are available from this class: * *
*/ public class OptionSet { private final static String CLASS = "OptionSet"; private java.util.ArrayList options = new java.util.ArrayList(); private java.util.HashMap keys = new java.util.HashMap(); private java.util.ArrayList unmatched = new java.util.ArrayList(); private java.util.ArrayList data = new java.util.ArrayList(); private String setName = null; private int minData = 0; private int maxData = 0; private Options.Prefix prefix = null; private Options.Multiplicity defaultMultiplicity = null; /** * Constructor */ OptionSet(Options.Prefix prefix, Options.Multiplicity defaultMultiplicity, String setName, int minData, int maxData) { if (setName == null) throw new IllegalArgumentException(CLASS + ": setName may not be null"); if (minData < 0) throw new IllegalArgumentException(CLASS + ": minData must be >= 0"); if (maxData < minData) throw new IllegalArgumentException(CLASS + ": maxData must be >= minData"); this.prefix = prefix; this.defaultMultiplicity = defaultMultiplicity; this.setName = setName; this.minData = minData; this.maxData = maxData; } /** * Get a list of all the options defined for this set *

* @return A list of {@link OptionData} instances defined for this set */ public java.util.ArrayList getOptionData() { return options; } /** * Get the data for a specific option, identified by its key name (which is unique) *

* @param key The key for the option *

* @return The {@link OptionData} instance *

* @throws IllegalArgumentException If the key is null or unknown in this set */ public OptionData getOption(String key) { if (key == null) throw new IllegalArgumentException(CLASS + ": key may not be null"); if (!keys.containsKey(key)) throw new IllegalArgumentException(CLASS + ": unknown key: " + key); return keys.get(key); } /** * Check whether a specific option is set, i. e. whether it was specified at least once on the command line. *

* @param key The key for the option *

* @return true or false, depending on the outcome of the check *

* @throws IllegalArgumentException If the key is null or unknown in this set */ public boolean isSet(String key) { if (key == null) throw new IllegalArgumentException(CLASS + ": key may not be null"); if (!keys.containsKey(key)) throw new IllegalArgumentException(CLASS + ": unknown key: " + key); return keys.get(key).getResultCount() > 0 ? true : false; } /** * Getter method for setName property *

* @return The value for the setName property */ public String getSetName() { return setName; } /** * Getter method for minData property *

* @return The value for the minData property */ public int getMinData() { return minData; } /** * Getter method for maxData property *

* @return The value for the maxData property */ public int getMaxData() { return maxData; } /** * Return the data items found (these are the items on the command line which do not start with the prefix, i. e. non-option arguments) *

* @return A list of strings with all data items found */ public java.util.ArrayList getData() { return data; } /** * Return all unmatched items found (these are the items on the command line which start with the prefix, but do not * match to one of the options) *

* @return A list of strings with all unmatched items found */ public java.util.ArrayList getUnmatched() { return unmatched; } /** * Add a non-value option with the given key, and the default prefix and multiplicity *

* @param key The key for the option *

* @return The set instance itself (to support invocation chaining for addOption() methods) *

* @throws IllegalArgumentException If the key is null or a key with this name has already been defined */ public OptionSet addOption(String key) { return addOption(key, defaultMultiplicity); } /** * Add a non-value option with the given key and multiplicity, and the default prefix *

* @param key The key for the option * @param multiplicity The multiplicity for the option *

* @return The set instance itself (to support invocation chaining for addOption() methods) *

* @throws IllegalArgumentException If the key is null or a key with this name has already been defined * or if multiplicity is null */ public OptionSet addOption(String key, Options.Multiplicity multiplicity) { return addOption(key, false, Options.Separator.NONE, false, multiplicity); } /** * Add a value option with the given key and separator, no details, and the default prefix and multiplicity *

* @param key The key for the option * @param separator The separator for the option *

* @return The set instance itself (to support invocation chaining for addOption() methods) *

* @throws IllegalArgumentException If the key is null or a key with this name has already been defined * or if separator is null */ public OptionSet addOption(String key, Options.Separator separator) { return addOption(key, false, separator, true, defaultMultiplicity); } /** * Add a value option with the given key, separator, and multiplicity, no details, and the default prefix *

* @param key The key for the option * @param separator The separator for the option * @param multiplicity The multiplicity for the option *

* @return The set instance itself (to support invocation chaining for addOption() methods) *

* @throws IllegalArgumentException If the key is null or a key with this name has already been defined * or if separator or multiplicity are null */ public OptionSet addOption(String key, Options.Separator separator, Options.Multiplicity multiplicity) { return addOption(key, false, separator, true, multiplicity); } /** * * Add a value option with the given key and separator, possibly details, and the default prefix and multiplicity *

* @param key The key for the option * @param details A boolean indicating whether details are expected for the option * @param separator The separator for the option *

* @return The set instance itself (to support invocation chaining for addOption() methods) *

* @throws IllegalArgumentException If the key is null or a key with this name has already been defined * or if separator is null */ public OptionSet addOption(String key, boolean details, Options.Separator separator) { return addOption(key, details, separator, true, defaultMultiplicity); } /** * Add a value option with the given key, separator, and multiplicity, possibly details, and the default prefix *

* @param key The key for the option * @param details A boolean indicating whether details are expected for the option * @param separator The separator for the option * @param multiplicity The multiplicity for the option *

* @return The set instance itself (to support invocation chaining for addOption() methods) *

* @throws IllegalArgumentException If the key is null or a key with this name has already been defined * or if separator or multiplicity are null */ public OptionSet addOption(String key, boolean details, Options.Separator separator, Options.Multiplicity multiplicity) { return addOption(key, details, separator, true, multiplicity); } /** * The master method to add an option. Since there are combinations which are not * acceptable (like a NONE separator and a true value), this method is not public. * Internally, we only supply acceptable combinations. */ OptionSet addOption(String key, boolean details, Options.Separator separator, boolean value, Options.Multiplicity multiplicity) { if (key == null) throw new IllegalArgumentException(CLASS + ": key may not be null"); if (multiplicity == null) throw new IllegalArgumentException(CLASS + ": multiplicity may not be null"); if (separator == null) throw new IllegalArgumentException(CLASS + ": separator may not be null"); if (keys.containsKey(key)) throw new IllegalArgumentException(CLASS + ": the key " + key + " has already been defined for this OptionSet"); OptionData od = new OptionData(prefix, key, details, separator, value, multiplicity); options.add(od); keys.put(key, od); return this; } }