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_ClassnameCachefor caching classname and constructor.protected MessageCollectionm_Errorsfor storing errors that occurred while consuming the options.protected Cm_Inputthe input data.protected OptionHandlerm_Outputthe generated object.protected HashSet<String>m_SkippedPropertiestop-level properties to skip.protected booleanm_UsePropertyNameswhether to use command-line flags or property names.protected MessageCollectionm_Warningsfor 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 voidcheckDeprecation(Class cls)Checks the deprecation of the object and logs a warning if that's the case.protected voidcheckDeprecation(Object object)Checks the deprecation of the object and logs a warning if that's the case.voidcleanUp()Cleans up data structures, frees up memory.OptionHandlerconsume()Consumes the current input.OptionHandlerconsume(OptionHandler output, C input)Consumes the provided input and updates the provided option handler.static OptionHandlerconsume(Class<? extends OptionConsumer> cls, Object input)Uses the specified consumer to process the given object and return the option handler.protected abstract CconvertToInput(String s)Converts the input string into the internal format.protected CharsetdetermineCharset()Returns the charset to use.protected abstract voiddoConsume(OptionManager manager, C input)Visits the options.protected ConstructorforName(String classname)Turns the classname into a string.OptionHandlerfromFile(File file)Processes the specified file.static OptionHandlerfromFile(Class<? extends OptionConsumer> cls, File file)Uses the specified consumer to parse the content of the specified file and return the option handler.static OptionHandlerfromString(Class<? extends OptionConsumer> cls, String s)Uses the specified consumer to parse the given string and return the option handler.static OptionHandlerfromString(Class<? extends OptionConsumer> cls, String s, MessageCollection errors)Uses the specified consumer to parse the given string and return the option handler.OptionHandlerfromString(String s)Processes the specified string.protected StringgenerateLoggingString(String s)Generates a debug string, e.g., based on the method name.MessageCollectiongetErrors()Returns the error log.CgetInput()Returns the currently set input data.protected StringgetOptionIdentifier(AbstractOption option)Returns either the property name or the commandline flag, depending on whether property names are to be used or not.OptionHandlergetOutput()Returns the visited top-level object.HashSet<String>getSkippedProperties()Returns the skipped top-level properties.protected booleangetUsePropertyNames()Returns whether property names are used or just the command-line flags.MessageCollectiongetWarnings()Returns the warning log.protected MethodgetWriteMethod(AbstractOption option)Attempts to return the write method of the option.abstract StringglobalInfo()Returns a string describing the object.booleanhasErrors()Checks whether errors were encountered while consuming the options.booleanhasWarnings()Checks whether warnings were encountered while consuming the options.protected voidinitialize()Initializes the members.protected abstract OptionHandlerinitOutput()Creates the empty option handler from the internal data structure and returns it.booleanisQuiet()Returns whether console output is suppressed or not.protected voidlogError(String msg)Adds the specified error message to the internal error log.protected voidlogWarning(String msg)Adds the specified warning message to the internal warning log.protected abstract voidprocessOption(AbstractArgumentOption option, V values)Processes the specified argument option.protected voidprocessOption(AbstractOption option, V values)Processes the specified argument option.protected abstract voidprocessOption(BooleanOption option, V values)Processes the specified boolean option.protected abstract voidprocessOption(ClassOption option, V values)Processes the specified class option.OptionHandlerread(String filename)Reads the option handler from the specified file.protected voidreset()Resets the members.voidsetInput(C input)Sets the input data to use.voidsetLoggingLevel(LoggingLevel value)Sets the logging level.voidsetQuiet(boolean value)Sets whether console output is suppressed or not.voidsetSkippedProperties(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:
globalInfoin 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:
setLoggingLevelin 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:
hasErrorsin interfaceOptionConsumer<C,V>- Returns:
- true if errors were encountered
- See Also:
getErrors()
-
getErrors
public MessageCollection getErrors()
Returns the error log.- Specified by:
getErrorsin 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:
hasWarningsin interfaceOptionConsumer<C,V>- Returns:
- true if warnings were encountered
- See Also:
getWarnings()
-
getWarnings
public MessageCollection getWarnings()
Returns the warning log.- Specified by:
getWarningsin 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:
setQuietin 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:
isQuietin 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:
setSkippedPropertiesin interfaceOptionConsumer<C,V>- Parameters:
value- the properties
-
getSkippedProperties
public HashSet<String> getSkippedProperties()
Returns the skipped top-level properties.- Specified by:
getSkippedPropertiesin 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:
getOutputin interfaceOptionConsumer<C,V>- Returns:
- the visited object
-
setInput
public void setInput(C input)
Sets the input data to use.- Specified by:
setInputin interfaceOptionConsumer<C,V>- Parameters:
input- the data to use
-
getInput
public C getInput()
Returns the currently set input data.- Specified by:
getInputin 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:
consumein 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:
consumein 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:
fromStringin 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:
fromFilein 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:
readin 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:
cleanUpin interfaceCleanUpHandler- Specified by:
cleanUpin 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
-
-