|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object infix2postfix.PostfixEvaluator
This object represents an abstraction to any postfix expression evaluator.
Field Summary | |
protected java.util.Stack |
inputStack
Stack prepared from the infix to postfix conversion of an expression. |
protected static int |
OPERAND
Identifier to indicate if a symbol in postfix expression is an operand. |
protected OperandManager |
operandManager
Operand Manager object reference. |
protected static int |
OPERATOR
Identifier to indicate if a symbol in postfix expression is an operator. |
protected java.util.HashMap |
operators
Map of valid operators and their symbols. |
protected java.util.Stack |
outputStack
Stack that is used to push operands on while executing the postfix expression. |
Constructor Summary | |
protected |
PostfixEvaluator(OperandManager operandManager)
Constructor |
Method Summary | |
boolean |
addOperator(Operator operator)
Adds an operator to the Map of valid Operators and their symbols. |
protected void |
checkAndPopulateInputStack(java.lang.Object postfixExpression)
Populates inputStack for this evaluator if its empty. |
java.lang.Object |
evaluate(java.lang.Object postfixExpression)
Evaluates given postfix expression. |
protected java.lang.Object |
evaluate(java.lang.Object operand1,
java.lang.Object operand2,
BinaryOperator operator)
Evaluates a binary operator. |
protected java.lang.Object |
evaluate(java.lang.Object operand,
UnaryOperator operator)
Evaluates a unary operator. |
java.util.HashMap |
getOperators()
Returns map of valid operators and their symbols. |
private boolean |
isOperand(java.lang.Object symbol)
Verifies if a given symbol is an operand. |
private boolean |
isOperator(java.lang.Object symbol)
Verifies if a symbol given is a valid operator. |
protected abstract void |
populateInputStack(java.lang.Object postfixExpression)
Abstracts the populating input stack operation from the postfix expression. |
boolean |
removeOperator(java.lang.Object operator)
Removes an operator from the Map of valid Operators and their symbols. |
protected void |
setOperandManager(OperandManager operandManager)
Sets operand manager for this evaluator object. |
static boolean |
validate(PostfixEvaluator pe,
java.lang.Object postfixExpression)
Validates given postfix expression for given pe. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected static final int OPERATOR
protected static final int OPERAND
protected java.util.Stack inputStack
protected java.util.Stack outputStack
protected java.util.HashMap operators
Operator
protected OperandManager operandManager
OperandManager
Constructor Detail |
protected PostfixEvaluator(OperandManager operandManager)
Method Detail |
private boolean isOperator(java.lang.Object symbol)
private boolean isOperand(java.lang.Object symbol)
OperandManger.isOperand()
method on the associated
operand manager with this evaluator object.
isOperator(Object)
,
OperandManager.isOperand(Object)
protected void setOperandManager(OperandManager operandManager)
public boolean addOperator(Operator operator)
public boolean removeOperator(java.lang.Object operator)
public java.util.HashMap getOperators()
public static boolean validate(PostfixEvaluator pe, java.lang.Object postfixExpression) throws InvalidExpressionFormatException
A postfix expression is validated as per following algorithm,
count =0; while( !inputStack.empty() ) { if ( symbolOnStack is operand ) { count++; } else if ( symbolOnStack is operator ) { count--; } else { // Invalid expression // return } } if ( count != 1 ) { // Invalid expression } else { // Valid expression }
pe holds the specification for valid operands and operators that is used by this method to classify symbols on stack as operand or operator.
pe
- Postfix evaluator for which the expression is to be validatedpostfixExpression
- Postfix expression to be validated
true
if the expression is valid;false
otherwise
InvalidExpressionFormatException
- If the postfixExpression
format can't be handled by the peprotected abstract void populateInputStack(java.lang.Object postfixExpression) throws InvalidExpressionFormatException
InvalidExpressionFormatException
- If the expression format
can't be handled by this evaluatorStringPostfixEvaluator.populateInputStack(Object)
,
CollectionPostfixEvaluator.populateInputStack(Object)
protected void checkAndPopulateInputStack(java.lang.Object postfixExpression) throws InvalidExpressionFormatException
inputStack
for this evaluator if its empty.
postfixExpression
- Postfix expression for this evaluator
InvalidExpressionFormatException
- If the expression format
can't be handled by this evaluatorprotected java.lang.Object evaluate(java.lang.Object operand, UnaryOperator operator) throws InvalidOperandException
operator.applyOperator()
passing
appropriate operands.
InvalidOperandException
UnaryOperator.applyOperator(Object)
protected java.lang.Object evaluate(java.lang.Object operand1, java.lang.Object operand2, BinaryOperator operator) throws InvalidOperandException
operator.applyOperator()
passing
appropriate operands.
InvalidOperandException
BinaryOperator.applyOperator(Object,Object)
public java.lang.Object evaluate(java.lang.Object postfixExpression) throws InvalidExpressionFormatException, InvalidOperatorException, InvalidOperandException, InvalidSymbolTypeException, InvalidPostfixExpressionException
InvalidExpressionFormatException
is thrown.
InvalidExpressionFormatException
- If the expression format
can't be handled by this evaluator
InvalidOperatorException
- If an invalid operator is
encountered in the postfix expression
InvalidOperandException
- If an invalid operand is
encountered in the postfix expression
InvalidSymbolTypeException
- If a symbol that is neither a
valid operator nor a valid operand is encountered in the
postfix expression
InvalidPostfixExpressionException
StringPostfixEvaluator
,
CollectionPostfixEvaluator
,
isOperator(Object)
,
OperandManager.isOperand(Object)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |