Class Builtin
This class contains static methods that implement fundamental operations such as addition, subtraction, multiplication, division, logical comparisons, bitwise operations, and printing. All functions are annotated to be recognized by the Naftah interpreter.
Note: This class cannot be instantiated.
- Author:
- Chakib Daii
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabs(T x) The (abs) function is used to calculate the absolute value of the given number, i.e., removing the negative sign if present.static <T> Objectadd(T left, T right) Adds two values and returns their sum.static <T> Objectand(T left, T right) The (and) function performs a bitwise AND operation on the given numbers.ceil(T x) The (ceil) function returns the smallest integer greater than or equal to the given number.static <T> Objectdivide(T left, T right) Divides the first value by the second and returns the quotient.static <T> ObjectelementWiseAdd(T left, T right) Performs element-wise addition of two values.static <T> ObjectelementWiseDivide(T left, T right) Performs element-wise division of two values.static <T> ObjectelementWiseModulo(T left, T right) Performs element-wise modulo of two values.static <T> ObjectelementWiseMultiply(T left, T right) Performs element-wise multiplication of two values.static <T> ObjectelementWiseSubtract(T left, T right) Performs element-wise subtraction of two values.static <T> booleanequals(T left, T right) The (equals) function compares two values to check if they are equal.floor(T x) The (floor) function returns the largest integer less than or equal to the given number.static StringReturns a string representing the type of the provided object according to the Naftah type system.static <T> booleangreaterThan(T left, T right) The (greaterThan) function checks if the first value is greater than the second.static <T> booleangreaterThanEquals(T left, T right) The (greaterThanEquals) function checks if the first value is greater than or equal to the second.static booleaninstanceOf(Object obj, JavaType javaType) Checks whether the given object is an instance of the specifiedJavaType.static <T extends Number>
booleanisZero(T x) The (isZero) function checks if the given number is zero.static <T> booleanlessThan(T left, T right) The (lessThan) function checks if the first value is less than the second.static <T> booleanlessThanEquals(T left, T right) The (lessThanEquals) function checks if the first value is less than or equal to the second.static <T> ObjectlogicalAnd(T left, T right) Logical AND operation with short-circuit evaluation.static <T> ObjectlogicalNot(T x) The (logicalNot) function returns the logical negation of the given value.static <T> ObjectlogicalOr(T left, T right) Logical OR operation with short-circuit evaluation.max(T left, T right) The (max) function is used to compare two numbers and return the larger one.min(T left, T right) The (min) function is used to compare two numbers and return the smaller one.static <T> Objectmodulo(T left, T right) Calculates the remainder of the division of the first value by the second.static <T> Objectmultiply(T left, T right) Multiplies two values and returns the product.negate(T x) The (negate) function returns the given number after changing it to its negative value.static <T> Objectnot(T x) The (not) function performs a bitwise NOT operation on the given number.static <T> booleannotEquals(T left, T right) The (notEquals) function compares two values to check if they are not equal.static <T> Objector(T left, T right) The (or) function performs a bitwise OR operation on the given numbers.static NumberparseDynamicNumber(String text, DynamicNumber radix) Parses a textual number according to a specified dynamic numeric system (radix) and returns the most appropriate numeric type.static <T> ObjectpostDecrement(T x) The (postDecrement) function decreases the given number by one after using it in the expression.static <T> ObjectpostIncrement(T x) The (postIncrement) function increases the given number by one after using it in the expression.static <T> Objectpow(T left, T right) Raises a value to a given power.static <T> ObjectpreDecrement(T x) The (preDecrement) function decreases the given number by one before using it in the expression.static <T> ObjectpreIncrement(T x) The (preIncrement) function increases the given number by one before using it in the expression.static voidPrints the string representation of the given object to the output.round(T x) The (round) function is used to round a decimal number to the nearest integer.shiftLeft(T x, T positions) The (shiftLeft) function performs a bitwise left shift on the given number by a specified number of positions.shiftRight(T x, T positions) The (shiftRight) function performs a bitwise right shift on the given number by a specified number of positions.static <T extends Number>
intsignum(T x) The (signum) function is used to determine the sign of the given number: returns -1 if the number is negative, 0 if zero, and 1 if positive.static NumberComputes the size of an object in a generic manner.sqrt(T x) The (sqrt) function is used to calculate the square root of the given number.static <T> Objectsubtract(T left, T right) Subtracts the second value from the first and returns the difference.static StringReturns the string representation of the given object.unsignedShiftRight(T x, T positions) The (unsignedShiftRight) function performs an unsigned bitwise right shift on the given number by a specified number of positions.static <T> Objectxor(T left, T right) The (xor) function performs a bitwise XOR operation on the given numbers.
-
Constructor Details
-
Builtin
private Builtin()Private constructor to prevent instantiation. ThrowsNaftahBugErrorif called.
-
-
Method Details
-
print
Prints the string representation of the given object to the output. If the object is null, prints a predefined NULL representation.- Parameters:
o- the object to print; can be any object
-
toString
Returns the string representation of the given object. If the object isnull, returns a predefined NULL string.- Parameters:
o- the object to convert to string- Returns:
- a string representation of the object, or a NULL representation if the object is null
-
add
Adds two values and returns their sum.- Type Parameters:
T- the type of the operands- Parameters:
left- the first valueright- the second value- Returns:
- the sum of left and right
-
logicalAnd
Logical AND operation with short-circuit evaluation.Returns the second operand if the first operand is "truthy"; otherwise returns the first operand.
Supports numbers, booleans, characters, and strings. Uses custom "truthy" rules via
isTruthy()to determine truthiness:- Numbers: non-zero values are truthy.
- Booleans:
trueis truthy,falseis falsy. - Characters: non-null, non-zero characters are truthy.
- Strings: non-empty strings are truthy.
- Type Parameters:
T- the type of operands (Number, Boolean, Character, String)- Parameters:
left- the first valueright- the second value- Returns:
- the second operand if the first is truthy, otherwise the first operand
-
logicalOr
Logical OR operation with short-circuit evaluation.Returns the first operand if it is "truthy"; otherwise returns the second operand.
Supports numbers, booleans, characters, and strings. Uses custom "truthy" rules via
isTruthy()to determine truthiness:- Numbers: non-zero values are truthy.
- Booleans:
trueis truthy,falseis falsy. - Characters: non-null, non-zero characters are truthy.
- Strings: non-empty strings are truthy.
- Type Parameters:
T- the type of operands (Number, Boolean, Character, String)- Parameters:
left- the first valueright- the second value- Returns:
- the first "truthy" operand, or the second operand if the first is falsy
-
subtract
Subtracts the second value from the first and returns the difference.- Type Parameters:
T- the type of the operands- Parameters:
left- the minuendright- the subtrahend- Returns:
- the difference between left and right
-
multiply
Multiplies two values and returns the product.- Type Parameters:
T- the type of the operands- Parameters:
left- the first factorright- the second factor- Returns:
- the product of left and right
-
pow
Raises a value to a given power.Computes the result of raising the first operand (base) to the second operand (exponent). Supports numbers and compatible numeric types.
- Type Parameters:
T- the type of the operands- Parameters:
left- the base valueright- the exponent value- Returns:
- the result of left raised to the power of right
-
divide
Divides the first value by the second and returns the quotient.- Type Parameters:
T- the type of the operands- Parameters:
left- the dividendright- the divisor- Returns:
- the quotient of left divided by right
-
modulo
Calculates the remainder of the division of the first value by the second.- Type Parameters:
T- the type of the operands- Parameters:
left- the dividendright- the divisor- Returns:
- the remainder after dividing left by right
-
max
The (max) function is used to compare two numbers and return the larger one. This operation is useful for determining the higher value when comparing two numeric values.- Type Parameters:
T- The type of number, must be Number or a subclass- Parameters:
left- The first number to compareright- The second number to compare- Returns:
- The larger number between left and right
-
min
The (min) function is used to compare two numbers and return the smaller one. This operation is useful for determining the lower value when comparing two numeric values.- Type Parameters:
T- The type of number, must be Number or a subclass- Parameters:
left- The first number to compareright- The second number to compare- Returns:
- The smaller number between left and right
-
pow
The (pow) function is used to raise a base number to an integer exponent. This operation is useful in mathematical calculations requiring exponential repetition such as squares or cubes.- Type Parameters:
T- The type of the base, must be Number or a subclass- Parameters:
base- The base number to be raisedexponent- The power to which the base is raised (integer)- Returns:
- The result of raising the base to the specified power
-
round
The (round) function is used to round a decimal number to the nearest integer. This operation is useful in calculations requiring precise integers.- Type Parameters:
T- The type of decimal number, must be Number or a subclass- Parameters:
x- The decimal number to round- Returns:
- The number rounded to the nearest integer
-
floor
The (floor) function returns the largest integer less than or equal to the given number. This operation is useful in calculations that require rounding down numbers.- Type Parameters:
T- The type of number, must be Number or a subclass- Parameters:
x- The number to floor- Returns:
- The largest integer less than or equal to the given number
-
ceil
The (ceil) function returns the smallest integer greater than or equal to the given number. This operation is useful in calculations that require rounding up numbers.- Type Parameters:
T- The type of number, must be Number or a subclass- Parameters:
x- The number to ceil- Returns:
- The smallest integer greater than or equal to the given number
-
negate
The (negate) function returns the given number after changing it to its negative value. This operation is useful in calculations that require reversing the numeric sign.- Type Parameters:
T- The type of number, must be Number or a subclass- Parameters:
x- The number to negate- Returns:
- The given number after changing it to its negative value
-
sqrt
The (sqrt) function is used to calculate the square root of the given number. This operation is useful in calculations that require finding a number whose square equals the original number.- Type Parameters:
T- The type of number, must be Number or a subclass- Parameters:
x- The number to calculate the square root of- Returns:
- The square root of the given number
-
abs
The (abs) function is used to calculate the absolute value of the given number, i.e., removing the negative sign if present. This operation is useful in calculations that require a positive value always.- Type Parameters:
T- The type of number, must be Number or a subclass- Parameters:
x- The number to calculate the absolute value for- Returns:
- The absolute value of the given number
-
signum
The (signum) function is used to determine the sign of the given number: returns -1 if the number is negative, 0 if zero, and 1 if positive.- Type Parameters:
T- The type of number, must be Number or a subclass- Parameters:
x- The number to determine its sign- Returns:
- -1 if negative, 0 if zero, 1 if positive
-
isZero
The (isZero) function checks if the given number is zero. Returns true if the number is zero, false otherwise.- Type Parameters:
T- The type of number, must be Number or subclass- Parameters:
x- The number to check- Returns:
- true if x is zero, false otherwise
-
equals
public static <T> boolean equals(T left, T right) The (equals) function compares two values to check if they are equal. Returns true if both are equal, false otherwise.- Type Parameters:
T- The type of the objects being compared- Parameters:
left- The first objectright- The second object- Returns:
- true if left equals right, false otherwise
-
notEquals
public static <T> boolean notEquals(T left, T right) The (notEquals) function compares two values to check if they are not equal. Returns true if they are different, false otherwise.- Type Parameters:
T- The type of the objects being compared- Parameters:
left- The first objectright- The second object- Returns:
- true if left does not equal right, false otherwise
-
lessThan
public static <T> boolean lessThan(T left, T right) The (lessThan) function checks if the first value is less than the second. Returns true if left is less than right, false otherwise.- Type Parameters:
T- The type of the objects being compared- Parameters:
left- The first objectright- The second object- Returns:
- true if left < right, false otherwise
-
lessThanEquals
public static <T> boolean lessThanEquals(T left, T right) The (lessThanEquals) function checks if the first value is less than or equal to the second. Returns true if left ≤ right, false otherwise.- Type Parameters:
T- The type of the objects being compared- Parameters:
left- The first objectright- The second object- Returns:
- true if left ≤ right, false otherwise
-
greaterThan
public static <T> boolean greaterThan(T left, T right) The (greaterThan) function checks if the first value is greater than the second. Returns true if left > right, false otherwise.- Type Parameters:
T- The type of the objects being compared- Parameters:
left- The first objectright- The second object- Returns:
- true if left > right, false otherwise
-
greaterThanEquals
public static <T> boolean greaterThanEquals(T left, T right) The (greaterThanEquals) function checks if the first value is greater than or equal to the second. Returns true if left ≥ right, false otherwise.- Type Parameters:
T- The type of the objects being compared- Parameters:
left- The first objectright- The second object- Returns:
- true if left ≥ right, false otherwise
-
and
The (and) function performs a bitwise AND operation on the given numbers. Returns the result of the bitwise AND between the two numbers.- Type Parameters:
T- The type of the objects being operated on- Parameters:
left- The first numberright- The second number- Returns:
- The result of bitwise AND operation
-
or
The (or) function performs a bitwise OR operation on the given numbers. Returns the result of the bitwise OR between the two numbers.- Type Parameters:
T- The type of the objects being operated on- Parameters:
left- The first numberright- The second number- Returns:
- The result of bitwise OR operation
-
xor
The (xor) function performs a bitwise XOR operation on the given numbers. Returns the result of the bitwise exclusive OR between the two numbers.- Type Parameters:
T- The type of the objects being operated on- Parameters:
left- The first numberright- The second number- Returns:
- The result of bitwise XOR operation
-
elementWiseAdd
Performs element-wise addition of two values.Applies addition to each element individually for numbers, strings, simple values, arrays, and compatible collections.
- Type Parameters:
T- the type of operands- Parameters:
left- the first valueright- the second value- Returns:
- the element-wise sum of left and right
-
elementWiseSubtract
Performs element-wise subtraction of two values.- Type Parameters:
T- the type of operands- Parameters:
left- the first valueright- the second value- Returns:
- the element-wise difference of left and right
-
elementWiseMultiply
Performs element-wise multiplication of two values.- Type Parameters:
T- the type of operands- Parameters:
left- the first valueright- the second value- Returns:
- the element-wise product of left and right
-
elementWiseDivide
Performs element-wise division of two values.- Type Parameters:
T- the type of operands- Parameters:
left- the first valueright- the second value- Returns:
- the element-wise quotient of left and right
-
elementWiseModulo
Performs element-wise modulo of two values.- Type Parameters:
T- the type of operands- Parameters:
left- the first valueright- the second value- Returns:
- the element-wise remainder of left divided by right
-
not
The (not) function performs a bitwise NOT operation on the given number. Returns the result of flipping all bits in the number.- Type Parameters:
T- The type of the object being operated on- Parameters:
x- The number to negate- Returns:
- The bitwise NOT of the number
-
shiftLeft
The (shiftLeft) function performs a bitwise left shift on the given number by a specified number of positions. Used in binary calculations to increase the value.- Type Parameters:
T- The type of the number- Parameters:
x- The number to shiftpositions- The number of positions to shift left- Returns:
- The shifted number
-
shiftRight
The (shiftRight) function performs a bitwise right shift on the given number by a specified number of positions. Used in binary calculations to decrease the value.- Type Parameters:
T- The type of the number- Parameters:
x- The number to shiftpositions- The number of positions to shift right- Returns:
- The shifted number
-
unsignedShiftRight
The (unsignedShiftRight) function performs an unsigned bitwise right shift on the given number by a specified number of positions. The sign bit is ignored. Used to handle numbers without sign extension.- Type Parameters:
T- The type of the number- Parameters:
x- The number to shiftpositions- The number of positions to shift right- Returns:
- The shifted number
-
preIncrement
The (preIncrement) function increases the given number by one before using it in the expression.- Type Parameters:
T- The type of the object to increment- Parameters:
x- The object to increment- Returns:
- The incremented value before use
-
postIncrement
The (postIncrement) function increases the given number by one after using it in the expression.- Type Parameters:
T- The type of the object to increment- Parameters:
x- The object to increment- Returns:
- The original value before incrementing
-
preDecrement
The (preDecrement) function decreases the given number by one before using it in the expression.- Type Parameters:
T- The type of the object to decrement- Parameters:
x- The object to decrement- Returns:
- The decremented value before use
-
postDecrement
The (postDecrement) function decreases the given number by one after using it in the expression.- Type Parameters:
T- The type of the object to decrement- Parameters:
x- The object to decrement- Returns:
- The original value before decrementing
-
negate
The (negate) function returns the arithmetic negation of the given value.It inverts the numeric sign of the value. For example:
negate(5) → -5 negate(-2) → 2- Type Parameters:
T- The type of the object to negate- Parameters:
x- The object whose sign should be inverted- Returns:
- The negated value
-
logicalNot
The (logicalNot) function returns the logical negation of the given value.It inverts a boolean value. For example:
logicalNot(true) → false logicalNot(false) → true- Type Parameters:
T- The type of the object to logically negate- Parameters:
x- The object to negate- Returns:
- The logical negation of the given value
-
parseDynamicNumber
Parses a textual number according to a specified dynamic numeric system (radix) and returns the most appropriate numeric type.This method handles numeric strings containing Arabic digits and converts them to Ascii digits before parsing. The returned
Numbertype depends on the input value and the specified radix.Example usage:
DynamicNumber radix = DynamicNumber.DECIMAL; Number n = parseDynamicNumber("١٢٣", radix); // returns 123 as Integer- Parameters:
text- the textual representation of the number; may contain Arabic digitsradix- the dynamic numeric system used for parsing (e.g., decimal, hexadecimal)- Returns:
- the parsed
Number, type determined dynamically (Integer, Long, Double, etc.) - Throws:
NullPointerException- if text or radix is nullNumberFormatException- if the text cannot be parsed as a valid number in the given radix
-
size
Computes the size of an object in a generic manner.The meaning of "size" depends on the object type:
- Array: length of the array
- Collection (List, Set): number of elements
- Map: number of entries
- String: length of the string
- Number or Boolean: 1
- Other Objects: number of non-static fields
Example usage:
List<String> list = List.of("a", "b", "c"); Number size = size(list); // returns 3 int[] arr = {1, 2, 3, 4}; Number arrSize = size(arr); // returns 4- Parameters:
obj- the object whose size is to be determined; may be null- Returns:
- the size as a
Number; 0 for null objects
-
getType
Returns a string representing the type of the provided object according to the Naftah type system.This method internally uses
JavaTypeto capture both raw and generic type information, providing a human-readable description of the type. Useful for dynamic type inspection and error reporting.Example usage:
String typeStr = getType(List.of("a", "b")); // "java.util.List<java.lang.String>"- Parameters:
obj- the object to inspect; may be null- Returns:
- a string describing the object's type; "null" if the object is null
-
instanceOf
Checks whether the given object is an instance of the specifiedJavaType.This method performs type checking similar to the Java
instanceofoperator, but also compares generic type parameters if available. It ensures that the object's raw type is assignable from the target type and that generic parameters match exactly.Example usage:
JavaType pairType = JavaType.of(new TypeReference<Pair<String, Integer>>() {}); Pair<String, Integer> pair = new Pair<>("hello", 42); boolean result = instanceOf(pair, pairType); // true- Parameters:
obj- the object to check; may be nulljavaType- the targetJavaTypeto check against; may be null- Returns:
trueif obj is an instance of javaType (including generic parameters),falseotherwise
-