Class Binary

java.lang.Object
dev.vortex.api.expressions.Binary
All Implemented Interfaces:
Expression

public final class Binary extends Object implements Expression
Represents a binary expression that operates on two child expressions using a binary operator. Binary expressions support comparison operations (equality, inequality, relational comparisons) and boolean algebra operations (AND, OR).

This class is immutable and implements the Expression interface, making it suitable for use in expression trees and query processing pipelines.

Example usage:

 // Create equality comparison: left == right
 Binary equalExpr = Binary.eq(leftExpr, rightExpr);

 // Create logical AND: expr1 && expr2 && expr3
 Binary andExpr = Binary.and(expr1, expr2, expr3);

 // Create greater than comparison: left > right
 Binary gtExpr = Binary.gt(leftExpr, rightExpr);
 
  • Method Details

    • parse

      public static Binary parse(byte[] metadata, List<Expression> children)
      Parses a Binary expression from protobuf metadata and child expressions.
      Parameters:
      metadata - the serialized protobuf metadata containing the binary operator
      children - the list of child expressions, must contain exactly 2 expressions
      Returns:
      a new Binary expression instance
      Throws:
      RuntimeException - if children size is not 2 or if metadata parsing fails
    • of

      public static Binary of(Binary.BinaryOp operator, Expression left, Expression right)
      Creates a new Binary expression with the specified operator and operands.
      Parameters:
      operator - the binary operator to apply
      left - the left operand expression
      right - the right operand expression
      Returns:
      a new Binary expression instance
    • and

      public static Binary and(Expression first, Expression... rest)
      Creates a logical AND expression combining multiple expressions. If only one expression is provided, it returns an AND with a literal true. Multiple expressions are right-associated: first && (rest[0] && rest[1] && ...).
      Parameters:
      first - the first expression (left operand)
      rest - additional expressions to AND together (right operands)
      Returns:
      a new Binary expression representing the logical AND operation
    • or

      public static Binary or(Expression first, Expression... rest)
      Creates a logical OR expression combining multiple expressions. If only one expression is provided, it returns an OR with a literal false. Multiple expressions are right-associated: first || (rest[0] || rest[1] || ...).
      Parameters:
      first - the first expression (left operand)
      rest - additional expressions to OR together (right operands)
      Returns:
      a new Binary expression representing the logical OR operation
    • eq

      public static Binary eq(Expression left, Expression right)
      Creates an equality comparison expression (==).
      Parameters:
      left - the left operand expression
      right - the right operand expression
      Returns:
      a new Binary expression representing left == right
    • notEq

      public static Binary notEq(Expression left, Expression right)
      Creates an inequality comparison expression (!=).
      Parameters:
      left - the left operand expression
      right - the right operand expression
      Returns:
      a new Binary expression representing left != right
    • gt

      public static Binary gt(Expression left, Expression right)
      Creates a greater-than comparison expression (>).
      Parameters:
      left - the left operand expression
      right - the right operand expression
      Returns:
      a new Binary expression representing left > right
    • gtEq

      public static Binary gtEq(Expression left, Expression right)
      Creates a greater-than-or-equal comparison expression (>=).
      Parameters:
      left - the left operand expression
      right - the right operand expression
      Returns:
      a new Binary expression representing left >= right
    • lt

      public static Binary lt(Expression left, Expression right)
      Creates a less-than comparison expression (<).
      Parameters:
      left - the left operand expression
      right - the right operand expression
      Returns:
      a new Binary expression representing left < right
    • ltEq

      public static Binary ltEq(Expression left, Expression right)
      Creates a less-than-or-equal comparison expression (<=).
      Parameters:
      left - the left operand expression
      right - the right operand expression
      Returns:
      a new Binary expression representing left <= right
    • id

      public String id()
      Description copied from interface: Expression
      The globally unique identifier for this type of expression.
      Specified by:
      id in interface Expression
    • children

      public List<Expression> children()
      Description copied from interface: Expression
      Returns the children of this expression.
      Specified by:
      children in interface Expression
    • metadata

      public Optional<byte[]> metadata()
      Description copied from interface: Expression
      Returns the serialized metadata for this expression, or empty if serialization is not supported.
      Specified by:
      metadata in interface Expression
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • accept

      public <T> T accept(Expression.Visitor<T> visitor)
      Description copied from interface: Expression
      Accepts a visitor and dispatches to the appropriate visit method based on the expression type. This method implements the visitor pattern, allowing different operations to be performed on expressions without modifying the expression classes themselves.
      Specified by:
      accept in interface Expression
      Type Parameters:
      T - the return type of the visitor
      Parameters:
      visitor - the visitor to accept
      Returns:
      the result of the visitor's operation on this expression
    • getOperator

      public Binary.BinaryOp getOperator()
      Returns the binary operator used in this expression.
      Returns:
      the binary operator
    • getLeft

      public Expression getLeft()
      Returns the left operand expression.
      Returns:
      the left operand expression
    • getRight

      public Expression getRight()
      Returns the right operand expression.
      Returns:
      the right operand expression