Enum Class UnaryOperation
- All Implemented Interfaces:
Serializable,Comparable<UnaryOperation>,Constable,Operation
- 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 ConstantDescriptionBITWISE_NOT: Performs a bitwise NOT (~) operation on numeric values.Unary minus operation.Logical NOT operation.Unary plus operation.POST_DECREMENT: Simulates the postfix decrement operation (x--).POST_INCREMENT: Simulates the postfix increment operation (x++).PRE_DECREMENT: Simulates the prefix decrement operation (--x).PRE_INCREMENT: Simulates the prefix increment operation (++x).Represents thesizeofoperation.Represents thetypeofoperation. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe string representation of the decrement operation.static final StringThe string representation of the increment operation.private final StringThe symbolic name of the unary operator (e.g., "PLUS", "MINUS", "NOT").static final StringRepresents the postfix position of an operator, such asx++orx--.static final StringRepresents the prefix position of an operator, such as++xor--x. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateUnaryOperation(String op) Constructs aUnaryOperationenum constant with its symbolic name. -
Method Summary
Modifier and TypeMethodDescriptionprotected Objectapply(boolean b) Applies the unary operation to abooleanoperand.protected Objectapply(char c) Applies the unary operation to acharoperand.protected abstract ObjectApplies the unary operation to aNumberoperand.Applies this unary operation to a dynamically typed operand.protected abstract ObjectApplies the unary operation to aStringoperand.protected ObjecthandleFalsy(Object object) Handles the application of this unary operation to a "falsy" value, such asnull,NaN, orNone.static NaftahBugErrornewNaftahBugError(Operation unaryOperation, Object o) Constructs aNaftahBugErrorto indicate that the specified unary operation is not supported for the given operand type.static UnaryOperationResolves aUnaryOperationby its operator string.toString()Returns the string representation of this unary operation, which is its symbolic operator.static UnaryOperationReturns the enum constant of this class with the specified name.static UnaryOperation[]values()Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
BITWISE_NOT
BITWISE_NOT: Performs a bitwise NOT (~) operation on numeric values.For integers, it inverts each bit. For characters and booleans, it first converts them to their integer representations, applies the operation, and converts back to the original type. Strings are unsupported and will typically throw a runtime exception.
-
PRE_INCREMENT
PRE_INCREMENT: Simulates the prefix increment operation (++x).Increases the numeric value by 1 and returns the result. For characters and booleans, it first converts them to integers, applies the increment, and converts back to the original type. Strings are unsupported and will typically result in a runtime error.
-
POST_INCREMENT
POST_INCREMENT: Simulates the postfix increment operation (x++).Returns the original numeric value before incrementing it by 1. For characters and booleans, it converts them to integers, stores the original, applies the increment, and returns the original value as the result. Strings are unsupported and will typically result in a runtime error.
-
PRE_DECREMENT
PRE_DECREMENT: Simulates the prefix decrement operation (--x).Decreases the numeric value by 1 and returns the result. For characters and booleans, it converts them to integers, applies the decrement, and then converts back to the original type. Strings are unsupported and will typically result in a runtime error.
-
POST_DECREMENT
POST_DECREMENT: Simulates the postfix decrement operation (x--).Returns the original numeric value before decreasing it by 1. For characters and booleans, the operation converts them to integers, performs the decrement, and then casts back to the original type. Strings are not supported and will result in an error if used.
-
PLUS
Unary plus operation.Returns the number as-is. For strings, returns
NaNto indicate invalid usage. -
MINUS
Unary minus operation.Negates numeric values. For strings, returns
NaNto indicate invalid usage. -
NOT
Logical NOT operation.Applies a "falsy" check to numeric or string values and returns the opposite boolean. Uses the custom
not(...)logic based on "truthy"/"falsy" evaluation. -
TYPE_OF
Represents thetypeofoperation.Returns the runtime language type of the given operand as a Naftah type descriptor.
- Resolves the operand's
JavaType. - Maps the Java type to the corresponding Naftah type using the parser vocabulary.
- Resolves the operand's
-
SIZE_OF
Represents thesizeofoperation.Computes the logical size of the given operand.
- For collections, returns the number of elements.
- For arrays, returns their length.
- For strings, returns the character count.
- For unsupported types, behavior is delegated to
ObjectUtils.size(Object).
-
-
Field Details
-
POST
Represents the postfix position of an operator, such asx++orx--. Used to indicate that the operator comes after the operand.- See Also:
-
PRE
Represents the prefix position of an operator, such as++xor--x. Used to indicate that the operator comes before the operand.- See Also:
-
INCREMENT
The string representation of the increment operation. Typically used to represent++in the language grammar or runtime logic.- See Also:
-
DECREMENT
The string representation of the decrement operation. Typically used to represent--in the language grammar or runtime logic.- See Also:
-
op
The symbolic name of the unary operator (e.g., "PLUS", "MINUS", "NOT").
-
-
Constructor Details
-
UnaryOperation
Constructs aUnaryOperationenum constant with its symbolic name.- Parameters:
op- the symbolic name of the operation
-
-
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
Constructs aNaftahBugErrorto indicate that the specified unary operation is not supported for the given operand type.- Parameters:
unaryOperation- the unary operation attemptedo- the operand for which the operation is not supported- Returns:
- a
NaftahBugErrorwith a descriptive error message
-
of
Resolves aUnaryOperationby its operator string.Accepts exact match, or variants with
PREorPOSTprefixes (e.g.,"++","PRE++","POST++").- Parameters:
op- the operator string (e.g., "NOT", "++", "MINUS")- Returns:
- the matching
UnaryOperationenum constant - Throws:
NaftahBugError- if no matching operation is found
-
apply
Applies this unary operation to a dynamically typed operand.Supports
Number,Boolean,Character, andString. If the value isNaN, it is returned as-is.- Parameters:
object- the operand to apply the operation to- Returns:
- the result of applying the operation
- Throws:
NaftahBugError- if the operand type is unsupported
-
handleFalsy
Handles the application of this unary operation to a "falsy" value, such asnull,NaN, orNone.By default, this method throws a
NaftahBugErrorto indicate that the operation is not supported for the given falsy input. Subclasses or enum constants may override this to provide custom behavior.- Parameters:
object- the input object considered "falsy"- Returns:
- the result of applying the operation, if overridden
- Throws:
NaftahBugError- if the operation is not valid for the input
-
apply
Applies the unary operation to aNumberoperand. -
apply
Applies the unary operation to acharoperand. Internally converts the character to its integer code point, applies the operation, and casts the result back tochar.- Parameters:
c- the character operand- Returns:
- the result of the operation as a character or
NaN
-
apply
Applies the unary operation to abooleanoperand. Converts the boolean to an integer (true → 1, false → 0), applies the operation, and converts the result back to boolean.- Parameters:
b- the boolean operand- Returns:
- the result of the operation as a boolean or
NaN
-
apply
Applies the unary operation to aStringoperand. The implementation is operation-specific.- Parameters:
string- the string operand- Returns:
- the result of the operation as a string or
NaN
-
toString
Returns the string representation of this unary operation, which is its symbolic operator.- Overrides:
toStringin classEnum<UnaryOperation>- Returns:
- the operator string (e.g., "PLUS", "MINUS", "NOT")
-