Package adams.core.option
Class OptionManager
- java.lang.Object
-
- adams.core.option.OptionManager
-
- All Implemented Interfaces:
CleanUpHandler,Serializable
public class OptionManager extends Object implements Serializable, CleanUpHandler
Class for managing option definitions.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static StringFILENAMEprotected static Set<String>m_BlacklistedClassesthe blacklisted classes.protected Map<String,Integer>m_CommandlineIndexthe commandline flag <-> index relationship.protected static Variablesm_DefaultVariablesInstancedefault instance (gets replaced at runtime).protected Set<String>m_NoVariablesPropertiesthe properties that cannot have variables attached.protected List<AbstractOption>m_Optionsthe options.protected OptionHandlerm_Ownerthe owner.protected static Propertiesm_Propertiesthe properties in use.protected Map<String,Integer>m_PropertyIndexthe (bean) property <-> index relationship.protected booleanm_Quietwhether to suppress error messages.protected Set<String>m_RemovedFlagsthe removed flags.protected Set<String>m_RemovedPropertiesthe removed properties.protected booleanm_ThrowExceptionswhether to throw exceptions or just ignore errors.protected Variablesm_Variablesthe Variables instance to use for resolving variables.
-
Constructor Summary
Constructors Constructor Description OptionManager(OptionHandler owner)Initializes the manager.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AbstractOptionadd(String commandline, String property, Object defValue)Determines the appropriate concrete option class, sets it up and returns it.AbstractOptionadd(String commandline, String property, Object defValue, Number lower, Number upper)Determines the appropriate concrete option class, sets it up and returns it.booleanallowsVariables(String property)Checks whether this property allows variables.voidcleanUp()Cleans up the options vector.voidderegisterVariables()Deregisters all the variables recursively.voiddisableVariables(String property)Disables variable support for this bean property.voidenabledVariables(String property)Enables variable support for this bean property again.StringensureVariableForPropertyExists(String property)If a variable is attached to the variable, it checks whether the variable really exists in the context.AbstractOptionfindByClass(Class cls)Tries to locate the corresponding option for the given class.AbstractOptionfindByFlag(String flag)Tries to locate the corresponding option for the given commandline string (without the leading dash).AbstractOptionfindByProperty(String property)Tries to locate the corresponding option for the given property name.protected AbstractOptionfindOption(String flagOrProperty, boolean flag)Returns the option for the commandline flag/property.AbstractOptionConstraintgetConstraintForProperty(String property)Tries to locate the option for the given property name and then return any constraint.<T extends AbstractOptionConstraint>
TgetConstraintForProperty(String property, Class<T> type)Tries to locate the option for the given property name and then return any constraint.List<AbstractOption>getOptionsList()Returns the list of options.OptionHandlergetOwner()Returns the owning OptionHandler.protected static PropertiesgetProperties()Returns the properties file with the custom editors.booleangetThrowExceptions()Returns whether to throw exceptions on errors or to ignore them.StringgetVariableForProperty(String property)Returns the variable that is attached to a property.VariablesgetVariables()Returns the variables instance in use.protected voidhandleError(Throwable t)Handles the given throwable/exception.booleanhasConstraintForProperty(String property)Tries to locate the option for the given property name and then checks for any constraint.booleanhasVariableForProperty(String property)Returns whether there is a variable attached to the property.AbstractOptioninsert(int index, String commandline, String property, Object defValue)Determines the appropriate concrete option class, sets it up and returns it.AbstractOptioninsert(int index, String commandline, String property, Object defValue, Number lower, Number upper)Determines the appropriate concrete option class, sets it up and returns it.protected static booleanisBlacklistedClass(Class cls)Checks whether the class is blacklisted.protected static booleanisBlacklistedClass(Object obj)Checks whether the object's class is blacklisted.protected static booleanisBlacklistedClass(String cls)Checks whether the class is blacklisted.booleanisDefaultValueByFlag(String flag)Checks whether the current value associated with the flag is the default value.booleanisDefaultValueByProperty(String property)Checks whether the current value associated with the property is the default value.booleanisQuiet()Returns whether to suppress error messages.booleanisRemovedFlag(String flag)Checks whether the flag has been removed intentionally.booleanisRemovedProperty(String property)Checks whether the property has been removed intentionally.booleanisValid(String property, Object value)Checks whether the value is valid, in case there is a constraint in place for the property.voidregisterVariables()Registers all the variables recursively.AbstractOptionremoveByFlag(String flag)Removes the corresponding option associated with the given commandline string (without the leading dash).AbstractOptionremoveByProperty(String property)Removes the corresponding option associated with the given property name.protected AbstractOptionremoveOption(String flagOrProperty, boolean isFlag)Removes the option associated with the commandline flag/property.booleansetConstraintForProperty(String property, AbstractOptionConstraint constraint)Tries to locate the option for the given property name and then set the constraint.voidsetDefaults()Sets the default values.voidsetQuiet(boolean value)Sets whether to suppress error messages.voidsetThrowExceptions(boolean value)Sets whether to throw exceptions or simply ignore errors (recursively).AbstractArgumentOptionsetVariableForProperty(String property, String variable)Sets the variable in the option handler for the given property.voidsetVariables(Variables value)Sets the Variables instance to use (not recursively!).intsize()Returns the number of managed options.voidtraverse(OptionTraverser traverser)Traverses all the options and lets the various options get handled by the supplied object.voidtraverse(OptionTraverser traverser, boolean nonAdams)Traverses all the options and lets the various options get handled by the supplied object.protected voidtraverse(OptionTraverser traverser, OptionTraversalPath path, boolean nonAdams)Traverses all the options and lets the various options get handled by the supplied object.protected voidtraverse(OptionTraverser traverser, OptionTraversalPath path, Object obj)Traverses a non-ADAMS OptionHandler.voidupdateVariablePrefix(String prefix, boolean add)Adds or removes the specified prefix from all variables.voidupdateVariablesInstance(Variables variables)Updates the Variables instance recursively on all options.StringupdateVariableValues()Updates the variables, i.e., in case an option uses a variable and this variable's value has changed, the updated value will be set.StringupdateVariableValues(boolean forceUpdate)Updates the variables, i.e., in case an option uses a variable and this variable's value has changed (or update is enforced), the updated value will be set.StringupdateVariableValues(boolean forceUpdate, Logger log)Updates the variables, i.e., in case an option uses a variable and this variable's value has changed (or update is enforced), the updated value will be set.
-
-
-
Field Detail
-
FILENAME
public static final String FILENAME
- See Also:
- Constant Field Values
-
m_Properties
protected static Properties m_Properties
the properties in use.
-
m_DefaultVariablesInstance
protected static Variables m_DefaultVariablesInstance
default instance (gets replaced at runtime).
-
m_Owner
protected OptionHandler m_Owner
the owner.
-
m_Options
protected List<AbstractOption> m_Options
the options.
-
m_CommandlineIndex
protected Map<String,Integer> m_CommandlineIndex
the commandline flag <-> index relationship.
-
m_PropertyIndex
protected Map<String,Integer> m_PropertyIndex
the (bean) property <-> index relationship.
-
m_NoVariablesProperties
protected Set<String> m_NoVariablesProperties
the properties that cannot have variables attached.
-
m_ThrowExceptions
protected boolean m_ThrowExceptions
whether to throw exceptions or just ignore errors.
-
m_Variables
protected transient Variables m_Variables
the Variables instance to use for resolving variables.
-
m_Quiet
protected boolean m_Quiet
whether to suppress error messages.
-
-
Constructor Detail
-
OptionManager
public OptionManager(OptionHandler owner)
Initializes the manager.- Parameters:
owner- the owner of this manager
-
-
Method Detail
-
getOwner
public OptionHandler getOwner()
Returns the owning OptionHandler.- Returns:
- the owner
-
setVariables
public void setVariables(Variables value)
Sets the Variables instance to use (not recursively!).- Parameters:
value- the instance to use- See Also:
updateVariablesInstance(Variables)
-
getVariables
public Variables getVariables()
Returns the variables instance in use.- Returns:
- the instance in use
-
setThrowExceptions
public void setThrowExceptions(boolean value)
Sets whether to throw exceptions or simply ignore errors (recursively).- Parameters:
value- if true then exceptions are thrown on errors
-
getThrowExceptions
public boolean getThrowExceptions()
Returns whether to throw exceptions on errors or to ignore them.- Returns:
- true if exceptions are thrown in case of errors
-
setQuiet
public void setQuiet(boolean value)
Sets whether to suppress error messages.- Parameters:
value- true if to suppress error messages
-
isQuiet
public boolean isQuiet()
Returns whether to suppress error messages.- Returns:
- true if to suppress error messages
-
handleError
protected void handleError(Throwable t)
Handles the given throwable/exception. Either throws the exception or- Parameters:
t- the error to throw/print- See Also:
getThrowExceptions()
-
add
public AbstractOption add(String commandline, String property, Object defValue)
Determines the appropriate concrete option class, sets it up and returns it. This call will always output the default value. Adds the option at the end.- Parameters:
commandline- the commandline string (without the leading dash "-")property- the Java Beans property namedefValue- the default value- Returns:
- the generated option object
-
insert
public AbstractOption insert(int index, String commandline, String property, Object defValue)
Determines the appropriate concrete option class, sets it up and returns it. This call will always output the default value.- Parameters:
index- the position for the option, use -1 to add at the endcommandline- the commandline string (without the leading dash "-")property- the Java Beans property namedefValue- the default value- Returns:
- the generated option object
-
add
public AbstractOption add(String commandline, String property, Object defValue, Number lower, Number upper)
Determines the appropriate concrete option class, sets it up and returns it. This call will always output the default value. Adds the option at the end.- Parameters:
commandline- the commandline string (without the leading dash "-")property- the Java Beans property namedefValue- the default valuelower- the lower boundupper- the upper bound- Returns:
- the generated option object
-
insert
public AbstractOption insert(int index, String commandline, String property, Object defValue, Number lower, Number upper)
Determines the appropriate concrete option class, sets it up and returns it.- Parameters:
index- the position for the option, use -1 to add at the endcommandline- the commandline string (without the leading dash "-")property- the Java Beans property namedefValue- the default valuelower- the lower boundupper- the upper bound- Returns:
- the generated option object
-
disableVariables
public void disableVariables(String property)
Disables variable support for this bean property.- Parameters:
property- the property to disable
-
enabledVariables
public void enabledVariables(String property)
Enables variable support for this bean property again.- Parameters:
property- the property to enable
-
allowsVariables
public boolean allowsVariables(String property)
Checks whether this property allows variables.- Parameters:
property- the bean property to check- Returns:
- true if variables allowed
-
getOptionsList
public List<AbstractOption> getOptionsList()
Returns the list of options.- Returns:
- the options
-
findOption
protected AbstractOption findOption(String flagOrProperty, boolean flag)
Returns the option for the commandline flag/property.- Parameters:
flagOrProperty- the commandline flag/property stringflag- if true then "flagOrProperty" is interpreted as flag instead of property- Returns:
- the option or null if not found
-
findByFlag
public AbstractOption findByFlag(String flag)
Tries to locate the corresponding option for the given commandline string (without the leading dash).- Parameters:
flag- the commandline to look for (no leading dash)- Returns:
- the option or null if not found
- See Also:
AbstractOption.getCommandline()
-
findByProperty
public AbstractOption findByProperty(String property)
Tries to locate the corresponding option for the given property name.- Parameters:
property- the property name to look for- Returns:
- the option or null if not found
-
setConstraintForProperty
public boolean setConstraintForProperty(String property, AbstractOptionConstraint constraint)
Tries to locate the option for the given property name and then set the constraint.- Parameters:
property- the property name to look forconstraint- the constraint to set, null to remove any constraint- Returns:
- true if successfully updated
-
hasConstraintForProperty
public boolean hasConstraintForProperty(String property)
Tries to locate the option for the given property name and then checks for any constraint.- Parameters:
property- the property name to look for- Returns:
- true if property located and constraint available
-
getConstraintForProperty
public AbstractOptionConstraint getConstraintForProperty(String property)
Tries to locate the option for the given property name and then return any constraint.- Parameters:
property- the property name to look for- Returns:
- the constraint, null if none set or property not found
-
getConstraintForProperty
public <T extends AbstractOptionConstraint> T getConstraintForProperty(String property, Class<T> type)
Tries to locate the option for the given property name and then return any constraint.- Parameters:
property- the property name to look fortype- the constraint type to cast to- Returns:
- the constraint, null if none set or property not found
-
findByClass
public AbstractOption findByClass(Class cls)
Tries to locate the corresponding option for the given class.- Parameters:
cls- the class to look for- Returns:
- the option or null if not found
- See Also:
AbstractOption.getReadMethod()
-
removeByFlag
public AbstractOption removeByFlag(String flag)
Removes the corresponding option associated with the given commandline string (without the leading dash).- Parameters:
flag- the commandline to look for (no leading dash)- Returns:
- the option or null if not found
- See Also:
AbstractOption.getCommandline()
-
removeByProperty
public AbstractOption removeByProperty(String property)
Removes the corresponding option associated with the given property name.- Parameters:
property- the property name to look for- Returns:
- the option or null if not found
-
removeOption
protected AbstractOption removeOption(String flagOrProperty, boolean isFlag)
Removes the option associated with the commandline flag/property.- Parameters:
flagOrProperty- the commandline flag/property stringisFlag- if true then "flagOrProperty" is interpreted as flag instead of property- Returns:
- the option or null if not found
-
isRemovedFlag
public boolean isRemovedFlag(String flag)
Checks whether the flag has been removed intentionally.- Parameters:
flag- the command-line flag to check- Returns:
- true if removed
-
isRemovedProperty
public boolean isRemovedProperty(String property)
Checks whether the property has been removed intentionally.- Parameters:
property- the property to check- Returns:
- true if removed
-
setVariableForProperty
public AbstractArgumentOption setVariableForProperty(String property, String variable)
Sets the variable in the option handler for the given property.- Parameters:
property- the property to set the variable forvariable- the variable name, null removes the variable- Returns:
- the option that the variable was set for, null if property not found
-
getVariableForProperty
public String getVariableForProperty(String property)
Returns the variable that is attached to a property.- Parameters:
property- the property to check for a variable- Returns:
- null if no variable attached, otherwise the variable name
-
hasVariableForProperty
public boolean hasVariableForProperty(String property)
Returns whether there is a variable attached to the property.- Parameters:
property- the property to check for a variable- Returns:
- true if variable attached
-
ensureVariableForPropertyExists
public String ensureVariableForPropertyExists(String property)
If a variable is attached to the variable, it checks whether the variable really exists in the context. If that shouldn't be the case, an error message gets returned, otherwise null. If no variable is attached, then null is being returned as well.- Parameters:
property- the property to check for a variable- Returns:
- null if no variable attached or variable exists, otherwise error message
-
isDefaultValueByFlag
public boolean isDefaultValueByFlag(String flag)
Checks whether the current value associated with the flag is the default value.- Parameters:
flag- the commandline to look for (no leading dash)- Returns:
- true if option located, has an argument and is the default value
-
isDefaultValueByProperty
public boolean isDefaultValueByProperty(String property)
Checks whether the current value associated with the property is the default value.- Parameters:
property- the property name to look for- Returns:
- true if option located, has an argument and is the default value
-
setDefaults
public void setDefaults()
Sets the default values.
-
updateVariablesInstance
public void updateVariablesInstance(Variables variables)
Updates the Variables instance recursively on all options.- Parameters:
variables- the variables instance to use
-
updateVariableValues
public String updateVariableValues()
Updates the variables, i.e., in case an option uses a variable and this variable's value has changed, the updated value will be set.- Returns:
- null if all variables were successfully updated
-
updateVariableValues
public String updateVariableValues(boolean forceUpdate)
Updates the variables, i.e., in case an option uses a variable and this variable's value has changed (or update is enforced), the updated value will be set.- Parameters:
forceUpdate- whether to force the update- Returns:
- null if all variables were successfully updated
-
updateVariableValues
public String updateVariableValues(boolean forceUpdate, Logger log)
Updates the variables, i.e., in case an option uses a variable and this variable's value has changed (or update is enforced), the updated value will be set.- Parameters:
forceUpdate- whether to force the updatelog- optional logger for logging traversal, can be null- Returns:
- null if all variables were successfully updated
-
registerVariables
public void registerVariables()
Registers all the variables recursively.
-
deregisterVariables
public void deregisterVariables()
Deregisters all the variables recursively.
-
updateVariablePrefix
public void updateVariablePrefix(String prefix, boolean add)
Adds or removes the specified prefix from all variables.- Parameters:
prefix- the prefix for the variablesadd- whether to add or remove the prefixes
-
traverse
public void traverse(OptionTraverser traverser)
Traverses all the options and lets the various options get handled by the supplied object.- Parameters:
traverser- the object to handled the traversed options
-
traverse
public void traverse(OptionTraverser traverser, boolean nonAdams)
Traverses all the options and lets the various options get handled by the supplied object.- Parameters:
traverser- the object to handled the traversed optionsnonAdams- whether to traverse non-ADAMS objects as well
-
traverse
protected void traverse(OptionTraverser traverser, OptionTraversalPath path, boolean nonAdams)
Traverses all the options and lets the various options get handled by the supplied object.- Parameters:
traverser- the object to handle the traversed optionspath- the path so farnonAdams- whether to traverse non-ADAMS objects as well
-
traverse
protected void traverse(OptionTraverser traverser, OptionTraversalPath path, Object obj)
Traverses a non-ADAMS OptionHandler.- Parameters:
traverser- the object to handle the traversed optionspath- the path so farobj- the non-ADAMS object to traverse
-
size
public int size()
Returns the number of managed options.- Returns:
- the number of options
-
isValid
public boolean isValid(String property, Object value)
Checks whether the value is valid, in case there is a constraint in place for the property.- Parameters:
property- the property to checkvalue- the value to check- Returns:
- true if valid
-
cleanUp
public void cleanUp()
Cleans up the options vector.- Specified by:
cleanUpin interfaceCleanUpHandler
-
getProperties
protected static Properties getProperties()
Returns the properties file with the custom editors.- Returns:
- the props file
-
isBlacklistedClass
protected static boolean isBlacklistedClass(Object obj)
Checks whether the object's class is blacklisted.- Parameters:
obj- the object to check- Returns:
- true if blacklisted
-
isBlacklistedClass
protected static boolean isBlacklistedClass(Class cls)
Checks whether the class is blacklisted.- Parameters:
cls- the class to check- Returns:
- true if blacklisted
-
isBlacklistedClass
protected static boolean isBlacklistedClass(String cls)
Checks whether the class is blacklisted.- Parameters:
cls- the class to check- Returns:
- true if blacklisted
-
-