Class AbstractCalculatorImpl

  • All Implemented Interfaces:
    java.io.Serializable, CalculatorImpl
    Direct Known Subclasses:
    FunctionCalculatorImpl

    public abstract class AbstractCalculatorImpl
    extends java.lang.Object
    implements CalculatorImpl, java.io.Serializable
    Basic calculator implementation. Provides a storage for variables, and maps the elementary operators to function calls.
    Version:
    1.14.0
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Long inputPrecision  
      private boolean pretty  
      private static long serialVersionUID  
      private java.util.Map<java.lang.String,​java.lang.Number> variables  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AbstractCalculatorImpl()
      Default constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Number add​(java.lang.Number x, java.lang.Number y)
      Addition.
      java.lang.Number divide​(java.lang.Number x, java.lang.Number y)
      Division.
      java.lang.Number doubleFactorial​(java.lang.Number x)
      Double factorial.
      java.lang.Number factorial​(java.lang.Number x)
      Factorial.
      protected boolean getFormat()
      Get the formatting option.
      protected java.lang.Long getInputPrecision()
      Get the input precision.
      java.lang.Number getVariable​(java.lang.String name)
      Get a variable.
      java.lang.Number mod​(java.lang.Number x, java.lang.Number y)
      Remainder.
      java.lang.Number multiply​(java.lang.Number x, java.lang.Number y)
      Multiplication.
      java.lang.Number negate​(java.lang.Number x)
      Negative value.
      java.lang.Number pow​(java.lang.Number x, java.lang.Number y)
      Power.
      void setFormat​(boolean pretty)
      Set the formatting option.
      void setInputPrecision​(java.lang.Long inputPrecision)
      Set a fixed input precision.
      void setVariable​(java.lang.String name, java.lang.Number value)
      Set a variable.
      java.lang.Number subtract​(java.lang.Number x, java.lang.Number y)
      Subtraction.
      private static java.util.List<java.lang.Number> toList​(java.lang.Number x)  
      private static java.util.List<java.lang.Number> toList​(java.lang.Number x, java.lang.Number y)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • variables

        private java.util.Map<java.lang.String,​java.lang.Number> variables
      • pretty

        private boolean pretty
      • inputPrecision

        private java.lang.Long inputPrecision
    • Constructor Detail

      • AbstractCalculatorImpl

        protected AbstractCalculatorImpl()
        Default constructor.
    • Method Detail

      • add

        public java.lang.Number add​(java.lang.Number x,
                                    java.lang.Number y)
                             throws ParseException
        Description copied from interface: CalculatorImpl
        Addition.
        Specified by:
        add in interface CalculatorImpl
        Parameters:
        x - First argument.
        y - Second argument.
        Returns:
        x + y
        Throws:
        ParseException - In case of invalid arguments.
      • subtract

        public java.lang.Number subtract​(java.lang.Number x,
                                         java.lang.Number y)
                                  throws ParseException
        Description copied from interface: CalculatorImpl
        Subtraction.
        Specified by:
        subtract in interface CalculatorImpl
        Parameters:
        x - First argument.
        y - Second argument.
        Returns:
        x - y
        Throws:
        ParseException - In case of invalid arguments.
      • multiply

        public java.lang.Number multiply​(java.lang.Number x,
                                         java.lang.Number y)
                                  throws ParseException
        Description copied from interface: CalculatorImpl
        Multiplication.
        Specified by:
        multiply in interface CalculatorImpl
        Parameters:
        x - First argument.
        y - Second argument.
        Returns:
        x * y
        Throws:
        ParseException - In case of invalid arguments.
      • divide

        public java.lang.Number divide​(java.lang.Number x,
                                       java.lang.Number y)
                                throws ParseException
        Description copied from interface: CalculatorImpl
        Division.
        Specified by:
        divide in interface CalculatorImpl
        Parameters:
        x - First argument.
        y - Second argument.
        Returns:
        x / y
        Throws:
        ParseException - In case of invalid arguments.
      • mod

        public java.lang.Number mod​(java.lang.Number x,
                                    java.lang.Number y)
                             throws ParseException
        Description copied from interface: CalculatorImpl
        Remainder.
        Specified by:
        mod in interface CalculatorImpl
        Parameters:
        x - First argument.
        y - Second argument.
        Returns:
        x % y
        Throws:
        ParseException - In case of invalid arguments.
      • pow

        public java.lang.Number pow​(java.lang.Number x,
                                    java.lang.Number y)
                             throws ParseException
        Description copied from interface: CalculatorImpl
        Power.
        Specified by:
        pow in interface CalculatorImpl
        Parameters:
        x - First argument.
        y - Second argument.
        Returns:
        xy
        Throws:
        ParseException - In case of invalid arguments.
      • getVariable

        public java.lang.Number getVariable​(java.lang.String name)
                                     throws ParseException
        Description copied from interface: CalculatorImpl
        Get a variable.
        Specified by:
        getVariable in interface CalculatorImpl
        Parameters:
        name - Name of the variable.
        Returns:
        Value of the variable, or null if the variable is not defined.
        Throws:
        ParseException - In case of invalid argument.
      • setVariable

        public void setVariable​(java.lang.String name,
                                java.lang.Number value)
        Description copied from interface: CalculatorImpl
        Set a variable.
        Specified by:
        setVariable in interface CalculatorImpl
        Parameters:
        name - Name of the variable.
        value - Value of the variable.
      • setFormat

        public void setFormat​(boolean pretty)
        Description copied from interface: CalculatorImpl
        Set the formatting option.
        Specified by:
        setFormat in interface CalculatorImpl
        Parameters:
        pretty - If a fixed-point or a floating-point notation should be used.
      • setInputPrecision

        public void setInputPrecision​(java.lang.Long inputPrecision)
        Description copied from interface: CalculatorImpl
        Set a fixed input precision.
        Specified by:
        setInputPrecision in interface CalculatorImpl
        Parameters:
        inputPrecision - The precision if a fixed precision is used or null for arbitrary precision.
      • getFormat

        protected boolean getFormat()
        Get the formatting option.
        Returns:
        If a fixed-point or a floating-point notation should be used.
      • getInputPrecision

        protected java.lang.Long getInputPrecision()
        Get the input precision.
        Returns:
        The input precision if a fixed precision is used or null for arbitrary precision.
      • toList

        private static java.util.List<java.lang.Number> toList​(java.lang.Number x)
      • toList

        private static java.util.List<java.lang.Number> toList​(java.lang.Number x,
                                                               java.lang.Number y)