public final class Calculator
extends java.lang.Object
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.Modifier and Type | Field and Description |
---|---|
static CalculatorEngine |
STANDARD
A singleton for the calculator engine that implements standard operations described by the
StandardOperation enumeration.
|
Modifier and Type | Method and Description |
---|---|
static Data |
calculate(CalculatorEngine engine,
java.lang.Enum<?> operationId,
Data... operands)
Performs calculation by applying the specified engine and operation to the operands.
|
static Data |
calculate(java.lang.Enum<?> operationId,
Data... operands)
Performs calculation by applying the specified 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.
|
public static final CalculatorEngine STANDARD
public static boolean registerEngine(java.lang.Class<? extends java.lang.Enum<?>> operationIdClass, CalculatorEngine engine)
operationIdClass
- Class of the enumeration that identifies operations implemented by the
given calculator engine.engine
- Calculator engine to be registered.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).java.lang.IllegalArgumentException
- if any of the parameters equals null
.public static CalculatorEngine getEngine(java.lang.Class<?> operationIdClass)
null
is returned.operationIdClass
- Class of the enumeration that identifies operations implemented by the
given engine.null
if no such engine is registered.java.lang.IllegalArgumentException
- if the parameter equals null
.public static boolean isSupported(java.lang.Enum<?> operationId, Data... operands)
operationId
- Operation identifier. Identifies an operation within a group.operands
- A variable number of operands.true
if the operation is supported for the given operand types or
false
otherwise.java.lang.IllegalArgumentException
- if any of the parameters equals null
.public static Data calculate(CalculatorEngine engine, java.lang.Enum<?> operationId, Data... operands)
engine
- Calculator engine.operationId
- Operation identifier. Identifies an operation within a group.operands
- A variable number of operands.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).public static Data calculate(java.lang.Enum<?> operationId, Data... operands)
operationId
- Operation identifier. Identifies an operation within a group.operands
- A variable number of operands.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).