- Type Parameters:
T
- the argument type of the operation
Operation interface. An operation is a function which maps some argument type
with a given arity to a result object of the same type:
Implementations of the
T[] -> T
.
final Op<Double> add = Op.of("add", 2, v -> v[0] + v[1]);
final Op<Double> add3 = Op.of("add3", 3, v -> v[0] + v[1] + v[2]);
final Op<Double> sub = Op.of("sub", 2, v -> v[0] - v[1]);
final Op<Double> sin = Op.of("sin", 1, v -> Math.sin(v[0]));
Op
interface are usually immutable and doesn't
maintain internal state. But some instances are ephemeral with changing state.
This classes must override the get()
method inherited from the
Supplier
interface and return a new instance.- Since:
- 3.9
- Version:
- 3.9
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionint
arity()
Return the arity of the operation function.get()
Returnthis
operation, or a new instance from the same type, if the operation needs to maintain internal state.default boolean
Determines if the operation is a terminal operation.name()
Return the name of the operation.static <T> Op<T>
Create a new operation from the given parameter.static <T> Op<T>
of
(String name, BinaryOperator<T> function) Create a new operation with the given name and binary operation.static <T> Op<T>
of
(String name, UnaryOperator<T> function) Create a new operation with the given name and unary operation.
-
Method Details
-
name
Return the name of the operation.- Returns:
- the name of the operation
-
arity
int arity()Return the arity of the operation function. If the arity is zero, the operation is terminal operation.- Returns:
- the arity of the operation
-
isTerminal
Determines if the operation is a terminal operation.- Returns:
true
if the operation is a terminal operation,false
otherwise
-
get
Returnthis
operation, or a new instance from the same type, if the operation needs to maintain internal state. This is essentially the case for ephemeral constants. -
of
Create a new operation from the given parameter.- Type Parameters:
T
- the operation type- Parameters:
name
- the operation namearity
- the arity of the operationfunction
- the function executed by the operation. In order to work properly, the given function should be stateless and must not have side effects.- Returns:
- a new operation from the given parameter
- Throws:
NullPointerException
- if the givenname
orfunction
isnull
IllegalArgumentException
- if the givenarity
is smaller than zero
-
of
Create a new operation with the given name and unary operation. The returnedOp
will have arity one.- Type Parameters:
T
- the operation type- Parameters:
name
- the name of the returned operationfunction
- the used function of the operation- Returns:
- a new operation with the given name and unary operation
- Throws:
NullPointerException
- if the givenname
orfunction
isnull
- Since:
- 4.0
-
of
Create a new operation with the given name and binary operation. The returnedOp
will have arity two.- Type Parameters:
T
- the operation type- Parameters:
name
- the name of the returned operationfunction
- the used function of the operation- Returns:
- a new operation with the given name and unary operation
- Throws:
NullPointerException
- if the givenname
orfunction
isnull
- Since:
- 4.0
-