infix2postfix
Class PostfixEvaluator

java.lang.Object
  extended byinfix2postfix.PostfixEvaluator
Direct Known Subclasses:
CollectionPostfixEvaluator, StringPostfixEvaluator

public abstract class PostfixEvaluator
extends java.lang.Object

This object represents an abstraction to any postfix expression evaluator.

Author:
Maulin H. Vasavada

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

OPERATOR

protected static final int OPERATOR
Identifier to indicate if a symbol in postfix expression is an operator.

See Also:
Constant Field Values

OPERAND

protected static final int OPERAND
Identifier to indicate if a symbol in postfix expression is an operand.

See Also:
Constant Field Values

inputStack

protected java.util.Stack inputStack
Stack prepared from the infix to postfix conversion of an expression.


outputStack

protected java.util.Stack outputStack
Stack that is used to push operands on while executing the postfix expression.


operators

protected java.util.HashMap operators
Map of valid operators and their symbols.

See Also:
Operator

operandManager

protected OperandManager operandManager
Operand Manager object reference.

See Also:
OperandManager
Constructor Detail

PostfixEvaluator

protected PostfixEvaluator(OperandManager operandManager)
Constructor

Method Detail

isOperator

private boolean isOperator(java.lang.Object symbol)
Verifies if a symbol given is a valid operator. Criteria for "valid operator" is,
Given symbols must be in the map of "valid operators".


isOperand

private boolean isOperand(java.lang.Object symbol)
Verifies if a given symbol is an operand. Criteria for "valid operand" is,
Given symbol must not be operator AND
Given symbol must be an operand by definition of OperandManger.isOperand() method on the associated operand manager with this evaluator object.

See Also:
isOperator(Object), OperandManager.isOperand(Object)

setOperandManager

protected void setOperandManager(OperandManager operandManager)
Sets operand manager for this evaluator object.


addOperator

public boolean addOperator(Operator operator)
Adds an operator to the Map of valid Operators and their symbols.


removeOperator

public boolean removeOperator(java.lang.Object operator)
Removes an operator from the Map of valid Operators and their symbols.


getOperators

public java.util.HashMap getOperators()
Returns map of valid operators and their symbols.


validate

public static boolean validate(PostfixEvaluator pe,
                               java.lang.Object postfixExpression)
                        throws InvalidExpressionFormatException
Validates given postfix expression for given pe.

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.

Parameters:
pe - Postfix evaluator for which the expression is to be validated
postfixExpression - Postfix expression to be validated
Returns:
boolean true if the expression is valid;false otherwise
Throws:
InvalidExpressionFormatException - If the postfixExpression format can't be handled by the pe

populateInputStack

protected abstract void populateInputStack(java.lang.Object postfixExpression)
                                    throws InvalidExpressionFormatException
Abstracts the populating input stack operation from the postfix expression. This is to allow specific subclasses for handling different postfix formats.

Throws:
InvalidExpressionFormatException - If the expression format can't be handled by this evaluator
See Also:
StringPostfixEvaluator.populateInputStack(Object), CollectionPostfixEvaluator.populateInputStack(Object)

checkAndPopulateInputStack

protected void checkAndPopulateInputStack(java.lang.Object postfixExpression)
                                   throws InvalidExpressionFormatException
Populates inputStack for this evaluator if its empty.

Parameters:
postfixExpression - Postfix expression for this evaluator
Throws:
InvalidExpressionFormatException - If the expression format can't be handled by this evaluator

evaluate

protected java.lang.Object evaluate(java.lang.Object operand,
                                    UnaryOperator operator)
                             throws InvalidOperandException
Evaluates a unary operator. When a unary operator is found on stack this method evalutes it via "callback" to the operator.applyOperator() passing appropriate operands.

Throws:
InvalidOperandException
See Also:
UnaryOperator.applyOperator(Object)

evaluate

protected java.lang.Object evaluate(java.lang.Object operand1,
                                    java.lang.Object operand2,
                                    BinaryOperator operator)
                             throws InvalidOperandException
Evaluates a binary operator. When a binary operator is found on stack this method evalutes it via "callback" to the operator.applyOperator() passing appropriate operands.

Throws:
InvalidOperandException
See Also:
BinaryOperator.applyOperator(Object,Object)

evaluate

public java.lang.Object evaluate(java.lang.Object postfixExpression)
                          throws InvalidExpressionFormatException,
                                 InvalidOperatorException,
                                 InvalidOperandException,
                                 InvalidSymbolTypeException,
                                 InvalidPostfixExpressionException
Evaluates given postfix expression. Here the postfixExpression is assumed to be in the format that this evaluator can evaluate. If it doesn't match the expected format then InvalidExpressionFormatException is thrown.

Returns:
Object Result of the postfix evaluation
Throws:
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
See Also:
StringPostfixEvaluator, CollectionPostfixEvaluator, isOperator(Object), OperandManager.isOperand(Object)