Java Reference
In-Depth Information
The Instance Variables
Instance variables named exp and context are the expression and the ScriptContext to
evaluate the expression, respectively. They are passed to the constructor of this class.
The instance variables named op1 and op2 represent the first and the second
operands in the expression, respectively. The instance variables op1Sign and op2Sign
represent signs, which could be '+' or '-', for the first and the second operands in the
expression, respectively. The operands and their signs are populated when the expression
is parsed using the parse() method.
The instance variable named operation represents an arithmetic operation
( + , - , * , or / )) to be performed on the operands.
The instance variable named parsed is used to keep track of the fact whether the
expression has been parsed or not. The parse() method sets it to true .
The Constructor
The constructor of the Expression class accepts an expression and a ScriptContext . It
makes sure that they are not null and stores them in the instance variables. It trims the
leading and trailing whitespaces from the expression before storing it in the instance
variable named exp .
The parse() Method
The parse() method parses the expression into operands and operations. It uses a
regular expression to parse the expression text. The regular expression expects the
expression text in the following form:
+ or - for the first operand
An optional sign
The first operand that may consist of a combination of
alphanumeric letters, currency signs, underscores, and decimal
points
Any number of whitespaces
+ , - , * , or /
An operation sign that may be
+ or - for the second operand
An optional sign
The second operand that may consist of a combination of
alphanumeric letters, currency signs, underscores, and decimal
points
The regular expression ([+-]?) will match the optional sign for the operand. The
regular expression ([\\p{Alnum}\\p{Sc}_.]+) will match an operand, which may be a
decimal number or a name. The regular expression ([\\s]*) will match any number of
whitespaces. The regular expression ([+*/-]) will match an operation sign. All regular
expressions are enclosed in parentheses to form groups, so you can capture the matched
parts of the expression.
 
Search WWH ::




Custom Search