Enum Class BinaryOperation

java.lang.Object
java.lang.Enum<BinaryOperation>
org.daiitech.naftah.builtin.utils.op.BinaryOperation
All Implemented Interfaces:
Serializable, Comparable<BinaryOperation>, Constable, Operation

public enum BinaryOperation extends Enum<BinaryOperation> implements Operation
Represents binary operations in the Naftah language.

This enum implements Operation and defines various overloaded apply methods to perform operations on different combinations of operand types including numbers, characters, booleans, and strings.

Subclasses/enums implementing this must provide implementations for applying the operation to Number operands, and for mixing Number with Object and String operands.

Utility conversions are performed internally, such as converting booleans to integers and characters to integers to unify the operation logic.

Author:
Chakib Daii
  • Enum Constant Details

    • AND

      public static final BinaryOperation AND
      Logical AND operation.

      Applies short-circuit evaluation:

      • If the left operand is "truthy", returns the right operand.
      • If the left operand is "falsy", returns the left operand.

      Supports numbers, booleans, characters, and strings. Treats values using custom "truthy" rules via isTruthy().

    • OR

      public static final BinaryOperation OR
      Logical OR operation.

      Applies short-circuit evaluation:

      • If the left operand is "truthy", returns the left operand.
      • If the left operand is "falsy", returns the right operand.

      Supports numbers, booleans, characters, and strings. Treats values using custom "truthy" rules via isTruthy().

    • ADD

      public static final BinaryOperation ADD
      Represents the addition operation (+). Supports adding numbers, concatenating strings, and converting booleans and characters appropriately during addition.
    • SUBTRACT

      public static final BinaryOperation SUBTRACT
      Represents the subtraction operation (-). Supports subtracting numbers and converting booleans and characters appropriately during subtraction.
    • MULTIPLY

      public static final BinaryOperation MULTIPLY
      Represents the multiplication operation (*). Supports multiplying numbers and converting booleans and characters appropriately during multiplication.
    • POWER

      public static final BinaryOperation POWER
      Represents the power operation (**). Supports power of numbers and converting booleans and characters appropriately during applying power.
    • DIVIDE

      public static final BinaryOperation DIVIDE
      Represents the division operation (/). Supports dividing numbers and converting booleans and characters appropriately during division.
    • MODULO

      public static final BinaryOperation MODULO
      Represents the modulo operation (%). Calculates the remainder of division between two numeric operands. Supports conversion of booleans and characters to numbers for the operation.
    • GREATER_THAN

      public static final BinaryOperation GREATER_THAN
      Represents the greater-than comparison operation (>). Compares two numeric values or compatible types and returns a boolean indicating whether the left operand is greater than the right operand. Supports conversions from boolean and character types to numbers.
    • GREATER_THAN_EQUALS

      public static final BinaryOperation GREATER_THAN_EQUALS
      Represents the greater-than-or-equal-to comparison operation (>=). Compares two numeric values or compatible types and returns a boolean indicating whether the left operand is greater than or equal to the right operand. Supports conversions from boolean and character types to numbers.
    • LESS_THAN

      public static final BinaryOperation LESS_THAN
      Represents the less-than comparison operation (<). Compares two numeric values or compatible types and returns a boolean indicating whether the left operand is less than the right operand. Supports conversions from boolean and character types to numbers.
    • LESS_THAN_EQUALS

      public static final BinaryOperation LESS_THAN_EQUALS
      Represents the less-than-or-equal-to comparison operation (<=). Compares two numeric values or compatible types and returns a boolean indicating whether the left operand is less than or equal to the right operand. Supports conversions from boolean and character types to numbers.
    • EQUALS

      public static final BinaryOperation EQUALS
      Represents the equality comparison operation (==). Compares two operands for equality and returns a boolean result. Supports numeric, boolean, character, and string comparisons with necessary conversions.
    • NOT_EQUALS

      public static final BinaryOperation NOT_EQUALS
      Represents the inequality comparison operation (!=). Compares two operands for inequality and returns a boolean result. Supports numeric, boolean, character, and string comparisons with necessary conversions.
    • BITWISE_AND

      public static final BinaryOperation BITWISE_AND
      Represents the bitwise AND operation (&). Performs a bitwise AND between two operands. Supports numeric and character operands with appropriate conversions.
    • BITWISE_OR

      public static final BinaryOperation BITWISE_OR
      Represents the bitwise OR operation (|). Performs a bitwise OR between two operands. Supports numeric and character operands with appropriate conversions.
    • BITWISE_XOR

      public static final BinaryOperation BITWISE_XOR
      Represents the bitwise XOR (exclusive OR) operation (^). Performs a bitwise exclusive OR between two operands. Supports numeric and character operands with appropriate conversions.
    • BITWISE_USHR

      public static final BinaryOperation BITWISE_USHR
      Represents the unsigned right bitwise shift operation.

      Shifts the bits of the left-hand operand to the right by the number of positions specified by the right-hand operand, filling with zeros.

      • If operands are numeric, performs a standard unsigned shift.
      • If one operand is non-numeric, its size is used as the shift value.
      • String operands are shifted based on their length.
      Falsy operands result in NaN.
    • BITWISE_SHR

      public static final BinaryOperation BITWISE_SHR
      Represents the signed right bitwise shift operation.

      Shifts the bits of the left-hand operand to the right while preserving the sign bit.

      • If operands are numeric, performs a signed arithmetic shift.
      • If one operand is non-numeric, its size is used as the shift value.
      • String operands are shifted based on their length.
      Falsy operands result in NaN.
    • BITWISE_SHL

      public static final BinaryOperation BITWISE_SHL
      Represents the left bitwise shift operation.

      Shifts the bits of the left-hand operand to the left by the number of positions specified by the right-hand operand.

      • If operands are numeric, performs a standard left shift.
      • If one operand is non-numeric, its size is used as the shift value.
      • String operands are shifted based on their length.
      Falsy operands result in NaN.
    • ELEMENTWISE_ADD

      public static final BinaryOperation ELEMENTWISE_ADD
      Represents element-wise addition. Applies addition operation to each corresponding element in collections or arrays. Supports element-wise combination of compatible data structures.
    • ELEMENTWISE_SUBTRACT

      public static final BinaryOperation ELEMENTWISE_SUBTRACT
      Represents element-wise subtraction. Performs subtraction on corresponding elements in collections or arrays. Supports element-wise operations on compatible data structures.
    • ELEMENTWISE_MULTIPLY

      public static final BinaryOperation ELEMENTWISE_MULTIPLY
      Represents element-wise multiplication. Performs multiplication on corresponding elements in collections or arrays. Supports element-wise operations on compatible data structures.
    • ELEMENTWISE_DIVIDE

      public static final BinaryOperation ELEMENTWISE_DIVIDE
      Represents element-wise division. Performs division on corresponding elements in collections or arrays. Supports element-wise operations on compatible data structures.
    • ELEMENTWISE_MODULO

      public static final BinaryOperation ELEMENTWISE_MODULO
      Represents element-wise modulo operation. Performs modulo on corresponding elements in collections or arrays. Supports element-wise operations on compatible data structures.
    • INSTANCE_OF

      public static final BinaryOperation INSTANCE_OF
      Represents the instanceof type-check operation.

      Evaluates whether the left-hand operand is an instance of the type represented by the right-hand operand.

      • The right-hand operand must be a JavaType.
      • Returns true if the left operand is compatible with the given type.
      • All other operand combinations are considered invalid.
  • Field Details

    • op

      private final String op
      The symbolic representation of the binary operator.
  • Constructor Details

    • BinaryOperation

      private BinaryOperation(String op)
      Constructs a BinaryOperation enum constant with its symbolic operator.
      Parameters:
      op - the operator symbol (e.g., "+", "-", "*")
  • Method Details

    • values

      public static BinaryOperation[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static BinaryOperation valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • newNaftahBugError

      public static NaftahBugError newNaftahBugError(Operation binaryOperation, Object left, Object right)
      Creates a new NaftahBugError indicating that the given binary operation is not supported for the provided operand types.
      Parameters:
      binaryOperation - the binary operation that was attempted
      left - the left operand involved in the operation
      right - the right operand involved in the operation
      Returns:
      a new NaftahBugError describing the unsupported operation and operand types
    • of

      public static BinaryOperation of(String op)
      Returns the BinaryOperation enum constant corresponding to the given operator symbol.
      Parameters:
      op - the operator symbol
      Returns:
      the matching BinaryOperation
      Throws:
      NaftahBugError - if no matching enum constant is found
    • apply

      public Object apply(Object left, Object right)
      Applies the binary operation to two dynamic operands.

      Supports combinations of the following types:

      • Number, Boolean, Character, String
      • Cross-type operations (e.g., Number + String, Boolean + Number, etc.)
      If the operand types are not supported, throws a NaftahBugError.
      Parameters:
      left - the left operand
      right - the right operand
      Returns:
      the result of applying the operation
      Throws:
      NaftahBugError - if the operand types are unsupported
    • handleFalsy

      protected abstract Object handleFalsy(Object left, Object right)
      Handles the case where one or both operands are "falsy" (e.g., null, None, or NaN).

      This method must be implemented by each binary operation to define custom handling logic for falsy values.

      Parameters:
      left - the left operand
      right - the right operand
      Returns:
      the result of the operation after handling falsy values
    • handleFalsyBitOrElementWiseArithmetic

      protected Object handleFalsyBitOrElementWiseArithmetic(Object left, Object right)
      Default handler for falsy values in bitwise or element-wise arithmetic operations.

      If either operand is NaN or None, it is treated as zero.

      Parameters:
      left - the left operand
      right - the right operand
      Returns:
      the result of applying the operation with normalized values
    • handleFalsyArithmetic

      protected Object handleFalsyArithmetic(Object left, Object right)
      Default handler for falsy values in basic arithmetic operations.

      - If either operand is NaN, the result is NaN. - If either operand is None, it is treated as zero.

      Parameters:
      left - the left operand
      right - the right operand
      Returns:
      the result of the arithmetic operation or NaN
    • apply

      protected abstract Object apply(Number left, Number right)
      Applies the binary operation to two Number operands.
      Parameters:
      left - the left operand
      right - the right operand
      Returns:
      the result of the operation
    • apply

      protected Object apply(Number left, char right)
      Applies the operation to a Number left operand and a char right operand.
      Parameters:
      left - the left operand
      right - the right operand as char
      Returns:
      the result of the operation
    • apply

      protected Object apply(char left, Number right)
      Applies the operation to a char left operand and a Number right operand.
      Parameters:
      left - the left operand as char
      right - the right operand
      Returns:
      the result of the operation
    • apply

      protected Object apply(Number left, boolean right)
      Applies the operation to a Number left operand and a boolean right operand.
      Parameters:
      left - the left operand
      right - the right operand as boolean
      Returns:
      the result of the operation
    • apply

      protected Object apply(boolean left, Number right)
      Applies the operation to a boolean left operand and a Number right operand.
      Parameters:
      left - the left operand as boolean
      right - the right operand
      Returns:
      the result of the operation
    • apply

      protected abstract Object apply(Number left, Object right)
      Applies the operation to a Number left operand and a generic Object right operand.
      Parameters:
      left - the left operand
      right - the right operand
      Returns:
      the result of the operation
    • apply

      protected abstract Object apply(Object left, Number right)
      Applies the operation to a generic Object left operand and a Number right operand.
      Parameters:
      left - the left operand
      right - the right operand
      Returns:
      the result of the operation
    • apply

      protected Object apply(char left, char right)
      Applies the operation to two char operands.

      If the result is a Number, it will be cast back to char. If the result is a Boolean, it will be returned as is. Otherwise, returns the raw result.

      Parameters:
      left - the left char operand
      right - the right char operand
      Returns:
      the result of the operation
    • apply

      protected Object apply(boolean left, boolean right)
      Applies the operation to two boolean operands.

      If the result is a Number, it will be converted back to boolean. If the result is a Boolean, it will be returned as is. Otherwise, returns the raw result.

      Parameters:
      left - the left boolean operand
      right - the right boolean operand
      Returns:
      the result of the operation
    • apply

      protected abstract Object apply(String left, String right)
      Applies the operation to two String operands.
      Parameters:
      left - the left string operand
      right - the right string operand
      Returns:
      the result of the operation
    • apply

      protected Object apply(NaftahTemporalPoint left, NaftahTemporalAmount right)
      Applies the operation to an NaftahTemporalPoint and an NaftahTemporalAmount.
      Parameters:
      left - the left ArabicTemporalPoint operand
      right - the right ArabicTemporalAmount operand
      Returns:
      the result of the operation
      Throws:
      NaftahBugError - if this operation is not supported
    • apply

      protected Object apply(NaftahTemporalPoint left, NaftahTemporalPoint right)
      Applies the operation to two NaftahTemporalPoint operands.
      Parameters:
      left - the left ArabicTemporalPoint operand
      right - the right ArabicTemporalPoint operand
      Returns:
      the result of the operation
      Throws:
      NaftahBugError - if this operation is not supported
    • apply

      protected Object apply(NaftahTemporalAmount left, NaftahTemporalAmount right)
      Applies the operation to two NaftahTemporalAmount operands.
      Parameters:
      left - the left ArabicTemporalAmount operand
      right - the right ArabicTemporalAmount operand
      Returns:
      the result of the operation
      Throws:
      NaftahBugError - if this operation is not supported
    • applyLogical

      protected Object applyLogical(Number left, Object right)
      Applies a logical operation where the left operand is a Number. Supports logical interaction with Boolean, Character, and String.
      Parameters:
      left - the left operand (Number)
      right - the right operand (Boolean, Character, or String)
      Returns:
      the result of the logical operation
      Throws:
      NaftahBugError - if the operand types are unsupported
    • applyLogical

      protected Object applyLogical(Object left, Number right)
      Applies a logical operation where the right operand is a Number. Supports logical interaction with Boolean, Character, and String.
      Parameters:
      left - the left operand (Boolean, Character, or String)
      right - the right operand (Number)
      Returns:
      the result of the logical operation
      Throws:
      NaftahBugError - if the operand types are unsupported
    • applyArithmetic

      protected Object applyArithmetic(Number left, Object right)
      Applies an arithmetic operation where the left operand is a Number. Casts the result back to appropriate logical type if necessary.
      Parameters:
      left - the left operand (Number)
      right - the right operand (Boolean, Character, or String)
      Returns:
      the result of the arithmetic operation
      Throws:
      NaftahBugError - if the operand types are unsupported
    • applyArithmetic

      protected Object applyArithmetic(Object left, Number right)
      Applies an arithmetic operation where the right operand is a Number. Casts the result back to appropriate logical type if necessary.
      Parameters:
      left - the left operand (Boolean, Character, or String)
      right - the right operand (Number)
      Returns:
      the result of the arithmetic operation
      Throws:
      NaftahBugError - if the operand types are unsupported
    • toString

      public String toString()
      Returns the string representation of the binary operator symbol.
      Overrides:
      toString in class Enum<BinaryOperation>
      Returns:
      the operator symbol as a string