Class BitwiseExpression


  • public class BitwiseExpression
    extends java.lang.Object
    This is generic utility class for handling Bitwise operations. Currently it handles only | and &. Precedence is controlled by paranthesis,(). This utility uses a "stack" based approach and computes the expression of the result while building the stack. Expression is parsed from the left-right and executed from right-left Expression consists of operands and operators. sample expression eg. EDIT|MODFIY&DELETE Operators: This utility currently supports | and & operators and will be later expanded to handle even !(NOT). Operands : Operand can be any string literal (EDIT,MODIFY,DELETE) which can be evaluated to boolean result by the registered operandEvaluator. Precedence : Precedence of the expression can be controlled by parenthesis(). If they are not specified expression is executed from right to left. EDIT|MODFIY&DELETE will be treated as EDIT|(MODFIY&DELETE). Valid Expressions: X - single operand expression X|Y|Z , X|Y&Z - precedence is controlled from right-left (X|Y)&Z - precedence is controlled by ( ,) InValid Expressions: X&(Y|Z - MalformedException - "no closed paranthesis" X&Y| - MalformedException -"Expecting operand after operator |" OperandEvaluator: With the main design goal of keeping this utility generic , logic of computing the boolean value of operand (where EDIT bit is set ?) is pushed to clients. clients need to implement BitwiseOperandEvaluator interface and implement evaluate function. If clients encountera operand variable for which boolean value cannot be fetched , it will throw InvalidOperandExceeption. Expression X&A is inValid because value of operand "A" cannot be fetched
    • Constructor Summary

      Constructors 
      Constructor Description
      BitwiseExpression​(BitwiseOperandEvaluator operandEval)
      creates a BitwiseExpressionEvaluator With the main design goal of keeping this class as generic as possible, logic of fetching each operand is decouple from this class.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean evaluateExpression​(java.lang.String expression)
      Builds and Evaluates the given Bitwise Expression ,E.g x & (y | z) parses the expression for operands and operators and pushes them in to two seperate stacks.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BitwiseExpression

        public BitwiseExpression​(BitwiseOperandEvaluator operandEval)
        creates a BitwiseExpressionEvaluator With the main design goal of keeping this class as generic as possible, logic of fetching each operand is decouple from this class. Instead it will be implemented by clients and specified through this argument.
    • Method Detail

      • evaluateExpression

        public boolean evaluateExpression​(java.lang.String expression)
                                   throws MalformedExpressionException,
                                          InvalidOperandException
        Builds and Evaluates the given Bitwise Expression ,E.g x & (y | z) parses the expression for operands and operators and pushes them in to two seperate stacks. Fetches the value of each operand by calling back the BitwiseOperandEvaluator , which is set during construction. Evaluates the expresssion with the () as precedence.
        Throws:
        MalformedExpressionException
        InvalidOperandException