Class Arithmetics

java.lang.Object
org.skriptlang.skript.lang.arithmetic.Arithmetics

public final class Arithmetics extends Object
Utility class for managing arithmetic operations.
  • 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.
      Type Parameters:
      T - the type of both operands and the return type
      Parameters:
      operator - the Operator representing this operation
      type - the type of both operands and the return type
      operation - the Operation that performs the calculation
    • 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 type
      R - the type of right operand
      Parameters:
      operator - the Operator representing this operation
      leftClass - the type of left operands and the return type
      rightClass - the type of right operand
      operation - the Operation 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 type
      R - the type of right operand
      Parameters:
      operator - The Operator representing this operation
      leftClass - the type of left operands and the return type
      rightClass - the type of right operand
      operation - the Operation that performs the calculation
      commutativeOperation - the counterpart operation
    • registerOperation

      public 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.
      Type Parameters:
      L - the type of left operand
      R - the type of right operand
      T - return type
      Parameters:
      operator - The Operator representing this operation
      leftClass - the type of left operand
      rightClass - the type of right operand
      returnType - return type
      operation - the Operation that performs the calculation
      commutativeOperation - the counterpart operation
    • registerOperation

      public 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.
      Type Parameters:
      L - the type of left operand
      R - the type of right operand
      T - return type
      Parameters:
      operator - The Operator representing this operation
      leftClass - the type of left operand
      rightClass - the type of right operand
      returnType - return type
      operation - the Operation 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 operation
      leftClass - the exact type of the left operand
      rightClass - the exact type of the right operand
      Returns:
      true if an exact match is found, else false
    • operationExists

      public static boolean operationExists(Operator operator, Class<?> leftClass, Class<?> rightClass)
      Checks if an operation exists, considering type conversions.
      Parameters:
      operator - the operator of the operation
      leftClass - the type of the left operand
      rightClass - the type of the right operand
      Returns:
      true if suitable operation is found, else false
      See Also:
    • getOperations

      public static @UnmodifiableView List<OperationInfo<?,?,?>> getOperations(Operator operator)
      Returns view of all operation infos instances registered for a specific Operator.
      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 specified type. 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 operator
      type - 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 from operator and leftClass. Unlike getOperations(Operator, Class), this method considers converters.
      Type Parameters:
      L - the type to filter by for the left operand
      Parameters:
      operator - the operator
      leftClass - left operand type
      Returns:
      list containing all valid operations from operator and leftClass.
    • lookupRightOperations

      public static <R> @Unmodifiable List<OperationInfo<?,R,?>> lookupRightOperations(Operator operator, Class<R> rightClass)
      Returns all valid operations from operator and rightClass. This method considers converters and type assignability.
      Type Parameters:
      R - the type to filter by for the right operand
      Parameters:
      operator - the operator
      rightClass - right operand type
      Returns:
      list containing all valid operations from operator and rightClass.
    • getOperationInfo

      @Nullable public static <L, R, T> @Nullable OperationInfo<L,R,T> getOperationInfo(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType)
      Retrieves an OperationInfo for a specific operator and operand types. This method considers type assignability, but does not consider converters.
      Type Parameters:
      L - the type of left operand
      R - the type of right operand
      T - return type
      Parameters:
      operator - the operator
      leftClass - the type of left operand
      rightClass - the type of right operand
      returnType - 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 an OperationInfo for a specific operator and operand types. This method considers type assignability, but does not consider converters.
      Type Parameters:
      L - the type of left operand
      R - the type of right operand
      Parameters:
      operator - the operator
      leftClass - the type of left operand
      rightClass - the type of right operand
      Returns:
      suitable operation info
    • getOperation

      @Nullable public static <L, R, T> @Nullable Operation<L,R,T> getOperation(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType)
      Retrieves an Operation for a specific operator and operand types. This method considers type assignability, but does not consider converters.
      Type Parameters:
      L - the type of left operand
      R - the type of right operand
      T - return type
      Parameters:
      operator - the operator
      leftClass - the type of left operand
      rightClass - the type of right operand
      returnType - 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 an Operation for a specific operator and operand types. This method considers type assignability, but does not consider converters.
      Type Parameters:
      L - the type of left operand
      R - the type of right operand
      Parameters:
      operator - the operator
      leftClass - the type of left operand
      rightClass - the type of right operand
      Returns:
      suitable operation
    • lookupOperationInfo

      @Nullable public static <L, R, T> @Nullable OperationInfo<L,R,T> lookupOperationInfo(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType)
      Retrieves an OperationInfo for a specific operator and operand types. This method considers type assignability and converters.
      Type Parameters:
      L - the type of left operand
      R - the type of right operand
      T - return type
      Parameters:
      operator - the operator
      leftClass - the type of left operand
      rightClass - the type of right operand
      returnType - 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 an OperationInfo for a specific operator and operand types. This method considers type assignability and converters.
      Type Parameters:
      L - the type of left operand
      R - the type of right operand
      Parameters:
      operator - the operator
      leftClass - the type of left operand
      rightClass - the type of right operand
      Returns:
      suitable operation
    • calculate

      @Nullable public static <L, R, T> T calculate(Operator operator, L left, R right, Class<T> returnType)
      Calculates the result of an operation.
      Type Parameters:
      L - the type of left operand
      R - the type of right operand
      T - return type
      Parameters:
      operator - operator to apply
      left - left operand
      right - right operand
      returnType - return type
      Returns:
      the calculated result
    • calculateUnsafe

      @Nullable public static <L, R, T> T calculateUnsafe(Operator operator, L left, R right)
      Calculates the result of an operation.

      This method does not verify the operation result type.

      Type Parameters:
      L - the type of left operand
      R - the type of right operand
      T - return type
      Parameters:
      operator - operator to apply
      left - left operand
      right - right operand
      Returns:
      the calculated result
    • registerDifference

      public static <T> void registerDifference(Class<T> type, Operation<T,T,T> operation)
      Registers a difference operation.
      Type Parameters:
      T - the type of the operands and result
      Parameters:
      type - the type of the operands and result
      operation - 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 operands
      returnType - type of the result
      operation - the operation for calculating the difference
      See Also:
    • exactDifferenceExists

      public static boolean exactDifferenceExists(Class<?> type)
      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

      public static boolean differenceExists(Class<?> type)
      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 a DifferenceInfo for a specific type and a desired return type.
      Type Parameters:
      T - operands type
      R - result type
      Parameters:
      type - operands type
      returnType - result type
      Returns:
      A suitable difference info if one is found, else null
    • getDifferenceInfo

      @Nullable public static <T> @Nullable DifferenceInfo<T,?> getDifferenceInfo(Class<T> type)
      Returns a DifferenceInfo 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,R> getDifference(Class<T> type, Class<R> returnType)
      Returns an Operation of a difference operation for a specific type and a desired return type.
      Type Parameters:
      T - operands type
      R - result type
      Parameters:
      type - operands type
      returnType - result type
      Returns:
      A suitable operation if one is found, else null
    • getDifference

      @Nullable public static <T> @Nullable Operation<T,T,?> getDifference(Class<T> type)
      Returns an Operation 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

      @Nullable public 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.
      Type Parameters:
      T - operands type
      R - return type
      Parameters:
      left - left operand
      right - right operand
      returnType - 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 type
      R - return type
      Parameters:
      left - left operand
      right - right operand
      Returns:
      difference
    • registerDefaultValue

      public static <T> void registerDefaultValue(Class<T> type, Supplier<T> supplier)
      Registers a default value for a given type.
      Type Parameters:
      T - type
      Parameters:
      type - type
      supplier - default value supplier
    • getDefaultValue

      @Nullable public static <R, T extends R> R getDefaultValue(Class<T> type)
      Returns a default value of a given type.
      Type Parameters:
      R - default value
      T - type
      Parameters:
      type - type
      Returns:
      default value
    • getAllReturnTypes

      public static Collection<Class<?>> getAllReturnTypes(Operator operator)
      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

      public static Set<Operator> 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