Class Prior

java.lang.Object
eu.bandm.tools.format.Format
eu.bandm.tools.format.Prior
All Implemented Interfaces:
Formattable, Serializable, Cloneable

public class Prior extends Format
This format is rendered differently, according to its relative positions to ancestor formats of this class. It prints it parens format, if it is (directly or indirectly) contained in a Prior Format with a higher priority level (which is a numerically larger priority code).

If not, but contained in a format with the same priority. a different value of alternative between these formats also selects the parens format. So does a negative alternative value.

Otherwise the noparens format is printed.

So one can automatically diferentiate between

      a+b*c+d   for the expression tree +(a, *(b, c), d)
      (a+b)*c*d                         *(+(a, b), c, d)
      a+(b-c)+d                         +(a, -(b, c), d)
      a-(b+c+d)                         -(a, +(b, c, d))
  

The priority of the outermost level, when initially calling FormatPrinter.startPrinting(Format), is set to zero.

In contrast to most other format classes, the preparatory calculations of the format's width requirements are only heuristical and use the respective maximum values, because the final print context is (naturally) not known.

See Also:
  • Field Details

    • level

      public final int level
      The paren version will be printed if this format is contained (maybe indirectly) in another Prior with higher priority.
    • alternative

      public final int alternative
      The paren version will be printed if this format is contained (maybe indirectly) in another Prior with same priority and different alternative, or if this alternative == -1.
    • noparens

      public final Format noparens
      Format not framed with parentheses.
    • parens

      public final Format parens
      Format framed with parentheses.
  • Constructor Details

    • Prior

      protected Prior(int level, int alternative, Format noparens, Format parens, int indent)
      A constructor with some field values explicit, some calculated from the subformats.
      Parameters:
      level - the binding power of the format: higher numerical value means higher binding power.
      alternative - numeric identifier to distinguish between non-equal operators with the same binding power.
      noparens - the format to print when the priority situation does not require parentheses
      parens - the format to print when the priority situation requires parentheses
      indent - the indentation value
      Throws:
      IllegalArgumentException - if ident value not valid, see Format.checkValidIndent(int)
    • Prior

      protected Prior(int level, int alternative, Format noparens, Format parens)
      Sets fields of the new instance, but the indentation to Format.INDENT_NOT_SET.
      Parameters:
      level - the binding power of the format: higher numerical value means higher binding power.
      alternative - numeric identifier to distinguish between non-equal operators with the same binding power.
      noparens - the format to print when the priority situation does not require parentheses
      parens - the format to print when the priority situation requires parentheses
  • Method Details

    • debugPrefix

      public String debugPrefix(int n)
      Description copied from class: Format
      Deliver the concatenation of the text contents, limited by the given number of characters, approximately. Useful for debugging purpose.
      Specified by:
      debugPrefix in class Format
      Parameters:
      n - the maximal number of characters which can be generated per tree node.
      Returns:
      the concatenated, unformatted text contents.
    • doprint

      protected void doprint(FormatPrinter v)
      Description copied from class: Format
      Over-ridden by all sub-classes of Format, for performing the print operations.
      Specified by:
      doprint in class Format
      Parameters:
      v - realizes the dynamic context for printing
    • indent

      public Prior indent(int i)
      Description copied from class: Format
      Returns a clone of this with the new value of Format.indent.
      Specified by:
      indent in class Format
      Parameters:
      i - the new value for indent.
      Returns:
      an new format with identical values but i, or this, if i is its indent value.
    • eval

      public Format eval(Context c, boolean partial)
      Description copied from class: Format
      Return the result of replacing all variables with the bindings in the given context.
      Overrides:
      eval in class Format
      Parameters:
      c - defines the replacement for variables by name and number.
      partial - decides when a Variable not defined in the context is found: if true the Variable stays aunaltered; if false an IllegalArgumentException is thrown.
      Returns:
      a format with all or some variables replaced