Enum Class BinaryOperation
- All Implemented Interfaces:
Serializable,Comparable<BinaryOperation>,Constable,Operation
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
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>> -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionRepresents the addition operation (+).Logical AND operation.Represents the bitwise AND operation (&).Represents the bitwise OR operation (|).Represents the left bitwise shift operation.Represents the signed right bitwise shift operation.Represents the unsigned right bitwise shift operation.Represents the bitwise XOR (exclusive OR) operation (^).Represents the division operation (/).Represents element-wise addition.Represents element-wise division.Represents element-wise modulo operation.Represents element-wise multiplication.Represents element-wise subtraction.Represents the equality comparison operation (==).Represents the greater-than comparison operation (>).Represents the greater-than-or-equal-to comparison operation (>=).Represents theinstanceoftype-check operation.Represents the less-than comparison operation (<).Represents the less-than-or-equal-to comparison operation (<=).Represents the modulo operation (%).Represents the multiplication operation (*).Represents the inequality comparison operation (!=).Logical OR operation.Represents the power operation (**).Represents the subtraction operation (-). -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateConstructs aBinaryOperationenum constant with its symbolic operator. -
Method Summary
Modifier and TypeMethodDescriptionprotected Objectapply(boolean left, boolean right) Applies the operation to twobooleanoperands.protected ObjectApplies the operation to abooleanleft operand and aNumberright operand.protected Objectapply(char left, char right) Applies the operation to twocharoperands.protected ObjectApplies the operation to acharleft operand and aNumberright operand.protected ObjectApplies the operation to aNumberleft operand and abooleanright operand.protected ObjectApplies the operation to aNumberleft operand and acharright operand.protected abstract ObjectApplies the binary operation to twoNumberoperands.protected abstract Objectprotected abstract ObjectApplies the binary operation to two dynamic operands.protected abstract ObjectApplies the operation to twoStringoperands.protected Objectapply(NaftahTemporalAmount left, NaftahTemporalAmount right) Applies the operation to twoNaftahTemporalAmountoperands.protected Objectapply(NaftahTemporalPoint left, NaftahTemporalAmount right) Applies the operation to anNaftahTemporalPointand anNaftahTemporalAmount.protected Objectapply(NaftahTemporalPoint left, NaftahTemporalPoint right) Applies the operation to twoNaftahTemporalPointoperands.protected ObjectapplyArithmetic(Number left, Object right) Applies an arithmetic operation where the left operand is aNumber.protected ObjectapplyArithmetic(Object left, Number right) Applies an arithmetic operation where the right operand is aNumber.protected ObjectapplyLogical(Number left, Object right) Applies a logical operation where the left operand is aNumber.protected ObjectapplyLogical(Object left, Number right) Applies a logical operation where the right operand is aNumber.protected abstract ObjecthandleFalsy(Object left, Object right) Handles the case where one or both operands are "falsy" (e.g.,null,None, orNaN).protected ObjecthandleFalsyArithmetic(Object left, Object right) Default handler for falsy values in basic arithmetic operations.protected ObjecthandleFalsyBitOrElementWiseArithmetic(Object left, Object right) Default handler for falsy values in bitwise or element-wise arithmetic operations.static NaftahBugErrornewNaftahBugError(Operation binaryOperation, Object left, Object right) Creates a newNaftahBugErrorindicating that the given binary operation is not supported for the provided operand types.static BinaryOperationReturns theBinaryOperationenum constant corresponding to the given operator symbol.toString()Returns the string representation of the binary operator symbol.static BinaryOperationReturns the enum constant of this class with the specified name.static BinaryOperation[]values()Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
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
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
Represents the addition operation (+). Supports adding numbers, concatenating strings, and converting booleans and characters appropriately during addition. -
SUBTRACT
Represents the subtraction operation (-). Supports subtracting numbers and converting booleans and characters appropriately during subtraction. -
MULTIPLY
Represents the multiplication operation (*). Supports multiplying numbers and converting booleans and characters appropriately during multiplication. -
POWER
Represents the power operation (**). Supports power of numbers and converting booleans and characters appropriately during applying power. -
DIVIDE
Represents the division operation (/). Supports dividing numbers and converting booleans and characters appropriately during division. -
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
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
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
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
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
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
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
Represents the bitwise AND operation (&). Performs a bitwise AND between two operands. Supports numeric and character operands with appropriate conversions. -
BITWISE_OR
Represents the bitwise OR operation (|). Performs a bitwise OR between two operands. Supports numeric and character operands with appropriate conversions. -
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
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
sizeis used as the shift value. - String operands are shifted based on their length.
NaN. -
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
sizeis used as the shift value. - String operands are shifted based on their length.
NaN. -
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
sizeis used as the shift value. - String operands are shifted based on their length.
NaN. -
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
Represents element-wise subtraction. Performs subtraction on corresponding elements in collections or arrays. Supports element-wise operations on compatible data structures. -
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
Represents element-wise division. Performs division on corresponding elements in collections or arrays. Supports element-wise operations on compatible data structures. -
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
Represents theinstanceoftype-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
trueif the left operand is compatible with the given type. - All other operand combinations are considered invalid.
- The right-hand operand must be a
-
-
Field Details
-
op
The symbolic representation of the binary operator.
-
-
Constructor Details
-
BinaryOperation
Constructs aBinaryOperationenum constant with its symbolic operator.- Parameters:
op- the operator symbol (e.g., "+", "-", "*")
-
-
Method Details
-
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
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 nameNullPointerException- if the argument is null
-
newNaftahBugError
public static NaftahBugError newNaftahBugError(Operation binaryOperation, Object left, Object right) Creates a newNaftahBugErrorindicating that the given binary operation is not supported for the provided operand types.- Parameters:
binaryOperation- the binary operation that was attemptedleft- the left operand involved in the operationright- the right operand involved in the operation- Returns:
- a new
NaftahBugErrordescribing the unsupported operation and operand types
-
of
Returns theBinaryOperationenum 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
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.)
NaftahBugError.- Parameters:
left- the left operandright- the right operand- Returns:
- the result of applying the operation
- Throws:
NaftahBugError- if the operand types are unsupported
-
handleFalsy
Handles the case where one or both operands are "falsy" (e.g.,null,None, orNaN).This method must be implemented by each binary operation to define custom handling logic for falsy values.
- Parameters:
left- the left operandright- the right operand- Returns:
- the result of the operation after handling falsy values
-
handleFalsyBitOrElementWiseArithmetic
Default handler for falsy values in bitwise or element-wise arithmetic operations.If either operand is
NaNorNone, it is treated as zero.- Parameters:
left- the left operandright- the right operand- Returns:
- the result of applying the operation with normalized values
-
handleFalsyArithmetic
Default handler for falsy values in basic arithmetic operations.- If either operand is
NaN, the result isNaN. - If either operand isNone, it is treated as zero.- Parameters:
left- the left operandright- the right operand- Returns:
- the result of the arithmetic operation or
NaN
-
apply
Applies the binary operation to twoNumberoperands.- Parameters:
left- the left operandright- the right operand- Returns:
- the result of the operation
-
apply
Applies the operation to aNumberleft operand and acharright operand.- Parameters:
left- the left operandright- the right operand as char- Returns:
- the result of the operation
-
apply
Applies the operation to acharleft operand and aNumberright operand.- Parameters:
left- the left operand as charright- the right operand- Returns:
- the result of the operation
-
apply
Applies the operation to aNumberleft operand and abooleanright operand.- Parameters:
left- the left operandright- the right operand as boolean- Returns:
- the result of the operation
-
apply
Applies the operation to abooleanleft operand and aNumberright operand.- Parameters:
left- the left operand as booleanright- the right operand- Returns:
- the result of the operation
-
apply
- Parameters:
left- the left operandright- the right operand- Returns:
- the result of the operation
-
apply
- Parameters:
left- the left operandright- the right operand- Returns:
- the result of the operation
-
apply
Applies the operation to twocharoperands.If the result is a
Number, it will be cast back tochar. If the result is aBoolean, it will be returned as is. Otherwise, returns the raw result.- Parameters:
left- the left char operandright- the right char operand- Returns:
- the result of the operation
-
apply
Applies the operation to twobooleanoperands.If the result is a
Number, it will be converted back toboolean. If the result is aBoolean, it will be returned as is. Otherwise, returns the raw result.- Parameters:
left- the left boolean operandright- the right boolean operand- Returns:
- the result of the operation
-
apply
Applies the operation to twoStringoperands.- Parameters:
left- the left string operandright- the right string operand- Returns:
- the result of the operation
-
apply
Applies the operation to anNaftahTemporalPointand anNaftahTemporalAmount.- Parameters:
left- the left ArabicTemporalPoint operandright- the right ArabicTemporalAmount operand- Returns:
- the result of the operation
- Throws:
NaftahBugError- if this operation is not supported
-
apply
Applies the operation to twoNaftahTemporalPointoperands.- Parameters:
left- the left ArabicTemporalPoint operandright- the right ArabicTemporalPoint operand- Returns:
- the result of the operation
- Throws:
NaftahBugError- if this operation is not supported
-
apply
Applies the operation to twoNaftahTemporalAmountoperands.- Parameters:
left- the left ArabicTemporalAmount operandright- the right ArabicTemporalAmount operand- Returns:
- the result of the operation
- Throws:
NaftahBugError- if this operation is not supported
-
applyLogical
Applies a logical operation where the left operand is aNumber. 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
Applies a logical operation where the right operand is aNumber. 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
Applies an arithmetic operation where the left operand is aNumber. 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
Applies an arithmetic operation where the right operand is aNumber. 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
Returns the string representation of the binary operator symbol.- Overrides:
toStringin classEnum<BinaryOperation>- Returns:
- the operator symbol as a string
-