ArithmeticEngine (FreeMarker 2.3.35 API)
Description: declaration: package: freemarker.core, class: ArithmeticEngine

Package freemarker.core

# Class ArithmeticEngine

java.lang.Object
freemarker.core.ArithmeticEngine

Direct Known Subclasses:
  `ArithmeticEngine.BigDecimalEngine`, `ArithmeticEngine.ConservativeEngine`

public abstract class ArithmeticEngine extends Object
Used for implementing the arithmetic operations and number comparisons in the template language. The concrete implementation is plugged into the configuration with the `arithmetical_engine` setting. (See `Configurable.setArithmeticEngine(ArithmeticEngine)`.)

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `ArithmeticEngine.BigDecimalEngine`
  This is the default arithmetic engine in FreeMarker.
  `static class`
  `ArithmeticEngine.ConservativeEngine`
  An arithmetic engine that conservatively widens the operation arguments to extent that they can hold the result of the operation.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static ArithmeticEngine.BigDecimalEngine`
  `BIGDECIMAL_ENGINE`
  Arithmetic engine that converts all numbers to `BigDecimal` and then operates on them, and also keeps the result as a `BigDecimal`.
  `static ArithmeticEngine.ConservativeEngine`
  `CONSERVATIVE_ENGINE`
  Arithmetic engine that uses (more-or-less) the widening conversions of Java language to determine the type of result of operation, instead of converting everything to BigDecimal up front.
  `protected int`
  `maxScale`
  `protected int`
  `minScale`
  `protected int`
  `roundingPolicy`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ArithmeticEngine()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `abstract Number`
  `add​(Number first, Number second)`
  `abstract int`
  `compareNumbers​(Number first, Number second)`
  `abstract Number`
  `divide​(Number first, Number second)`
  `Number`
  `hexadecimalToNumber​(String s)`
  Parses a hexadecimal number like `"0xCAFEBABE"`.
  `abstract Number`
  `modulus​(Number first, Number second)`
  `abstract Number`
  `multiply​(Number first, Number second)`
  `void`
  `setMaxScale​(int maxScale)`
  Sets the maximal scale to use when multiplying BigDecimal numbers.
  `void`
  `setMinScale​(int minScale)`
  Sets the minimal scale to use when dividing BigDecimal numbers.
  `void`
  `setRoundingPolicy​(int roundingPolicy)`
  `abstract Number`
  `subtract​(Number first, Number second)`
  `abstract Number`
  `toNumber​(String s)`
  Should be able to parse all FTL numerical literals, Java Double toString results, and XML Schema numbers.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### BIGDECIMAL_ENGINE

    public static final ArithmeticEngine.BigDecimalEngine BIGDECIMAL_ENGINE
    Arithmetic engine that converts all numbers to `BigDecimal` and then operates on them, and also keeps the result as a `BigDecimal`. This is FreeMarker's default arithmetic engine.

  - ### CONSERVATIVE_ENGINE

    public static final ArithmeticEngine.ConservativeEngine CONSERVATIVE_ENGINE
    Arithmetic engine that uses (more-or-less) the widening conversions of Java language to determine the type of result of operation, instead of converting everything to BigDecimal up front.

  - ### minScale

    protected int minScale

  - ### maxScale

    protected int maxScale

  - ### roundingPolicy

    protected int roundingPolicy

- ## Constructor Details

  - ### ArithmeticEngine

    public ArithmeticEngine()

- ## Method Details

  - ### compareNumbers

    public abstract int compareNumbers​(Number first, Number second) throws TemplateException
    Throws:
      `TemplateException`

  - ### add

    public abstract Number add​(Number first, Number second) throws TemplateException
    Throws:
      `TemplateException`

  - ### subtract

    public abstract Number subtract​(Number first, Number second) throws TemplateException
    Throws:
      `TemplateException`

  - ### multiply

    public abstract Number multiply​(Number first, Number second) throws TemplateException
    Throws:
      `TemplateException`

  - ### divide

    public abstract Number divide​(Number first, Number second) throws TemplateException
    Throws:
      `TemplateException`

  - ### modulus

    public abstract Number modulus​(Number first, Number second) throws TemplateException
    Throws:
      `TemplateException`

  - ### toNumber

    public abstract Number toNumber​(String s)
    Should be able to parse all FTL numerical literals, Java Double toString results, and XML Schema numbers. This means these should be parsed successfully, except if the arithmetical engine couldn't support the resulting value anyway (such as NaN, infinite, even non-integers): `-123.45`, `1.5e3`, `1.5E3`, `0005`, `+0`, `-0`, `NaN`, `INF`, `-INF`, `Infinity`, `-Infinity`.

  - ### hexadecimalToNumber

    public Number hexadecimalToNumber​(String s)
    Parses a hexadecimal number like `"0xCAFEBABE"`. The template language parser restricts what needs to be supported here:
    - Parsing must be case-insensitive
    - There's never a sign (plus or minus); if there's a minus sign, that will be handled outside this method
    - The string always starts with `0x` (or `0X`), so you can safely ignore the first 2 characters
    - There's an arbitrary number of digits
    - Only whole numbers need to be supported (there's never a `.` in the string)

    Default implementation in `ArithmeticEngine` chooses the smallest type that can still represent the number (always positive) without loss: `Integer`, `Long`, `BigInteger`

    Since:
      2.3.35

  - ### setMinScale

    public void setMinScale​(int minScale)
    Sets the minimal scale to use when dividing BigDecimal numbers. Default value is 12.

  - ### setMaxScale

    public void setMaxScale​(int maxScale)
    Sets the maximal scale to use when multiplying BigDecimal numbers. Default value is 100.

  - ### setRoundingPolicy

    public void setRoundingPolicy​(int roundingPolicy)