Package adams.core
Class Variables
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.core.Variables
-
- All Implemented Interfaces:
CleanUpHandler
,CloneHandler<Variables>
,LoggingSupporter
,SizeOfHandler
,Serializable
- Direct Known Subclasses:
FlowVariables
public class Variables extends LoggingObject implements CleanUpHandler, CloneHandler<Variables>
A container for storing variables and their values. Values are stored as string representations. A variable placeholder string is of the following form:
- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
ENVIRONMENT_VARIABLE_PREFIX
,SYSTEM_PROPERTY_PREFIX
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static String
CHARS
allowed characters.static String
END
the end of a variable.static String
ENVIRONMENT_VARIABLE_PREFIX
the prefix for environment variables.protected Hashtable<String,String>
m_EnvironmentVariables
the environment variables <-> value relation.protected Hashtable<String,String>
m_SystemProperties
the system properties <-> value relation.protected WrapperHashSet<VariableChangeListener>
m_VariableChangeListeners
the listeners.protected Hashtable<String,String>
m_Variables
the variable <-> value relation.static String
START
the start of a variable.static String
SYSTEM_PROPERTY_PREFIX
the prefix for system properties.-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Constructor Description Variables()
Initializes the container.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addVariableChangeListener(VariableChangeListener l)
Adds the listener to the internal list.void
assign(Variables other)
Adds all the variables from the other Variables object (overwrites any existing ones).void
assign(Variables other, BaseRegExp filter)
Adds all the variables from the other Variables object (overwrites any existing ones).void
cleanUp()
Cleans up data structures, frees up memory.void
clear()
Removes all currently stored variables.static String
createValidName(String s)
Creates a valid variable name.protected String
doExpand(String s)
Performs all expansions.protected String
doExpandEnv(String s)
Expands environment variables.protected String
doExpandRegular(String s)
Expands regular variables.protected String
doExpandSystemProps(String s)
Expands system properties.String
expand(String s)
Replaces all variables in the string with the currently stored values.protected String
expand(String s, boolean recurse)
Replaces all variables in the string with the currently stored values.static String
extractName(String variable)
Extracts the name of a variable from the placeholder string, i.e., it strips the "@{" and "}" from the string.static String[]
extractNames(String expr)
Extracts all the variables from the given expression.String
get(String name)
Returns the stored value if present, otherwise null.String
get(String name, String defValue)
Returns the stored value if present, otherwise the default value.Variables
getClone()
Returns a clone of the object (but without the listeners).Variables
getClone(BaseRegExp filter)
Returns a clone of the object (but without the listeners).Object
getObject(String name)
Returns the store value.Object
getObject(String name, Object defValue)
Returns the store value.boolean
has(String name)
Checks whether a variable is stored or not.boolean
hasVariableChangeListener(VariableChangeListener l)
Checks whether the listener is in the internal list.boolean
isObject(String name)
Returns whether the stored value is present as non-string object.static boolean
isPlaceholder(String s)
Checks whether the string represents a variable placeholder, i.e., it starts with "@{" and ends with "}".static boolean
isValidName(String s)
Checks whether the string represents a valid name (without the "@{" and "}").Enumeration<String>
names()
Returns an enumeration over the names of the variables.Set<String>
nameSet()
Returns a set containing the names of the variables.protected void
notifyVariableChangeListeners(VariableChangeEvent e)
Notifies all listeners with the specified event.static String
padName(String name)
Surrounds the variable name with "@{" and "}", if necessary.boolean
remove(BaseRegExp regExp)
Removes variables that match a regular expressions.String
remove(String name)
Removes the variable.void
removeVariableChangeListener(VariableChangeListener l)
Removes the listener from the internal list.void
removeVariableChangeListeners()
Removes all listeners from the internal list.void
set(String name, String value)
Stores the value for the variable.int
size()
Returns the number of variables currently stored.String
toString()
Simply returns the internal hashtable of variable/value pairs.static String
toValidName(String s)
Turns any string (with surrounding @{...}) into a valid variable name, by replacing invalid characters with underscores ("_").static String
toValidName(String s, String replace)
Turns any string (with surrounding @{...}) into a valid variable name, by replacing invalid characters with the specified replacement string.-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
-
-
-
Field Detail
-
START
public static final String START
the start of a variable.- See Also:
- Constant Field Values
-
END
public static final String END
the end of a variable.- See Also:
- Constant Field Values
-
CHARS
public static final String CHARS
allowed characters.- See Also:
- Constant Field Values
-
ENVIRONMENT_VARIABLE_PREFIX
public static final String ENVIRONMENT_VARIABLE_PREFIX
the prefix for environment variables.- See Also:
- Constant Field Values
-
SYSTEM_PROPERTY_PREFIX
public static final String SYSTEM_PROPERTY_PREFIX
the prefix for system properties.- See Also:
- Constant Field Values
-
m_EnvironmentVariables
protected Hashtable<String,String> m_EnvironmentVariables
the environment variables <-> value relation.
-
m_SystemProperties
protected Hashtable<String,String> m_SystemProperties
the system properties <-> value relation.
-
m_VariableChangeListeners
protected WrapperHashSet<VariableChangeListener> m_VariableChangeListeners
the listeners.
-
-
Method Detail
-
clear
public void clear()
Removes all currently stored variables.
-
names
public Enumeration<String> names()
Returns an enumeration over the names of the variables.- Returns:
- the names
-
nameSet
public Set<String> nameSet()
Returns a set containing the names of the variables.- Returns:
- the names
-
set
public void set(String name, String value)
Stores the value for the variable.- Parameters:
name
- the name (or placeholder string) of the variablevalue
- the value of the variable
-
has
public boolean has(String name)
Checks whether a variable is stored or not.- Parameters:
name
- the name (or placeholder string) of the variable- Returns:
- true if the variable is stored
-
get
public String get(String name)
Returns the stored value if present, otherwise null.- Parameters:
name
- the name (or placeholder string) of the variable- Returns:
- the associated value or null if not found
-
remove
public String remove(String name)
Removes the variable.- Parameters:
name
- the name (or placeholder string) of the variable- Returns:
- the previously stored value
-
remove
public boolean remove(BaseRegExp regExp)
Removes variables that match a regular expressions.- Parameters:
regExp
- the regular expression to match the name of the variables against- Returns:
- true if at least one was removed
-
get
public String get(String name, String defValue)
Returns the stored value if present, otherwise the default value.- Parameters:
name
- the name (or placeholder string) of the variabledefValue
- the default value, in case the variable is not stored- Returns:
- the associated value
-
isObject
public boolean isObject(String name)
Returns whether the stored value is present as non-string object.
Default implementation always returns false.- Returns:
- true if the value is stored as non-string
- See Also:
getObject(String)
-
getObject
public Object getObject(String name)
Returns the store value.- Parameters:
name
- the name of the value- Returns:
- the value referenced by the name, null if not available
- See Also:
getObject(String,Object)
-
getObject
public Object getObject(String name, Object defValue)
- Parameters:
name
- the name of the valuedefValue
- the default value to use if value not present- Returns:
- the value referenced by the name, defValue if not available
-
size
public int size()
Returns the number of variables currently stored.- Returns:
- the number of variables
-
hasVariableChangeListener
public boolean hasVariableChangeListener(VariableChangeListener l)
Checks whether the listener is in the internal list.- Parameters:
l
- the listener to check- Returns:
- true if already in the list
-
addVariableChangeListener
public void addVariableChangeListener(VariableChangeListener l)
Adds the listener to the internal list.- Parameters:
l
- the listener to add
-
removeVariableChangeListener
public void removeVariableChangeListener(VariableChangeListener l)
Removes the listener from the internal list.- Parameters:
l
- the listener to remove
-
removeVariableChangeListeners
public void removeVariableChangeListeners()
Removes all listeners from the internal list.
-
notifyVariableChangeListeners
protected void notifyVariableChangeListeners(VariableChangeEvent e)
Notifies all listeners with the specified event.- Parameters:
e
- the event to send
-
extractName
public static String extractName(String variable)
Extracts the name of a variable from the placeholder string, i.e., it strips the "@{" and "}" from the string. Does nothing if already stripped.
-
extractNames
public static String[] extractNames(String expr)
Extracts all the variables from the given expression.- Parameters:
expr
- the expression to parse for variable names- Returns:
- the variable names located in the expression
-
padName
public static String padName(String name)
Surrounds the variable name with "@{" and "}", if necessary.
-
isPlaceholder
public static boolean isPlaceholder(String s)
Checks whether the string represents a variable placeholder, i.e., it starts with "@{" and ends with "}".- Parameters:
s
- the string to check- Returns:
- true if the string represents a variable placeholder string
-
isValidName
public static boolean isValidName(String s)
Checks whether the string represents a valid name (without the "@{" and "}").- Parameters:
s
- the name to check- Returns:
- true if valid
-
createValidName
public static String createValidName(String s)
Creates a valid variable name.- Parameters:
s
- the name to turn into a valid variable name- Returns:
- the processed name
-
toValidName
public static String toValidName(String s)
Turns any string (with surrounding @{...}) into a valid variable name, by replacing invalid characters with underscores ("_").- Parameters:
s
- the string to convert into a valid variable name- Returns:
- the (potentially) fixed name
-
toValidName
public static String toValidName(String s, String replace)
Turns any string (with surrounding @{...}) into a valid variable name, by replacing invalid characters with the specified replacement string.- Parameters:
s
- the string to convert into a valid variable namereplace
- the replacement string for invalid chars- Returns:
- the (potentially) fixed name
-
expand
public String expand(String s)
Replaces all variables in the string with the currently stored values.- Parameters:
s
- the string to process- Returns:
- the processed string
-
doExpandEnv
protected String doExpandEnv(String s)
Expands environment variables.- Parameters:
s
- the string to expand- Returns:
- the potentially expanded string
-
doExpandSystemProps
protected String doExpandSystemProps(String s)
Expands system properties.- Parameters:
s
- the string to expand- Returns:
- the potentially expanded string
-
doExpandRegular
protected String doExpandRegular(String s)
Expands regular variables.- Parameters:
s
- the string to expand- Returns:
- the potentially expanded string
-
doExpand
protected String doExpand(String s)
Performs all expansions.- Parameters:
s
- the string to expand- Returns:
- the potentially expanded string
-
expand
protected String expand(String s, boolean recurse)
Replaces all variables in the string with the currently stored values.- Parameters:
s
- the string to processrecurse
- whether to recurse, i.e., replacing "@{@{"- Returns:
- the processed string
-
cleanUp
public void cleanUp()
Cleans up data structures, frees up memory.- Specified by:
cleanUp
in interfaceCleanUpHandler
-
getClone
public Variables getClone()
Returns a clone of the object (but without the listeners).- Specified by:
getClone
in interfaceCloneHandler<Variables>
- Returns:
- the clone, null if failed
-
getClone
public Variables getClone(BaseRegExp filter)
Returns a clone of the object (but without the listeners).- Parameters:
filter
- the regular expression that the variable names must match, null to ignore- Returns:
- the clone, null if failed
-
assign
public void assign(Variables other)
Adds all the variables from the other Variables object (overwrites any existing ones).- Parameters:
other
- the Variables to copy
-
assign
public void assign(Variables other, BaseRegExp filter)
Adds all the variables from the other Variables object (overwrites any existing ones).- Parameters:
other
- the Variables to copyfilter
- the regular expression that the variable names must match, null to ignore
-
-