Package adams.core.option
Class AbstractOptionConsumer<C,V>
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.core.option.AbstractOptionConsumer<C,V>
-
- Type Parameters:
C
- the type of data to consumeV
- the type of data used for values
- All Implemented Interfaces:
CleanUpHandler
,LoggingSupporter
,OptionConsumer<C,V>
,SizeOfHandler
,Serializable
- Direct Known Subclasses:
AbstractRecursiveOptionConsumer
public abstract class AbstractOptionConsumer<C,V> extends LoggingObject implements OptionConsumer<C,V>
Sets the option values based on the input data.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected static Map<String,Constructor>
m_ClassnameCache
for caching classname and constructor.protected MessageCollection
m_Errors
for storing errors that occurred while consuming the options.protected C
m_Input
the input data.protected OptionHandler
m_Output
the generated object.protected HashSet<String>
m_SkippedProperties
top-level properties to skip.protected boolean
m_UsePropertyNames
whether to use command-line flags or property names.protected MessageCollection
m_Warnings
for storing warnings that occurred while consuming the options.-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Constructor Description AbstractOptionConsumer()
Initializes the visitor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
checkDeprecation(Class cls)
Checks the deprecation of the object and logs a warning if that's the case.protected void
checkDeprecation(Object object)
Checks the deprecation of the object and logs a warning if that's the case.void
cleanUp()
Cleans up data structures, frees up memory.OptionHandler
consume()
Consumes the current input.OptionHandler
consume(OptionHandler output, C input)
Consumes the provided input and updates the provided option handler.static OptionHandler
consume(Class<? extends OptionConsumer> cls, Object input)
Uses the specified consumer to process the given object and return the option handler.protected abstract C
convertToInput(String s)
Converts the input string into the internal format.protected Charset
determineCharset()
Returns the charset to use.protected abstract void
doConsume(OptionManager manager, C input)
Visits the options.protected Constructor
forName(String classname)
Turns the classname into a string.OptionHandler
fromFile(File file)
Processes the specified file.static OptionHandler
fromFile(Class<? extends OptionConsumer> cls, File file)
Uses the specified consumer to parse the content of the specified file and return the option handler.static OptionHandler
fromString(Class<? extends OptionConsumer> cls, String s)
Uses the specified consumer to parse the given string and return the option handler.static OptionHandler
fromString(Class<? extends OptionConsumer> cls, String s, MessageCollection errors)
Uses the specified consumer to parse the given string and return the option handler.OptionHandler
fromString(String s)
Processes the specified string.protected String
generateLoggingString(String s)
Generates a debug string, e.g., based on the method name.MessageCollection
getErrors()
Returns the error log.C
getInput()
Returns the currently set input data.protected String
getOptionIdentifier(AbstractOption option)
Returns either the property name or the commandline flag, depending on whether property names are to be used or not.OptionHandler
getOutput()
Returns the visited top-level object.HashSet<String>
getSkippedProperties()
Returns the skipped top-level properties.protected boolean
getUsePropertyNames()
Returns whether property names are used or just the command-line flags.MessageCollection
getWarnings()
Returns the warning log.protected Method
getWriteMethod(AbstractOption option)
Attempts to return the write method of the option.abstract String
globalInfo()
Returns a string describing the object.boolean
hasErrors()
Checks whether errors were encountered while consuming the options.boolean
hasWarnings()
Checks whether warnings were encountered while consuming the options.protected void
initialize()
Initializes the members.protected abstract OptionHandler
initOutput()
Creates the empty option handler from the internal data structure and returns it.boolean
isQuiet()
Returns whether console output is suppressed or not.protected void
logError(String msg)
Adds the specified error message to the internal error log.protected void
logWarning(String msg)
Adds the specified warning message to the internal warning log.protected abstract void
processOption(AbstractArgumentOption option, V values)
Processes the specified argument option.protected void
processOption(AbstractOption option, V values)
Processes the specified argument option.protected abstract void
processOption(BooleanOption option, V values)
Processes the specified boolean option.protected abstract void
processOption(ClassOption option, V values)
Processes the specified class option.OptionHandler
read(String filename)
Reads the option handler from the specified file.protected void
reset()
Resets the members.void
setInput(C input)
Sets the input data to use.void
setLoggingLevel(LoggingLevel value)
Sets the logging level.void
setQuiet(boolean value)
Sets whether console output is suppressed or not.void
setSkippedProperties(HashSet<String> value)
Sets the top-level properties to skip.-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface adams.core.option.OptionConsumer
getLoggingLevel
-
-
-
-
Field Detail
-
m_Input
protected C m_Input
the input data.
-
m_UsePropertyNames
protected boolean m_UsePropertyNames
whether to use command-line flags or property names.
-
m_Output
protected OptionHandler m_Output
the generated object.
-
m_Errors
protected MessageCollection m_Errors
for storing errors that occurred while consuming the options.
-
m_Warnings
protected MessageCollection m_Warnings
for storing warnings that occurred while consuming the options.
-
m_ClassnameCache
protected static Map<String,Constructor> m_ClassnameCache
for caching classname and constructor.
-
-
Method Detail
-
globalInfo
public abstract String globalInfo()
Returns a string describing the object.- Specified by:
globalInfo
in interfaceOptionConsumer<C,V>
- Returns:
- a description suitable for displaying in the gui
-
initialize
protected void initialize()
Initializes the members.
-
reset
protected void reset()
Resets the members.
-
setLoggingLevel
public void setLoggingLevel(LoggingLevel value)
Sets the logging level.- Specified by:
setLoggingLevel
in interfaceOptionConsumer<C,V>
- Parameters:
value
- the level
-
generateLoggingString
protected String generateLoggingString(String s)
Generates a debug string, e.g., based on the method name.
Default implementation merely returns the string.- Parameters:
s
- the string to process- Returns:
- the processed string
-
logError
protected void logError(String msg)
Adds the specified error message to the internal error log.- Parameters:
msg
- the error message to log- See Also:
hasErrors()
,getErrors()
-
hasErrors
public boolean hasErrors()
Checks whether errors were encountered while consuming the options.- Specified by:
hasErrors
in interfaceOptionConsumer<C,V>
- Returns:
- true if errors were encountered
- See Also:
getErrors()
-
getErrors
public MessageCollection getErrors()
Returns the error log.- Specified by:
getErrors
in interfaceOptionConsumer<C,V>
- Returns:
- the error log, can be empty
- See Also:
logError(String)
,hasErrors()
-
logWarning
protected void logWarning(String msg)
Adds the specified warning message to the internal warning log.- Parameters:
msg
- the warning message to log- See Also:
hasWarnings()
,getWarnings()
-
hasWarnings
public boolean hasWarnings()
Checks whether warnings were encountered while consuming the options.- Specified by:
hasWarnings
in interfaceOptionConsumer<C,V>
- Returns:
- true if warnings were encountered
- See Also:
getWarnings()
-
getWarnings
public MessageCollection getWarnings()
Returns the warning log.- Specified by:
getWarnings
in interfaceOptionConsumer<C,V>
- Returns:
- the warning log, can be empty
- See Also:
logWarning(String)
,hasWarnings()
-
checkDeprecation
protected void checkDeprecation(Object object)
Checks the deprecation of the object and logs a warning if that's the case.- Parameters:
object
- the object to check
-
checkDeprecation
protected void checkDeprecation(Class cls)
Checks the deprecation of the object and logs a warning if that's the case.- Parameters:
cls
- the class to check
-
setQuiet
public void setQuiet(boolean value)
Sets whether console output is suppressed or not.- Specified by:
setQuiet
in interfaceOptionConsumer<C,V>
- Parameters:
value
- if true then console output is suppressed (out/err)
-
isQuiet
public boolean isQuiet()
Returns whether console output is suppressed or not.- Specified by:
isQuiet
in interfaceOptionConsumer<C,V>
- Returns:
- true if console output is suppressed
-
setSkippedProperties
public void setSkippedProperties(HashSet<String> value)
Sets the top-level properties to skip.- Specified by:
setSkippedProperties
in interfaceOptionConsumer<C,V>
- Parameters:
value
- the properties
-
getSkippedProperties
public HashSet<String> getSkippedProperties()
Returns the skipped top-level properties.- Specified by:
getSkippedProperties
in interfaceOptionConsumer<C,V>
- Returns:
- the properties
-
getUsePropertyNames
protected boolean getUsePropertyNames()
Returns whether property names are used or just the command-line flags.- Returns:
- true if property names are used
-
getOptionIdentifier
protected String getOptionIdentifier(AbstractOption option)
Returns either the property name or the commandline flag, depending on whether property names are to be used or not.- Parameters:
option
- the option to return the identifier for- Returns:
- the identifier
- See Also:
getUsePropertyNames()
-
getOutput
public OptionHandler getOutput()
Returns the visited top-level object.- Specified by:
getOutput
in interfaceOptionConsumer<C,V>
- Returns:
- the visited object
-
setInput
public void setInput(C input)
Sets the input data to use.- Specified by:
setInput
in interfaceOptionConsumer<C,V>
- Parameters:
input
- the data to use
-
getInput
public C getInput()
Returns the currently set input data.- Specified by:
getInput
in interfaceOptionConsumer<C,V>
- Returns:
- the data in use
-
convertToInput
protected abstract C convertToInput(String s)
Converts the input string into the internal format.- Parameters:
s
- the string to process- Returns:
- the internal format, null in case of an error
-
forName
protected Constructor forName(String classname) throws Exception
Turns the classname into a string.- Parameters:
classname
- the classname- Returns:
- the class
- Throws:
Exception
-
initOutput
protected abstract OptionHandler initOutput()
Creates the empty option handler from the internal data structure and returns it. This option handler will then be "visited".- Returns:
- the generated option handler, null in case of an error
-
getWriteMethod
protected Method getWriteMethod(AbstractOption option)
Attempts to return the write method of the option. Outputs an error message in the console if not available.- Parameters:
option
- the option to get the write method for- Returns:
- the method, null if not available
-
processOption
protected abstract void processOption(BooleanOption option, V values) throws Exception
Processes the specified boolean option.- Parameters:
option
- the boolean option to processvalues
- the value for the boolean option- Throws:
Exception
- if something goes wrong
-
processOption
protected abstract void processOption(ClassOption option, V values) throws Exception
Processes the specified class option.- Parameters:
option
- the class option to processvalues
- the value for the class option- Throws:
Exception
- if something goes wrong
-
processOption
protected abstract void processOption(AbstractArgumentOption option, V values) throws Exception
Processes the specified argument option.- Parameters:
option
- the argument option to processvalues
- the value for the argument option- Throws:
Exception
- if something goes wrong
-
processOption
protected void processOption(AbstractOption option, V values) throws Exception
Processes the specified argument option.- Parameters:
option
- the argument option to processvalues
- the value for the argument option- Throws:
Exception
- if something goes wrong
-
doConsume
protected abstract void doConsume(OptionManager manager, C input)
Visits the options.- Parameters:
manager
- the manager to visitinput
- the input data to use
-
consume
public OptionHandler consume()
Consumes the current input. The generated option handler can be retrieved via getOutput() as well.- Specified by:
consume
in interfaceOptionConsumer<C,V>
- Returns:
- the created object
- See Also:
getOutput()
-
consume
public OptionHandler consume(OptionHandler output, C input)
Consumes the provided input and updates the provided option handler. The option handler can be retrieved via getOutput() as well.- Specified by:
consume
in interfaceOptionConsumer<C,V>
- Parameters:
output
- the option handler to updateinput
- the data to use for updating- Returns:
- the updated object
- See Also:
getOutput()
-
determineCharset
protected Charset determineCharset()
Returns the charset to use. Checks whether consumer implementsEncodingSupporter
.- Returns:
- the character set
-
fromString
public OptionHandler fromString(String s)
Processes the specified string.- Specified by:
fromString
in interfaceOptionConsumer<C,V>
- Parameters:
s
- the string to process- Returns:
- the created object
- See Also:
getOutput()
-
fromFile
public OptionHandler fromFile(File file)
Processes the specified file.- Specified by:
fromFile
in interfaceOptionConsumer<C,V>
- Parameters:
file
- the file to process- Returns:
- the created object, null in case content of file couldn't be loaded
-
read
public OptionHandler read(String filename)
Reads the option handler from the specified file.- Specified by:
read
in interfaceOptionConsumer<C,V>
- Parameters:
filename
- the file to read from- Returns:
- the option handler if successful, null otherwise
-
cleanUp
public void cleanUp()
Cleans up data structures, frees up memory.- Specified by:
cleanUp
in interfaceCleanUpHandler
- Specified by:
cleanUp
in interfaceOptionConsumer<C,V>
-
fromString
public static OptionHandler fromString(Class<? extends OptionConsumer> cls, String s)
Uses the specified consumer to parse the given string and return the option handler.- Parameters:
cls
- the consumer class to uses
- the string to parse- Returns:
- the option handler, null in case of an error
-
fromString
public static OptionHandler fromString(Class<? extends OptionConsumer> cls, String s, MessageCollection errors)
Uses the specified consumer to parse the given string and return the option handler.- Parameters:
cls
- the consumer class to uses
- the string to parseerrors
- for storing errors, can be null- Returns:
- the option handler, null in case of an error
-
fromFile
public static OptionHandler fromFile(Class<? extends OptionConsumer> cls, File file)
Uses the specified consumer to parse the content of the specified file and return the option handler.- Parameters:
cls
- the consumer class to usefile
- the file to load and parse- Returns:
- the option handler, null in case of an error
-
consume
public static OptionHandler consume(Class<? extends OptionConsumer> cls, Object input)
Uses the specified consumer to process the given object and return the option handler.- Parameters:
cls
- the consumer class to useinput
- the data structure to process- Returns:
- the option handler, null in case of an error
-
-