Interface CalculationReport

All Known Implementing Classes:
DummyCalculationReport, FlexibleCalculationReport, HTMLCalculationReport, SwingCalculationReport, TextCalculationReport

public interface CalculationReport
This represents a report for any typical MegaMek suite calculation such as BV, cost or AS conversion. It assumes that each line will have at most three entries, a sort of header text (Damage:), a calculation (5 + 5 + 7 * 0.2) and a result on the right side (11.4).

The result can be given as a String or as a double value with a prefix String. When a double value is given, it will be rounded to one decimal digit. Since the second entry (the calculation text) can be more complicated than a single number, it can only be passed as a String and rounding must be performed by the caller if needed.

Classes that implement this interface ideally provide a runtime representation of the resulting report when overriding toJComponent(). This should be displayable in a Swing Panel and not be null. At the worst, a text explaining that the report is not available in this format should be given.
  • Method Details

    • addLine

      CalculationReport addLine(@Nullable String type, @Nullable String calculation, @Nullable String result)
      Adds a single line to the CalculationReport.
      Parameters:
      type - The first element of this line, such as "Damage: "
      calculation - A calculation or other info, displayed after the type
      result - A result or other info, displayed on the right side
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • addInformationalLine

      default CalculationReport addInformationalLine(@Nullable String type, @Nullable String calculation, @Nullable String result)
      Adds a line that provides inputs or context but is not itself part of a report's running total. Display-oriented reports render this exactly like a regular line; structured report implementations may retain the semantic distinction.
      Parameters:
      type - The first element of this line
      calculation - A calculation or other info
      result - A result or other info displayed on the right side
      Returns:
      The CalculationReport itself
    • addLine

      default CalculationReport addLine(@Nullable String type, @Nullable String calculation, @Nullable String resultPrefix, double result)
      Adds a single line to the CalculationReport. This method performs rounding to a single decimal digit on the result and writes resultPrefix in front of it.
      Parameters:
      type - The first element of this line, such as "Damage: "
      calculation - A calculation or other info, displayed after the type
      resultPrefix - A text to be display immediately in front of the result, such as "= "
      result - A numerical result which will be rounded to a single decimal, e.g. 25.1
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • addLine

      default CalculationReport addLine(@Nullable String type, @Nullable String resultPrefix, double result)
      Adds a single line to the CalculationReport. This method performs rounding to a single decimal digit on the result and writes resultPrefix in front of it. This line has only two elements, the type ("Damage: ") and the result displayed on the right side.
      Parameters:
      type - The first element of this line, such as "Damage: "
      resultPrefix - A text to be display immediately in front of the result, such as "= "
      result - A numerical result which will be rounded to a single decimal, e.g. 25.1
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • addResultLine

      default CalculationReport addResultLine(@Nullable String type, @Nullable String calculation, @Nullable String resultPrefix, double result)
      Adds a single line to the CalculationReport in the way addLine() does, except the result has a line above it that may e.g. indicate a summary of previous values. This method performs rounding to a single decimal digit on the result and writes resultPrefix in front of it.
      Parameters:
      type - The first element of this line, such as "Damage: "
      calculation - A calculation or other info, displayed after the type
      resultPrefix - A text to be display immediately in front of the result, such as "= "
      result - A numerical result which will be rounded to a single decimal, e.g. 25.1
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • addResultLine

      default CalculationReport addResultLine(@Nullable String type, @Nullable String resultPrefix, double result)
      Adds a single line to the CalculationReport in the way addLine() does, except the result has a line above it that may e.g. indicate a summary of previous values. This line has only two elements, the type ("Damage: ") and the result displayed on the right side. This method performs rounding to a single decimal digit on the result and writes resultPrefix in front of it.
      Parameters:
      type - The first element of this line, such as "Damage: "
      resultPrefix - A text to be display immediately in front of the result, such as "= "
      result - A numerical result which will be rounded to a single decimal, e.g. 25.1
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • addResultLine

      default CalculationReport addResultLine(@Nullable String resultPrefix, double result)
      Adds a single line to the CalculationReport in the way addLine() does, except the result has a line above it that may e.g. indicate a summary of previous values. This line has only one element, the result displayed on the right side. This method performs rounding to a single decimal digit on the result and writes resultPrefix in front of it.
      Parameters:
      resultPrefix - A text to be display immediately in front of the result, such as "= "
      result - A numerical result which will be rounded to a single decimal, e.g. 25.1
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • addEmptyLine

      default CalculationReport addEmptyLine()
      Adds an empty line to the CalculationReport.
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • addLine

      default CalculationReport addLine(@Nullable String type, @Nullable String result)
      Adds a single line to the CalculationReport. This line has only two elements, the type ("Damage: ") and a result displayed on the right side.
      Parameters:
      type - The first element of this line, such as "Damage: "
      result - A result or other info, displayed on the right side
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • addLine

      default CalculationReport addLine(@Nullable String result)
      Adds a single line to the CalculationReport. This line only has one element displayed on the right side.
      Parameters:
      result - A result or other info, displayed on the right side
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • addSubHeader

      CalculationReport addSubHeader(String text)
      Adds a single line to the CalculationReport containing a sub-header.
      Parameters:
      text - The header text
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • addHeader

      CalculationReport addHeader(String text)
      Adds a single line to the CalculationReport containing the header for the CalculationReport. This would typically be used as the first line but can be used anywhere in the CalculationReport and multiple times.
      Parameters:
      text - The header text
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • addResultLine

      CalculationReport addResultLine(@Nullable String type, @Nullable String calculation, @Nullable String result)
      Adds a single line to the CalculationReport in the way addLine() does, except the result has a line above it that may e.g. indicate a summary of previous values.
      Parameters:
      type - The first element of this line, such as "Damage: "
      calculation - A calculation or other info, displayed after the type
      result - A result or other info, displayed on the right side
      Returns:
      The CalculationReport itself. Enables multiple stringed addLine calls
    • toJComponent

      JComponent toJComponent()
      Returns the CalculationReport as a JComponent that can be added to a dialog or other Swing component.
      Returns:
      The CalculationReport wrapped in a JComponent form.
    • formatForReport

      static String formatForReport(double d)
      Formats the given double with only the necessary digits and at most three digits. Uses the fixed Locale.US as the Java way of converting "" + value seems to use Locale.US by default as well.
    • startTentativeSection

      void startTentativeSection()
      Starts a report section that is tentative, i.e. that may be added or discarded depending on whether endTentativeSection() or discardTentativeSection() is called at a later point. All lines added to the report after calling this method are kept separate until either endTentativeSection() or discardTentativeSection() is called. When subsequently endTentativeSection() is called, the lines of the section are written to the report normally. When subsequently discardTentativeSection() is called, all lines in the section are discarded (not written to the report). Note that calling this method multiple times has no further effect. The first consecutive call of this method stays the one that marks the start of the section. Not more than a single section is maintained at any time.
    • endTentativeSection

      void endTentativeSection()
      End the current section of lines, writing them to the report normally. Note that when a section has been ended and no new section begun, calling this method again has no effect.
    • discardTentativeSection

      void discardTentativeSection()
      Discard all lines written to this section (all lines added to the report after calling startTentativeSection()). Note that when a section has been ended and no new section begun, calling this method again has no effect.
    • finalizeTentativeSection

      default void finalizeTentativeSection(boolean keepSection)
      End the current section of lines, keeping or discarding the section depending on the given keepSection. Calls either endTentativeSection() or discardTentativeSection().
      Parameters:
      keepSection - When true, keeps the current section, otherwise discards it.