Package parser

Class Bracket


public class Bracket extends Operator
  • Field Details

    • name

      private transient String name
      the name of the bracket i.e "(" or ")"
    • index

      private int index
      The index of the bracket in the ArrayList containing the scanned function
    • complement

      private transient Bracket complement
      objects of this class keep a record of their counterpart or complementing bracket.
    • evaluated

      private boolean evaluated
      Return true if the contents of the bracket have been evaluated
  • Constructor Details

    • Bracket

      public Bracket(String op)
      Constructor of this class for creating its objects and initializing their names with either a ( or a ) and initial
      Parameters:
      op -
  • Method Details

    • setEvaluated

      public void setEvaluated(boolean evaluated)
      Parameters:
      evaluated - set whether or not this bracket's contents have been evaluated
    • isEvaluated

      public boolean isEvaluated()
      Returns:
      true if this bracket's contents have been evaluated
    • createTwinBracket

      public static Bracket createTwinBracket(Bracket brac)
      Used to create similar objects that are not equal The object created by this class is similar to the parameter because it contains the same data as the parameter. However,its address in memory is different because it refers to an entirely different object of the same class,but having similar attributes. How can this method be of any use? Imagine an Array of Brackets say array bracs filled with Bracket objects. If we create another Bracket array, say array moreBracs and copy the objects in bracs into moreBracs.Now, both bracs and moreBracs will hold references to these Bracket objects in memory.Java will not create new, similar objects at another address in memory and store in the new array. The command was most likely moreBracs=bracs; or in a loop, it would look like: for(int i=0;i<bracs.length;i++){ moreBracs=bracs[i]; } These statements will only ensure that both arrays will hold a reference to the same objects in memory,i.e RAM. Hence whenever an unsuspecting coder modifies the contents of bracs, thinking He/She has a backup in moreBracs,Java is effecting the modification on the objects referred to by moreBracs, too.This can cause a serious logical error in applications. To stop this, we use this method in this way: for(int i=0;i<bracs.length;i++){ moreBracs[i]=createTwinBracket(bracs[i]); } Note that this can be applied to all storage objects too e.g Collection objects and so on.
      Parameters:
      brac - The object whose twin we wish to create.
      Returns:
      a Bracket object that manifests exactly the same attributes as brac but is a distinct object from brac.
    • createTwinBracket

      public Bracket createTwinBracket()
      non-static version of the above method. This one creates a twin for this Bracket object. The one above creates a twin for the specified bracket object.
      Returns:
      a Bracket object that manifests exactly the same attributes as brac but is a distinct object from brac.
    • getIndex

      public int getIndex()
      Returns:
      the index of this Bracket object in a scanned function
    • setIndex

      public void setIndex(int index)
      Parameters:
      index - the ne w index to assign to this Bracket object in a scanned Function
    • getName

      public String getName()
      Overrides:
      getName in class Operator
      Returns:
      the name of this Bracket either ( or )
    • setName

      public void setName(String name)
      Overrides:
      setName in class Operator
      Parameters:
      name - sets the name of this bracket to either ( or )
    • getComplement

      public Bracket getComplement()
      Returns:
      the Bracket object which is the complement of this Bracket object
    • setComplement

      public void setComplement(Bracket complement)
      Parameters:
      complement - sets the Bracket object which is to be the complement to this one in the scanned Function
    • isComplement

      public boolean isComplement(Bracket brac)
      checks if the Bracket object argument below is the sane as the complement to this Bracket object.
      Parameters:
      brac - The Bracket object whose identity is to be checked whether or not it complements this Bracket object.
      Returns:
      true if the parameter is the complement to this one.
    • encloses

      public boolean encloses(Bracket brac)
      Parameters:
      brac - the bracket to be checked if or not it is enclosed by this bracket and its complement.
      Returns:
      true if the bracket is enclosed by this bracket and its counterpart.
    • getNumberOfInternalBrackets

      public int getNumberOfInternalBrackets(ArrayList<Bracket> brac)
      Parameters:
      brac - an ArrayList object containing all brackets found in a function
      Returns:
      the number of bracket pairs contained between this Bracket object and its complement
    • isSBP

      public boolean isSBP(ArrayList<String> scan)
      Parameters:
      scan - The ArrayList object containing the scanned function.
      Returns:
      true if this Bracket object forms with its complement, a single bracket pair that is a bracket pair containing no other bracket pairs.
    • getComplementIndex

      public static int getComplementIndex(boolean isOpenBracket, int start, ArrayList<String> scan)
      Parameters:
      isOpenBracket - boolean variable that should be true if this bracket object whose complement we seek is an opening bracket i.e (, and should be set to false if this bracket object whose complement we seek is a closing bracket i.e )
      start - the index of the given bracket.
      scan - the ArrayList containing the scanned function.
      Returns:
      the index of the enclosing or complement bracket of this bracket object
    • getComplementIndex

      public static int getComplementIndex(boolean isOpenBracket, int start, List<String> scan)
      Parameters:
      isOpenBracket - boolean variable that should be true if this bracket object whose complement we seek is an opening bracket i.e (, and should be set to false if this bracket object whose complement we seek is a closing bracket i.e )
      start - the index of the given bracket.
      scan - the ArrayList containing the scanned function.
      Returns:
      the index of the enclosing or complement bracket of this bracket object
    • getComplementIndex

      public static int getComplementIndex(boolean isOpenBracket, int start, String expr)
      Parameters:
      isOpenBracket - boolean variable that should be true if this bracket object whose complement we seek is an opening bracket i.e (, and should be set to false if this bracket object whose complement we seek is a closing bracket i.e )
      start - the index of the given bracket.
      expr - the function string containing the brackets.
      Returns:
      the index of the enclosing or complement bracket of this bracket object
    • checkBracketStructure

      public static boolean checkBracketStructure(List<String> list, int start, int end)
      Parameters:
      list - The list containing the scanned math expression.
      start - The point in the list where this algorithm should start checking the bracket syntax.(inclusive)
      end - The point in the list where this algorithm should stop checking the bracket syntax.(inclusive)
      Returns:
      true if the bracket syntax of the scanned expression in the given range is valid or the expression in the given range is devoid of brackets.
    • hasBracketsInRange

      public static boolean hasBracketsInRange(List<String> list, int start, int end)
      Parameters:
      list - The list containing the scanned math expression.
      start - The point in the list where this algorithm should start checking for brackets.(inclusive)
      end - The point in the list where this algorithm should stop checking for brackets.(inclusive)
      Returns:
      true if the scanned expression contains no brackets in the given range.
    • isOpenBracket

      public static boolean isOpenBracket(String bracket)
      Parameters:
      bracket - The String object.
      Returns:
      true if the String object represents an open bracket
    • isCloseBracket

      public static boolean isCloseBracket(String bracket)
      Parameters:
      bracket - The String object.
      Returns:
      true if the String object represents a close bracket
    • simpleBracketPairHasVariables

      public boolean simpleBracketPairHasVariables(ArrayList<String> scan)
      Parameters:
      scan - The ArrayList containing the scanned function inside which this Bracket exists.
      Returns:
      true if between this Bracket and its complement, a Variable object is found.
    • getDomainContents

      public String getDomainContents(ArrayList<String> scan)
      Parameters:
      scan - The ArrayList object containing the scanned function.
      Returns:
      The contents of this bracket and its complement as a string, the bracket and its complement are also returned. e.g in 5+(2+3-sin2).. This method will return (2+3-sin2).
    • getBracketDomainContents

      public List<String> getBracketDomainContents(ArrayList<String> scan)
      returns a List containing the contents of a bracket pair,including the bracket pair itself.
      Parameters:
      scan - the ArrayList containing the scanner output for a Function
      Returns:
      the bracket pair and its contents.
    • validateBracketStructure

      private static boolean validateBracketStructure(List<String> scanner)
      method mapBrackets goes over an input equation and maps all positions that have corresponding brackets
      Parameters:
      scanner - The ArrayList object that contains the scanned math function.
      Returns:
      true if the structure of the bracket is valid.
    • multiplyContentsByMinusOne

      public void multiplyContentsByMinusOne(List<String> scanner)
      Parameters:
      scanner - The ArrayList containing the scanner output for a Function Multiplies the contents of this List by -1.
    • domainTokenAt

      public String domainTokenAt(List<String> scanner, int index)
      Parameters:
      scanner - The ArrayList containing the scanner output for a Function
      index - The index at which the token is to be retrieved. The first and elements are compulsorily always an open bracket and a close bracket respectively.