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 String
FILENAME
protected static Set<String>
m_BlacklistedClasses
the blacklisted classes.protected Map<String,Integer>
m_CommandlineIndex
the commandline flag <-> index relationship.protected static Variables
m_DefaultVariablesInstance
default instance (gets replaced at runtime).protected Set<String>
m_NoVariablesProperties
the properties that cannot have variables attached.protected List<AbstractOption>
m_Options
the options.protected OptionHandler
m_Owner
the owner.protected static Properties
m_Properties
the properties in use.protected Map<String,Integer>
m_PropertyIndex
the (bean) property <-> index relationship.protected boolean
m_Quiet
whether to suppress error messages.protected boolean
m_ThrowExceptions
whether to throw exceptions or just ignore errors.protected Variables
m_Variables
the 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 AbstractOption
add(String commandline, String property, Object defValue)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
add(String commandline, String property, Object defValue, boolean outputDefValue)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
add(String commandline, String property, Object defValue, boolean outputDefValue, UserMode minUserMode)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
add(String commandline, String property, Object defValue, boolean outputDefValue, Number lower, Number upper)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
add(String commandline, String property, Object defValue, boolean outputDefValue, Number lower, Number upper, UserMode minUserMode)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
add(String commandline, String property, Object defValue, UserMode minUserMode)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
add(String commandline, String property, Object defValue, Number lower, Number upper)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
add(String commandline, String property, Object defValue, Number lower, Number upper, UserMode minUserMode)
Determines the appropriate concrete option class, sets it up and returns it.boolean
allowsVariables(String property)
Checks whether this property allows variables.void
cleanUp()
Cleans up the options vector.void
deregisterVariables()
Deregisters all the variables recursively.void
disableVariables(String property)
Disables variable support for this bean property.void
enabledVariables(String property)
Enables variable support for this bean property again.String
ensureVariableForPropertyExists(String property)
If a variable is attached to the variable, it checks whether the variable really exists in the context.AbstractOption
findByClass(Class cls)
Tries to locate the corresponding option for the given class.AbstractOption
findByFlag(String flag)
Tries to locate the corresponding option for the given commandline string (without the leading dash).AbstractOption
findByProperty(String property)
Tries to locate the corresponding option for the given property name.protected AbstractOption
findOption(String flagOrProperty, boolean flag)
Returns the option for the commandline flag/property.List<AbstractOption>
getOptionsList()
Returns the list of options.OptionHandler
getOwner()
Returns the owning OptionHandler.protected static Properties
getProperties()
Returns the properties file with the custom editors.boolean
getThrowExceptions()
Returns whether to throw exceptions on errors or to ignore them.String
getVariableForProperty(String property)
Returns the variable that is attached to a property.Variables
getVariables()
Returns the variables instance in use.protected void
handleError(Throwable t)
Handles the given throwable/exception.boolean
hasVariableForProperty(String property)
Returns whether there is a variable attached to the property.AbstractOption
insert(int index, String commandline, String property, Object defValue)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
insert(int index, String commandline, String property, Object defValue, boolean outputDefValue)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
insert(int index, String commandline, String property, Object defValue, boolean outputDefValue, UserMode minUserMode)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
insert(int index, String commandline, String property, Object defValue, boolean outputDefValue, Number lower, Number upper)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
insert(int index, String commandline, String property, Object defValue, boolean outputDefValue, Number lower, Number upper, UserMode minUserMode)
Determines the appropriate concrete option class, sets it up and returns it.AbstractOption
insert(int index, String commandline, String property, Object defValue, UserMode minUserMode)
Determines the appropriate concrete option class, sets it up and returns it.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.AbstractOption
insert(int index, String commandline, String property, Object defValue, Number lower, Number upper, UserMode minUserMode)
Determines the appropriate concrete option class, sets it up and returns it.protected static boolean
isBlacklistedClass(Class cls)
Checks whether the class is blacklisted.protected static boolean
isBlacklistedClass(Object obj)
Checks whether the object's class is blacklisted.protected static boolean
isBlacklistedClass(String cls)
Checks whether the class is blacklisted.boolean
isDefaultValueByFlag(String flag)
Checks whether the current value associated with the flag is the default value.boolean
isDefaultValueByProperty(String property)
Checks whether the current value associated with the property is the default value.boolean
isQuiet()
Returns whether to suppress error messages.boolean
isValid(String property, Number n)
Checks whether the numeric value is valid (within the bounds, if any).void
registerVariables()
Registers all the variables recursively.AbstractOption
removeByFlag(String flag)
Removes the corresponding option associated with the given commandline string (without the leading dash).AbstractOption
removeByProperty(String property)
Removes the corresponding option associated with the given property name.protected AbstractOption
removeOption(String flagOrProperty, boolean flag)
Removes the option associated with the commandline flag/property.void
setDefaults()
Sets the default values.void
setQuiet(boolean value)
Sets whether to suppress error messages.void
setThrowExceptions(boolean value)
Sets whether to throw exceptions or simply ignore errors (recursively).AbstractArgumentOption
setVariableForProperty(String property, String variable)
Sets the variable in the option handler for the given property.void
setVariables(Variables value)
Sets the Variables instance to use (not recursively!).int
size()
Returns the number of managed options.void
traverse(OptionTraverser traverser)
Traverses all the options and lets the various options get handled by the supplied object.void
traverse(OptionTraverser traverser, boolean nonAdams)
Traverses all the options and lets the various options get handled by the supplied object.protected void
traverse(OptionTraverser traverser, OptionTraversalPath path, boolean nonAdams)
Traverses all the options and lets the various options get handled by the supplied object.protected void
traverse(OptionTraverser traverser, OptionTraversalPath path, Object obj)
Traverses a non-ADAMS OptionHandler.void
updateVariablePrefix(String prefix, boolean add)
Adds or removes the specified prefix from all variables.void
updateVariablesInstance(Variables variables)
Updates the Variables instance recursively on all options.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.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.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.
-
-
-
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
-
add
public AbstractOption add(String commandline, String property, Object defValue, UserMode minUserMode)
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 valueminUserMode
- the minimum user mode before showing this option- 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
-
insert
public AbstractOption insert(int index, String commandline, String property, Object defValue, UserMode minUserMode)
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 valueminUserMode
- the minimum user mode before showing this option- Returns:
- the generated option object
-
add
public AbstractOption add(String commandline, String property, Object defValue, boolean outputDefValue)
Determines the appropriate concrete option class, sets it up and returns it. Adds the option at the end.- Parameters:
commandline
- the commandline string (without the leading dash "-")property
- the Java Beans property namedefValue
- the default valueoutputDefValue
- if true then the default value will be listed in the Javadoc- Returns:
- the generated option object
-
add
public AbstractOption add(String commandline, String property, Object defValue, boolean outputDefValue, UserMode minUserMode)
Determines the appropriate concrete option class, sets it up and returns it. Adds the option at the end.- Parameters:
commandline
- the commandline string (without the leading dash "-")property
- the Java Beans property namedefValue
- the default valueoutputDefValue
- if true then the default value will be listed in the JavadocminUserMode
- the minimum user mode before showing this option- Returns:
- the generated option object
-
insert
public AbstractOption insert(int index, String commandline, String property, Object defValue, boolean outputDefValue)
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 valueoutputDefValue
- if true then the default value will be listed in the Javadoc- Returns:
- the generated option object
-
insert
public AbstractOption insert(int index, String commandline, String property, Object defValue, boolean outputDefValue, UserMode minUserMode)
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 valueoutputDefValue
- if true then the default value will be listed in the JavadocminUserMode
- the minimum user mode before showing this option- 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
-
add
public AbstractOption add(String commandline, String property, Object defValue, Number lower, Number upper, UserMode minUserMode)
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 boundminUserMode
- the minimum user mode before showing this option- 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. 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 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, UserMode minUserMode)
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 valuelower
- the lower boundupper
- the upper boundminUserMode
- the minimum user mode before showing this option- Returns:
- the generated option object
-
add
public AbstractOption add(String commandline, String property, Object defValue, boolean outputDefValue, Number lower, Number upper)
Determines the appropriate concrete option class, sets it up and returns it. 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 boundoutputDefValue
- if true then the default value will be listed in the Javadoc- Returns:
- the generated option object
-
add
public AbstractOption add(String commandline, String property, Object defValue, boolean outputDefValue, Number lower, Number upper, UserMode minUserMode)
Determines the appropriate concrete option class, sets it up and returns it. 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 boundoutputDefValue
- if true then the default value will be listed in the JavadocminUserMode
- the minimum user mode before showing this option- Returns:
- the generated option object
-
insert
public AbstractOption insert(int index, String commandline, String property, Object defValue, boolean outputDefValue, 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 boundoutputDefValue
- if true then the default value will be listed in the Javadoc- Returns:
- the generated option object
-
insert
public AbstractOption insert(int index, String commandline, String property, Object defValue, boolean outputDefValue, Number lower, Number upper, UserMode minUserMode)
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 boundoutputDefValue
- if true then the default value will be listed in the JavadocminUserMode
- the minimum user mode before showing this option- 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
-
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 flag)
Removes the option associated with 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
-
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, Number n)
Checks whether the numeric value is valid (within the bounds, if any).- Parameters:
property
- the property to checkn
- the number to check- Returns:
- true if valid
-
cleanUp
public void cleanUp()
Cleans up the options vector.- Specified by:
cleanUp
in 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
-
-