Class Arithmetics
java.lang.Object
org.skriptlang.skript.lang.arithmetic.Arithmetics
Utility class for managing arithmetic operations.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <L,
R, T> T Calculates the result of an operation.static <L,
R, T> T calculateUnsafe
(Operator operator, L left, R right) Calculates the result of an operation.static <T,
R> R difference
(T left, T right, Class<R> returnType) Calculates a difference between two types if difference operation for given return type exist.static boolean
differenceExists
(Class<?> type) Checks if a difference operation for given type exists considering type assignability.static <T,
R> R differenceUnsafe
(T left, T right) Calculates a difference between two types.static boolean
exactDifferenceExists
(Class<?> type) Checks if a difference operation for exactly given type exists.static boolean
exactOperationExists
(Operator operator, Class<?> leftClass, Class<?> rightClass) Checks if an operation with the exact specified left and right operand types is already registered.Returns set of all operators with operations registered to them.static Collection
<Class<?>> getAllReturnTypes
(Operator operator) All registered types that could be returned from a calculation using this operator.static <R,
T extends R>
RgetDefaultValue
(Class<T> type) Returns a default value of a given type.static <T> @Nullable Operation
<T, T, ?> getDifference
(Class<T> type) Returns anOperation
of a difference operation for a specific type.static <T,
R> @Nullable Operation <T, T, R> getDifference
(Class<T> type, Class<R> returnType) Returns anOperation
of a difference operation for a specific type and a desired return type.static <T> @Nullable DifferenceInfo
<T, ?> getDifferenceInfo
(Class<T> type) Returns aDifferenceInfo
for a specific type.static <T,
R> @Nullable DifferenceInfo <T, R> getDifferenceInfo
(Class<T> type, Class<R> returnType) Returns aDifferenceInfo
for a specific type and a desired return type.static <L,
R> @Nullable Operation <L, R, ?> getOperation
(Operator operator, Class<L> leftClass, Class<R> rightClass) Retrieves anOperation
for a specific operator and operand types.static <L,
R, T> @Nullable Operation <L, R, T> getOperation
(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType) Retrieves anOperation
for a specific operator and operand types.static <L,
R> @Nullable OperationInfo <L, R, ?> getOperationInfo
(Operator operator, Class<L> leftClass, Class<R> rightClass) Retrieves anOperationInfo
for a specific operator and operand types.static <L,
R, T> @Nullable OperationInfo <L, R, T> getOperationInfo
(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType) Retrieves anOperationInfo
for a specific operator and operand types.static @UnmodifiableView List
<OperationInfo<?, ?, ?>> getOperations
(Operator operator) Returns view of all operation infos instances registered for a specificOperator
.static <T> @Unmodifiable List
<OperationInfo<T, ?, ?>> getOperations
(Operator operator, Class<T> type) Returns a list of operation infos for a given operator, where the left operand type is assignable from the specifiedtype
.static <L> @Unmodifiable List
<OperationInfo<L, ?, ?>> lookupLeftOperations
(Operator operator, Class<L> leftClass) Returns all valid operations fromoperator
andleftClass
.static <L,
R> @Nullable OperationInfo <L, R, ?> lookupOperationInfo
(Operator operator, Class<L> leftClass, Class<R> rightClass) Retrieves anOperationInfo
for a specific operator and operand types.static <L,
R, T> @Nullable OperationInfo <L, R, T> lookupOperationInfo
(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType) Retrieves anOperationInfo
for a specific operator and operand types.static <R> @Unmodifiable List
<OperationInfo<?, R, ?>> lookupRightOperations
(Operator operator, Class<R> rightClass) Returns all valid operations fromoperator
andrightClass
.static boolean
operationExists
(Operator operator, Class<?> leftClass, Class<?> rightClass) Checks if an operation exists, considering type conversions.static <T> void
registerDefaultValue
(Class<T> type, Supplier<T> supplier) Registers a default value for a given type.static <T,
R> void registerDifference
(Class<T> type, Class<R> returnType, Operation<T, T, R> operation) Registers a difference operation.static <T> void
registerDifference
(Class<T> type, Operation<T, T, T> operation) Registers a difference operation.static <L,
R, T> void registerOperation
(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType, Operation<L, R, T> operation) Registers a binary operation.static <L,
R, T> void registerOperation
(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType, Operation<L, R, T> operation, Operation<R, L, T> commutativeOperation) Registers a binary operation and its commutative counterpart.static <L,
R> void registerOperation
(Operator operator, Class<L> leftClass, Class<R> rightClass, Operation<L, R, L> operation) Registers a binary operation where the return type is the same as the left operand's type.static <L,
R> void registerOperation
(Operator operator, Class<L> leftClass, Class<R> rightClass, Operation<L, R, L> operation, Operation<R, L, L> commutativeOperation) Registers a binary operation and its commutative counterpart, where the return type is the same as the left operand's type for the primary operation.static <T> void
registerOperation
(Operator operator, Class<T> type, Operation<T, T, T> operation) Registers a binary operation where both left and right operands are of the same type, and the return type is also the same as the operand type.
-
Method Details
-
registerOperation
public static <T> void registerOperation(Operator operator, Class<T> type, Operation<T, T, T> operation) Registers a binary operation where both left and right operands are of the same type, and the return type is also the same as the operand type. -
registerOperation
public static <L,R> void registerOperation(Operator operator, Class<L> leftClass, Class<R> rightClass, Operation<L, R, L> operation) Registers a binary operation where the return type is the same as the left operand's type.- Type Parameters:
L
- the type of left operands and the return typeR
- the type of right operand- Parameters:
operator
- theOperator
representing this operationleftClass
- the type of left operands and the return typerightClass
- the type of right operandoperation
- theOperation
that performs the calculation
-
registerOperation
public static <L,R> void registerOperation(Operator operator, Class<L> leftClass, Class<R> rightClass, Operation<L, R, L> operation, Operation<R, L, L> commutativeOperation) Registers a binary operation and its commutative counterpart, where the return type is the same as the left operand's type for the primary operation.- Type Parameters:
L
- the type of left operands and the return typeR
- the type of right operand- Parameters:
operator
- TheOperator
representing this operationleftClass
- the type of left operands and the return typerightClass
- the type of right operandoperation
- theOperation
that performs the calculationcommutativeOperation
- the counterpart operation
-
registerOperation
public static <L,R, void registerOperationT> (Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType, Operation<L, R, T> operation, Operation<R, L, T> commutativeOperation) Registers a binary operation and its commutative counterpart.- Type Parameters:
L
- the type of left operandR
- the type of right operandT
- return type- Parameters:
operator
- TheOperator
representing this operationleftClass
- the type of left operandrightClass
- the type of right operandreturnType
- return typeoperation
- theOperation
that performs the calculationcommutativeOperation
- the counterpart operation
-
registerOperation
public static <L,R, void registerOperationT> (Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType, Operation<L, R, T> operation) Registers a binary operation.- Type Parameters:
L
- the type of left operandR
- the type of right operandT
- return type- Parameters:
operator
- TheOperator
representing this operationleftClass
- the type of left operandrightClass
- the type of right operandreturnType
- return typeoperation
- theOperation
that performs the calculation
-
exactOperationExists
public static boolean exactOperationExists(Operator operator, Class<?> leftClass, Class<?> rightClass) Checks if an operation with the exact specified left and right operand types is already registered.- Parameters:
operator
- the operator of the operationleftClass
- the exact type of the left operandrightClass
- the exact type of the right operand- Returns:
true
if an exact match is found, elsefalse
-
operationExists
Checks if an operation exists, considering type conversions.- Parameters:
operator
- the operator of the operationleftClass
- the type of the left operandrightClass
- the type of the right operand- Returns:
true
if suitable operation is found, elsefalse
- See Also:
-
getOperations
Returns view of all operation infos instances registered for a specificOperator
.- Parameters:
operator
- the operator- Returns:
- all registered operations for the given operator
-
getOperations
public static <T> @Unmodifiable List<OperationInfo<T,?, getOperations?>> (Operator operator, Class<T> type) Returns a list of operation infos for a given operator, where the left operand type is assignable from the specifiedtype
. This does not consider type conversions, but does consider type assignability.- Type Parameters:
T
- the type to filter by for the left operand- Parameters:
operator
- the operatortype
- left operand type- Returns:
- all registered operations for the given operator and type
-
lookupLeftOperations
public static <L> @Unmodifiable List<OperationInfo<L,?, lookupLeftOperations?>> (Operator operator, Class<L> leftClass) Returns all valid operations fromoperator
andleftClass
. UnlikegetOperations(Operator, Class)
, this method considers converters.- Type Parameters:
L
- the type to filter by for the left operand- Parameters:
operator
- the operatorleftClass
- left operand type- Returns:
- list containing all valid operations from
operator
andleftClass
.
-
lookupRightOperations
public static <R> @Unmodifiable List<OperationInfo<?,R, lookupRightOperations?>> (Operator operator, Class<R> rightClass) Returns all valid operations fromoperator
andrightClass
. This method considers converters and type assignability.- Type Parameters:
R
- the type to filter by for the right operand- Parameters:
operator
- the operatorrightClass
- right operand type- Returns:
- list containing all valid operations from
operator
andrightClass
.
-
getOperationInfo
@Nullable public static <L,R, @Nullable OperationInfo<L,T> R, getOperationInfoT> (Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType) Retrieves anOperationInfo
for a specific operator and operand types. This method considers type assignability, but does not consider converters.- Type Parameters:
L
- the type of left operandR
- the type of right operandT
- return type- Parameters:
operator
- the operatorleftClass
- the type of left operandrightClass
- the type of right operandreturnType
- return type- Returns:
- suitable operation info
-
getOperationInfo
@Nullable public static <L,R> @Nullable OperationInfo<L,R, getOperationInfo?> (Operator operator, Class<L> leftClass, Class<R> rightClass) Retrieves anOperationInfo
for a specific operator and operand types. This method considers type assignability, but does not consider converters.- Type Parameters:
L
- the type of left operandR
- the type of right operand- Parameters:
operator
- the operatorleftClass
- the type of left operandrightClass
- the type of right operand- Returns:
- suitable operation info
-
getOperation
@Nullable public static <L,R, @Nullable Operation<L,T> R, getOperationT> (Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType) Retrieves anOperation
for a specific operator and operand types. This method considers type assignability, but does not consider converters.- Type Parameters:
L
- the type of left operandR
- the type of right operandT
- return type- Parameters:
operator
- the operatorleftClass
- the type of left operandrightClass
- the type of right operandreturnType
- return type- Returns:
- suitable operation
-
getOperation
@Nullable public static <L,R> @Nullable Operation<L,R, getOperation?> (Operator operator, Class<L> leftClass, Class<R> rightClass) Retrieves anOperation
for a specific operator and operand types. This method considers type assignability, but does not consider converters.- Type Parameters:
L
- the type of left operandR
- the type of right operand- Parameters:
operator
- the operatorleftClass
- the type of left operandrightClass
- the type of right operand- Returns:
- suitable operation
-
lookupOperationInfo
@Nullable public static <L,R, @Nullable OperationInfo<L,T> R, lookupOperationInfoT> (Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType) Retrieves anOperationInfo
for a specific operator and operand types. This method considers type assignability and converters.- Type Parameters:
L
- the type of left operandR
- the type of right operandT
- return type- Parameters:
operator
- the operatorleftClass
- the type of left operandrightClass
- the type of right operandreturnType
- return type- Returns:
- suitable operation
-
lookupOperationInfo
@Nullable public static <L,R> @Nullable OperationInfo<L,R, lookupOperationInfo?> (Operator operator, Class<L> leftClass, Class<R> rightClass) Retrieves anOperationInfo
for a specific operator and operand types. This method considers type assignability and converters.- Type Parameters:
L
- the type of left operandR
- the type of right operand- Parameters:
operator
- the operatorleftClass
- the type of left operandrightClass
- the type of right operand- Returns:
- suitable operation
-
calculate
@Nullable public static <L,R, T calculateT> (Operator operator, L left, R right, Class<T> returnType) Calculates the result of an operation.- Type Parameters:
L
- the type of left operandR
- the type of right operandT
- return type- Parameters:
operator
- operator to applyleft
- left operandright
- right operandreturnType
- return type- Returns:
- the calculated result
-
calculateUnsafe
Calculates the result of an operation.This method does not verify the operation result type.
- Type Parameters:
L
- the type of left operandR
- the type of right operandT
- return type- Parameters:
operator
- operator to applyleft
- left operandright
- right operand- Returns:
- the calculated result
-
registerDifference
Registers a difference operation.- Type Parameters:
T
- the type of the operands and result- Parameters:
type
- the type of the operands and resultoperation
- the operation for calculating the difference- See Also:
-
registerDifference
public static <T,R> void registerDifference(Class<T> type, Class<R> returnType, Operation<T, T, R> operation) Registers a difference operation.- Type Parameters:
T
- the type of the operands and result- Parameters:
type
- the type of the operandsreturnType
- type of the resultoperation
- the operation for calculating the difference- See Also:
-
exactDifferenceExists
Checks if a difference operation for exactly given type exists.- Parameters:
type
- type to check- Returns:
- if a difference operation for exactly given type exists
-
differenceExists
Checks if a difference operation for given type exists considering type assignability.- Parameters:
type
- type to check- Returns:
- if a difference operation for given type exists
-
getDifferenceInfo
@Nullable public static <T,R> @Nullable DifferenceInfo<T,R> getDifferenceInfo(Class<T> type, Class<R> returnType) Returns aDifferenceInfo
for a specific type and a desired return type.- Type Parameters:
T
- operands typeR
- result type- Parameters:
type
- operands typereturnType
- result type- Returns:
- A suitable difference info if one is found, else null
-
getDifferenceInfo
Returns aDifferenceInfo
for a specific type.- Type Parameters:
T
- operands type- Parameters:
type
- operands type- Returns:
- A suitable difference info if one is found, else null
-
getDifference
@Nullable public static <T,R> @Nullable Operation<T,T, getDifferenceR> (Class<T> type, Class<R> returnType) Returns anOperation
of a difference operation for a specific type and a desired return type.- Type Parameters:
T
- operands typeR
- result type- Parameters:
type
- operands typereturnType
- result type- Returns:
- A suitable operation if one is found, else null
-
getDifference
Returns anOperation
of a difference operation for a specific type.- Type Parameters:
T
- operands type- Parameters:
type
- operands type- Returns:
- A suitable operation if one is found, else null
-
difference
Calculates a difference between two types if difference operation for given return type exist.- Type Parameters:
T
- operands typeR
- return type- Parameters:
left
- left operandright
- right operandreturnType
- return type- Returns:
- difference
-
differenceUnsafe
@Nullable public static <T,R> R differenceUnsafe(T left, T right) Calculates a difference between two types.This method does not check for the return type of the difference info.
- Type Parameters:
T
- operands typeR
- return type- Parameters:
left
- left operandright
- right operand- Returns:
- difference
-
registerDefaultValue
Registers a default value for a given type.- Type Parameters:
T
- type- Parameters:
type
- typesupplier
- default value supplier
-
getDefaultValue
Returns a default value of a given type.- Type Parameters:
R
- default valueT
- type- Parameters:
type
- type- Returns:
- default value
-
getAllReturnTypes
All registered types that could be returned from a calculation using this operator. This is used to fetch potential return types when unknown (variable) arguments are used in a sum.- Parameters:
operator
- The operator to test- Returns:
- Every type this could return
-
getAllOperators
Returns set of all operators with operations registered to them.This set is sorted by the priority of the operators.
Modifying this set does not change the registered modifiers.
- Returns:
- registered operators
-