Class Calculator


  • public final class Calculator
    extends java.lang.Object
    The Calculator class is responsible for performing calculations on data objects using an extendible set of operations. It encapsulates a table of calculator engines each implementing operations that belong to some logic group. Each group is represented by a enumeration identifying operations it contains. The class of the enumeration is used to identify engines implementing operations from the given group. It is possible to extend functionality by registering custom engines implementing new group of operations. Also, you can replace existing engines with custom engines if needed.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static CalculatorEngine STANDARD
      A singleton for the calculator engine that implements standard operations described by the StandardOperation enumeration.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Data calculate​(java.lang.Enum<?> operationId, Data... operands)
      Performs calculation by applying the specified operation to the operands.
      static Data calculate​(CalculatorEngine engine, java.lang.Enum<?> operationId, Data... operands)
      Performs calculation by applying the specified engine and operation to the operands.
      static CalculatorEngine getEngine​(java.lang.Class<?> operationIdClass)
      Returns the engine that performs calculations using a specific group of operations.
      static boolean isSupported​(java.lang.Enum<?> operationId, Data... operands)
      Checks whether the specified operation is supported for the provided operands.
      static boolean registerEngine​(java.lang.Class<? extends java.lang.Enum<?>> operationIdClass, CalculatorEngine engine)
      Registers a calculator engine that performs calculations using operations that belong to the given operation group which is described with a corresponding enumeration.
      • Methods inherited from class java.lang.Object

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

      • STANDARD

        public static final CalculatorEngine STANDARD
        A singleton for the calculator engine that implements standard operations described by the StandardOperation enumeration.
    • Method Detail

      • registerEngine

        public static boolean registerEngine​(java.lang.Class<? extends java.lang.Enum<?>> operationIdClass,
                                             CalculatorEngine engine)
        Registers a calculator engine that performs calculations using operations that belong to the given operation group which is described with a corresponding enumeration. The class of the enumeration serves as a key.
        Parameters:
        operationIdClass - Class of the enumeration that identifies operations implemented by the given calculator engine.
        engine - Calculator engine to be registered.
        Returns:
        true if the engine was successfully registered and it had not been previously registered or false if an engine identified by the specified class had already been registered (in this case, it is replaced with a new engine).
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters equals null.
      • getEngine

        public static CalculatorEngine getEngine​(java.lang.Class<?> operationIdClass)
        Returns the engine that performs calculations using a specific group of operations. Operations that belong to a single group are identified with a corresponding enumeration. The class of the enumeration serves as a key. If no such engine is registered null is returned.
        Parameters:
        operationIdClass - Class of the enumeration that identifies operations implemented by the given engine.
        Returns:
        Engine responsible for performing a specific group of operations or null if no such engine is registered.
        Throws:
        java.lang.IllegalArgumentException - if the parameter equals null.
      • isSupported

        public static boolean isSupported​(java.lang.Enum<?> operationId,
                                          Data... operands)
        Checks whether the specified operation is supported for the provided operands. The class of the operation identifier, its value and operand types are taken into consideration.
        Parameters:
        operationId - Operation identifier. Identifies an operation within a group.
        operands - A variable number of operands.
        Returns:
        true if the operation is supported for the given operand types or false otherwise.
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters equals null.
      • calculate

        public static Data calculate​(CalculatorEngine engine,
                                     java.lang.Enum<?> operationId,
                                     Data... operands)
        Performs calculation by applying the specified engine and operation to the operands.
        Parameters:
        engine - Calculator engine.
        operationId - Operation identifier. Identifies an operation within a group.
        operands - A variable number of operands.
        Returns:
        Data object holding the calculated value.
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters equals null.
        java.lang.UnsupportedOperationException - if the operation is not supported or its invariants are violated (e.g. operand types do not match).
      • calculate

        public static Data calculate​(java.lang.Enum<?> operationId,
                                     Data... operands)
        Performs calculation by applying the specified operation to the operands.
        Parameters:
        operationId - Operation identifier. Identifies an operation within a group.
        operands - A variable number of operands.
        Returns:
        Data object holding the calculated value.
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters equals null.
        java.lang.UnsupportedOperationException - if the operation is not supported or its invariants are violated (e.g. operand types do not match).