Enum Class UnaryOperation

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

public enum UnaryOperation extends Enum<UnaryOperation> implements Operation
Represents all unary operations supported by the Naftah language. Unary operations take a single operand and return a result after applying a transformation (e.g., negation, logical not, etc.).
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 Constants
    Enum Constant
    Description
    BITWISE_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 the sizeof operation.
    Represents the typeof operation.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The string representation of the decrement operation.
    static final String
    The string representation of the increment operation.
    private final String
    The symbolic name of the unary operator (e.g., "PLUS", "MINUS", "NOT").
    static final String
    Represents the postfix position of an operator, such as x++ or x--.
    static final String
    Represents the prefix position of an operator, such as ++x or --x.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Constructs a UnaryOperation enum constant with its symbolic name.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected Object
    apply(boolean b)
    Applies the unary operation to a boolean operand.
    protected Object
    apply(char c)
    Applies the unary operation to a char operand.
    protected abstract Object
    apply(Number number)
    Applies the unary operation to a Number operand.
    apply(Object object)
    Applies this unary operation to a dynamically typed operand.
    protected abstract Object
    apply(String string)
    Applies the unary operation to a String operand.
    protected Object
    Handles the application of this unary operation to a "falsy" value, such as null, NaN, or None.
    newNaftahBugError(Operation unaryOperation, Object o)
    Constructs a NaftahBugError to indicate that the specified unary operation is not supported for the given operand type.
    of(String op)
    Resolves a UnaryOperation by its operator string.
    Returns the string representation of this unary operation, which is its symbolic operator.
    Returns the enum constant of this class with the specified name.
    Returns an array containing the constants of this enum class, in the order they are declared.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Enum Constant Details

    • BITWISE_NOT

      public static final UnaryOperation 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

      public static final UnaryOperation 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

      public static final UnaryOperation 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

      public static final UnaryOperation 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

      public static final UnaryOperation 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

      public static final UnaryOperation PLUS
      Unary plus operation.

      Returns the number as-is. For strings, returns NaN to indicate invalid usage.

    • MINUS

      public static final UnaryOperation MINUS
      Unary minus operation.

      Negates numeric values. For strings, returns NaN to indicate invalid usage.

    • NOT

      public static final UnaryOperation 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

      public static final UnaryOperation TYPE_OF
      Represents the typeof operation.

      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.
    • SIZE_OF

      public static final UnaryOperation SIZE_OF
      Represents the sizeof operation.

      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

      public static final String POST
      Represents the postfix position of an operator, such as x++ or x--. Used to indicate that the operator comes after the operand.
      See Also:
    • PRE

      public static final String PRE
      Represents the prefix position of an operator, such as ++x or --x. Used to indicate that the operator comes before the operand.
      See Also:
    • INCREMENT

      public static final String INCREMENT
      The string representation of the increment operation. Typically used to represent ++ in the language grammar or runtime logic.
      See Also:
    • DECREMENT

      public static final String DECREMENT
      The string representation of the decrement operation. Typically used to represent -- in the language grammar or runtime logic.
      See Also:
    • op

      private final String op
      The symbolic name of the unary operator (e.g., "PLUS", "MINUS", "NOT").
  • Constructor Details

    • UnaryOperation

      private UnaryOperation(String op)
      Constructs a UnaryOperation enum constant with its symbolic name.
      Parameters:
      op - the symbolic name of the operation
  • Method Details

    • values

      public static UnaryOperation[] 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 UnaryOperation 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 unaryOperation, Object o)
      Constructs a NaftahBugError to indicate that the specified unary operation is not supported for the given operand type.
      Parameters:
      unaryOperation - the unary operation attempted
      o - the operand for which the operation is not supported
      Returns:
      a NaftahBugError with a descriptive error message
    • of

      public static UnaryOperation of(String op)
      Resolves a UnaryOperation by its operator string.

      Accepts exact match, or variants with PRE or POST prefixes (e.g., "++", "PRE++" , "POST++").

      Parameters:
      op - the operator string (e.g., "NOT", "++", "MINUS")
      Returns:
      the matching UnaryOperation enum constant
      Throws:
      NaftahBugError - if no matching operation is found
    • apply

      public Object apply(Object object)
      Applies this unary operation to a dynamically typed operand.

      Supports Number, Boolean, Character, and String. If the value is NaN, 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

      protected Object handleFalsy(Object object)
      Handles the application of this unary operation to a "falsy" value, such as null, NaN, or None.

      By default, this method throws a NaftahBugError to 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

      protected abstract Object apply(Number number)
      Applies the unary operation to a Number operand.
      Parameters:
      number - the numeric operand
      Returns:
      the result of the unary operation as a Number or NaN
    • apply

      protected Object apply(char c)
      Applies the unary operation to a char operand. Internally converts the character to its integer code point, applies the operation, and casts the result back to char.
      Parameters:
      c - the character operand
      Returns:
      the result of the operation as a character or NaN
    • apply

      protected Object apply(boolean b)
      Applies the unary operation to a boolean operand. 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

      protected abstract Object apply(String string)
      Applies the unary operation to a String operand. The implementation is operation-specific.
      Parameters:
      string - the string operand
      Returns:
      the result of the operation as a string or NaN
    • toString

      public String toString()
      Returns the string representation of this unary operation, which is its symbolic operator.
      Overrides:
      toString in class Enum<UnaryOperation>
      Returns:
      the operator string (e.g., "PLUS", "MINUS", "NOT")