Package adams.parser
Class ParserHelper
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.parser.ParserHelper
-
- All Implemented Interfaces:
LoggingSupporter
,SizeOfHandler
,Serializable
- Direct Known Subclasses:
ParserHelper
,ParserHelper
public class ParserHelper extends LoggingObject
Helper class for parsers.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected BusinessDays
m_BusinessDays
the business days to use.protected Calendar
m_Calendar
the calendar instance to use.protected static HashMap<String,ParserFunction>
m_Functions
the cache of functions.protected static HashMap<String,ParserProcedure>
m_Procedures
the cache of procedures.protected HashMap
m_Symbols
the symbols.-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Constructor Description ParserHelper()
Initializes the helper.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Date
add(Date d, Double n, TimeAmount t)
Adds the request amount of time.protected TimeAmount
adjustAmount(Date d, Double n, TimeAmount t)
Corrects the amount using the factor and the note in the amount.Object
callFunction(String name, Object[] params)
Call function.void
callProcedure(String name, Object[] params)
Call procedure.Double
compare(Object o1, Object o2)
Compares two objects.BusinessDays
getBusinessDays()
Returns the type of business days to use.Calendar
getCalendar()
Returns the calendar instance to use.Calendar
getCalendar(Date date)
Returns the calendar instance to use.Double
getDateFieldFromString(Object obj, int field)
Returns the specified date from the object (if it is a String or Date object).protected static ParserFunction
getFunction(String name)
Returns the function associated with the given function name.static String
getFunctionOverview()
Builds a string of all the additionally available functions.protected static ParserProcedure
getProcedure(String name)
Returns the procedure associated with the given procedure name.static String
getProcedureOverview()
Builds a string of all the additionally available procedures.HashMap
getSymbols()
Returns the currently used symbols.protected static void
initFunctions()
Initializes the additional functions.protected void
initialize()
Initializes members.protected static void
initProcedures()
Initializes the additional procedures.boolean
isBusinessDay(Object obj)
Checks whether the string/date represents a business day.String
left(String str, int len)
Returns the specified number of characters from the left of the string.String
mid(String str, int pos, int len)
Returns the specified substring from the string.String
repeat(String str, int num)
Generates a string made up of multiple copies of a string.String
replace(String str, int pos, int len, String newStr)
Replaces a sub-string at a specified location with a new string.static HashMap
reportToSymbols(Report report)
Turns the content of the report into symbols.String
right(String str, int len)
Returns the specified number of characters from the right of the string.void
setBusinessDays(BusinessDays value)
Sets the type of business days to use.void
setSymbols(HashMap value)
Sets the symbols in use.String
substitute(String str, String find, String replace)
Substitutes a occurrences of a string with a replacement string.String
substitute(String str, String find, String replace, int occurrences)
Substitutes a occurrences of a string with a replacement string.Boolean
toBoolean(Object obj)
Turns the object into a boolean.Date
toDate(String s)
Turns the string into a Date object.Double
toDouble(Object obj)
Turns the object into a double.Integer
toInteger(Object obj)
Turns the object (ie double) into an integer.Double
toNumber(Object obj)
Turns the object into a double.String
toString(Object obj)
Turns the object into a string.-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
-
-
-
Field Detail
-
m_Symbols
protected HashMap m_Symbols
the symbols.
-
m_Calendar
protected Calendar m_Calendar
the calendar instance to use.
-
m_BusinessDays
protected BusinessDays m_BusinessDays
the business days to use.
-
m_Functions
protected static HashMap<String,ParserFunction> m_Functions
the cache of functions.
-
m_Procedures
protected static HashMap<String,ParserProcedure> m_Procedures
the cache of procedures.
-
-
Method Detail
-
reportToSymbols
public static HashMap reportToSymbols(Report report)
Turns the content of the report into symbols.- Parameters:
report
- the report- Returns:
- the evaluated result
-
initialize
protected void initialize()
Initializes members.
-
setSymbols
public void setSymbols(HashMap value)
Sets the symbols in use.- Parameters:
value
- the symbols
-
getSymbols
public HashMap getSymbols()
Returns the currently used symbols.- Returns:
- the symbols
-
getCalendar
public Calendar getCalendar()
Returns the calendar instance to use.- Returns:
- the calendar
-
getCalendar
public Calendar getCalendar(Date date)
Returns the calendar instance to use.- Parameters:
date
- the date to initialize the calendar with- Returns:
- the calendar
-
setBusinessDays
public void setBusinessDays(BusinessDays value)
Sets the type of business days to use.- Parameters:
value
- the type
-
getBusinessDays
public BusinessDays getBusinessDays()
Returns the type of business days to use.- Returns:
- the type
-
adjustAmount
protected TimeAmount adjustAmount(Date d, Double n, TimeAmount t)
Corrects the amount using the factor and the note in the amount.- Parameters:
t
- the amount to correct- Returns:
- the (potentially) corrected amount
-
add
public Date add(Date d, Double n, TimeAmount t)
Adds the request amount of time.- Parameters:
d
- the current daten
- the factort
- the time amount to use- Returns:
- the new date
-
getDateFieldFromString
public Double getDateFieldFromString(Object obj, int field)
Returns the specified date from the object (if it is a String or Date object).- Parameters:
obj
- the object to extract the date field fromfield
- the date field (see constants of Calendar class)- Returns:
- the value or NaN if failed to convert
-
isBusinessDay
public boolean isBusinessDay(Object obj)
Checks whether the string/date represents a business day.- Parameters:
obj
- the string/date to check- Returns:
- true if business day
- See Also:
getBusinessDays()
-
toNumber
public Double toNumber(Object obj)
Turns the object into a double.- Parameters:
obj
- the object to convert- Returns:
- the numeric value, NaN if not a number
-
toDouble
public Double toDouble(Object obj)
Turns the object into a double.- Parameters:
obj
- the object to convert- Returns:
- the numeric value, NaN if not a double
-
toInteger
public Integer toInteger(Object obj)
Turns the object (ie double) into an integer.- Parameters:
obj
- the object to convert- Returns:
- the numeric value, MAX_VALUE if not a double
-
toBoolean
public Boolean toBoolean(Object obj)
Turns the object into a boolean.- Parameters:
obj
- the object to convert- Returns:
- the boolean, false if not a boolean
-
toString
public String toString(Object obj)
Turns the object into a string.- Parameters:
obj
- the object to convert- Returns:
- the string representation
-
toDate
public Date toDate(String s)
Turns the string into a Date object.- Parameters:
s
- the string to convert- Returns:
- the Date object, null if failed to convert
-
compare
public Double compare(Object o1, Object o2)
Compares two objects.- Parameters:
o1
- the first objecto2
- the second object- Returns:
- the comparison, NaN if failed to compare
-
substitute
public String substitute(String str, String find, String replace)
Substitutes a occurrences of a string with a replacement string.- Parameters:
str
- the string to processfind
- the string to replacereplace
- the replacement string
-
repeat
public String repeat(String str, int num)
Generates a string made up of multiple copies of a string.- Parameters:
str
- the string to copy multiple timesnum
- the number of times to copy- Returns:
- the generated string
-
replace
public String replace(String str, int pos, int len, String newStr)
Replaces a sub-string at a specified location with a new string.- Parameters:
str
- the string to updatepos
- the starting position (0-based)len
- the length of the substring to replacenewStr
- the new String to insert- Returns:
- the updated string
-
substitute
public String substitute(String str, String find, String replace, int occurrences)
Substitutes a occurrences of a string with a replacement string.- Parameters:
str
- the string to processfind
- the string to replacereplace
- the replacement stringoccurrences
- the maximum number of occurrences, use <= 0 to ignore
-
left
public String left(String str, int len)
Returns the specified number of characters from the left of the string. If string is shorter than specified length, all of the string is returned.- Parameters:
str
- the source stringlen
- the number of characters- Returns:
- the substring
-
mid
public String mid(String str, int pos, int len)
Returns the specified substring from the string. If the start position is greater than the string's length itself, then an empty string is returned. If (pos+len) exceeds the string's length, then only the available substring is returned.- Parameters:
str
- the source stringpos
- the starting position (0-based)len
- the number of characters to copy- Returns:
- the substring
-
right
public String right(String str, int len)
Returns the specified number of characters from the right of the string. If string is shorter than specified length, all of the string is returned.- Parameters:
str
- the source stringlen
- the number of characters- Returns:
- the substring
-
initFunctions
protected static void initFunctions()
Initializes the additional functions.
-
getFunction
protected static ParserFunction getFunction(String name)
Returns the function associated with the given function name.- Parameters:
name
- the name of the function- Returns:
- the instance of the function
- Throws:
IllegalArgumentException
- if the function name is not available
-
getFunctionOverview
public static String getFunctionOverview()
Builds a string of all the additionally available functions.- Returns:
- the generated overview
-
callFunction
public Object callFunction(String name, Object[] params)
Call function.- Parameters:
name
- the name of the functionparams
- the parameters of the function- Returns:
- the result of the function call
-
initProcedures
protected static void initProcedures()
Initializes the additional procedures.
-
getProcedure
protected static ParserProcedure getProcedure(String name)
Returns the procedure associated with the given procedure name.- Parameters:
name
- the name of the procedure- Returns:
- the instance of the procedure
- Throws:
IllegalArgumentException
- if the procedure name is not available
-
getProcedureOverview
public static String getProcedureOverview()
Builds a string of all the additionally available procedures.- Returns:
- the generated overview
-
-