Class OperationGroup<OperationIdT extends java.lang.Enum<OperationIdT>>

  • Type Parameters:
    OperationIdT - Class of the enumeration that specifies operations included in the operation group. An operation group object can hold only operations that are members of the same group and are described as elements of the same enumeration.
    All Implemented Interfaces:
    CalculatorEngine

    public final class OperationGroup<OperationIdT extends java.lang.Enum<OperationIdT>>
    extends java.lang.Object
    implements CalculatorEngine
    The OperationGroup class is an implementation of a calculator engine that encapsulates a collection of objects that implement specific operations. Operation objects are grouped by the data type they perform operations on. That is, there may be several implementations of the same operation for different data types.
    • Constructor Summary

      Constructors 
      Constructor Description
      OperationGroup()
      Constructor for an operation group object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Data calculate​(java.lang.Enum<?> operationId, Data... operands)
      Performs calculation by applying the specified operation to the operands.
      boolean isSupported​(java.lang.Enum<?> operationId, Data... operands)
      Checks whether the specified operation is supported for the provided operands.
      static <T extends java.lang.Enum<T>>
      java.util.Map<T,​Operation<T>>
      newOperationMap​(java.lang.Class<T> clazz, java.util.Collection<? extends Operation<T>> operations)
      Puts operations from the specified collection to a map where key corresponds to operation identifier and value corresponds to operation implementation.
      void registerOperations​(DataTypeId typeId, java.util.Map<OperationIdT,​Operation<OperationIdT>> operationsForType)
      Registers a group of operators that perform calculations on a specific data type.
      • Methods inherited from class java.lang.Object

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

      • OperationGroup

        public OperationGroup()
        Constructor for an operation group object.
    • Method Detail

      • registerOperations

        public final void registerOperations​(DataTypeId typeId,
                                             java.util.Map<OperationIdT,​Operation<OperationIdT>> operationsForType)
        Registers a group of operators that perform calculations on a specific data type.
        Parameters:
        typeId - Data type identifier.
        operationsForType - A map of operation on the specified data type. Key is the operation identifier and value is the operation implementation.
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters equals null.
      • isSupported

        public final boolean isSupported​(java.lang.Enum<?> operationId,
                                         Data... operands)
        Checks whether the specified operation is supported for the provided operands. Operation identifier and operand types are taken into consideration.
        Specified by:
        isSupported in interface CalculatorEngine
        Parameters:
        operationId - Operator identifier. Identifies an operation within a group.
        operands - Array of operands.
        Returns:
        true if the operation is supported for the given operand types or false if it is not supported or its invariants are violated (e.g. operand types do not match).
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters equals null.
      • calculate

        public final Data calculate​(java.lang.Enum<?> operationId,
                                    Data... operands)
        Performs calculation by applying the specified operation to the operands.
        Specified by:
        calculate in interface CalculatorEngine
        Parameters:
        operationId - Operator identifier. Identifies an operation within a group.
        operands - Array 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 specified operation is not supported for the provided operands.
      • newOperationMap

        public static <T extends java.lang.Enum<T>> java.util.Map<T,​Operation<T>> newOperationMap​(java.lang.Class<T> clazz,
                                                                                                        java.util.Collection<? extends Operation<T>> operations)
        Puts operations from the specified collection to a map where key corresponds to operation identifier and value corresponds to operation implementation.
        Type Parameters:
        T - Operation identifier type.
        Parameters:
        clazz - Class describing the enumeration that defines operation identifiers.
        operations - Collection of operations.
        Returns:
        New operation map.