Class CollectionBuiltinFunctions
java.lang.Object
org.daiitech.naftah.builtin.functions.CollectionBuiltinFunctions
Provides built-in functions used within the Naftah language for performing various
arithmetic and logical operations with precision and efficiency.
This class contains static methods that implement fundamental operations such as addition, subtraction, multiplication, division, logical comparisons, bitwise operations, and element-wise collection operations. All functions are annotated to be recognized by the Naftah interpreter.
Note: This class cannot be instantiated.
- Author:
- Chakib Daii
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Objectadd(T left, T right) Adds two values element by element of collection, map with themselves or with simple value.static booleanaddElement(Object x, Object element) Adds an element to a collection or a map.static <T> Objectand(T left, T right) Performs bitwise AND operation element by element of collection, map with themselves or with simple value.private static <T> voidcheckParam(T x) Checks if the parameter is a tuple, collection, map, or array.private static <T> voidcheckParams(T left, T right) Checks if at least one of the two parameters is a tuple, collection, map, or array.static voidClears all elements from a collection, map, or array.static <T> booleancontainsElement(T x, Object element) Checks if a tuple, collection, map, or array contains a specific element.static <K,V> boolean containsKey(Map<K, V> map, K key) Checks if a map contains a specific key.static <T> Objectdivide(T left, T right) Divides the first value by the second value element by element of collection, map with themselves or with simple value.static <T> ObjectelementWiseAdd(T left, T right) Performs element-wise addition element by element of collection, map with themselves or with simple value.static <T> ObjectelementWiseDivide(T left, T right) Performs element-wise division element by element of collection, map with themselves or with simple value.static <T> ObjectelementWiseModulo(T left, T right) Performs element-wise modulo element by element of collection, map with themselves or with simple value.static <T> ObjectelementWiseMultiply(T left, T right) Performs element-wise multiplication element by element of collection, map with themselves or with simple value.static <T> ObjectelementWiseSubtract(T left, T right) Performs element-wise subtraction element by element of collection, map with themselves or with simple value.static <T> Objectequals(T left, T right) Checks if two values are equal element by element of collection, map with themselves or with simple value.getElementAt(Collection<T> collection, I targetIndex) Gets an element from a collection by index.static <T> ObjectgreaterThan(T left, T right) Checks if the first value is greater than the second element by element of collection, map with themselves or with simple value.static <T> ObjectgreaterThanEquals(T left, T right) Checks if the first value is greater than or equal to the second element by element of collection, map with themselves or with simple value.static <T> ObjectlessThan(T left, T right) Checks if the first value is less than the second element by element of collection, map with themselves or with simple value.static <T> ObjectlessThanEquals(T left, T right) Checks if the first value is less than or equal to the second element by element of collection, map with themselves or with simple value.static <T> ObjectlogicalAnd(T left, T right) Performs logical AND element by element of collection, map with themselves or with simple value.static <T> ObjectlogicalNot(T x) Performs logical NOT on a collection or map.static <T> ObjectlogicalOr(T left, T right) Performs logical OR element by element of collection, map with themselves or with simple value.static <T> Objectmodulo(T left, T right) Computes the modulo (remainder) of the first value divided by the second element by element of collection, map with themselves or with simple value.static <T> Objectmultiply(T left, T right) Multiplies two values element by element of collection, map with themselves or with simple value.static <T> Objectnegate(T x) Negates a collection or map.static NaftahBugErrornewNaftahNotCollectionOrMapArgumentError(boolean singleArgument) Creates aNaftahBugErrorindicating that one or more arguments are not valid collection, array, or map types.static <T> Objectnot(T x) Performs bitwise NOT element by element of collection, map with themselves or with simple value.static <T> ObjectnotEquals(T left, T right) Checks if two values are not equal element by element of collection, map with themselves or with simple value.static <T> Objector(T left, T right) Performs bitwise OR operation element by element of collection, map with themselves or with simple value.static <T> ObjectpostDecrement(T x) Performs post-decrement element by element of collection, map with themselves or with simple value.static <T> ObjectpostIncrement(T x) Performs post-increment element by element of collection, map with themselves or with simple value.static <T> Objectpow(T left, T right) Raises the first value to the power of the second value element by element of collection, map with themselves or with simple value.static <T> ObjectpreDecrement(T x) Performs pre-decrement element by element of collection, map with themselves or with simple value.static <T> ObjectpreIncrement(T x) Performs pre-increment element by element of collection, map with themselves or with simple value.static booleanremoveElement(Object x, Object element) Removes an element from a collection or a map.removeElementAt(Collection<T> collection, I targetIndex) Removes an element from a collection at a given index.static <T> booleanretainElements(Collection<T> collection, Collection<T> otherCollection) Retains only the elements in a collection that are contained in another collection.static <I extends Number,T>
voidsetElementAt(Collection<T> collection, I targetIndex, T newValue) Sets an element in a collection at a given index.static <T> Objectsubtract(T left, T right) Subtracts the second value from the first element by element of collection, map with themselves or with simple value.static <T> Objectxor(T left, T right) Performs bitwise XOR operation element by element of collection, map with themselves or with simple value.
-
Constructor Details
-
CollectionBuiltinFunctions
private CollectionBuiltinFunctions()Private constructor to prevent instantiation. ThrowsNaftahBugErrorif called.
-
-
Method Details
-
add
Adds two values element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first operandright- second operand- Returns:
- collection element-wise sum
-
logicalAnd
Performs logical AND element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first operandright- second operand- Returns:
- collection logical AND results
-
logicalOr
Performs logical OR element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first operandright- second operand- Returns:
- collection of logical OR results
-
subtract
Subtracts the second value from the first element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first operandright- second operand- Returns:
- subtraction result
-
multiply
Multiplies two values element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first operandright- second operand- Returns:
- multiplication result
-
pow
Raises the first value to the power of the second value element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- baseright- exponent- Returns:
- result of exponentiation
-
divide
Divides the first value by the second value element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- numeratorright- denominator- Returns:
- division result
-
modulo
Computes the modulo (remainder) of the first value divided by the second element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- dividendright- divisor- Returns:
- remainder after division
-
equals
Checks if two values are equal element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first valueright- second value- Returns:
- collection of booleans
-
notEquals
Checks if two values are not equal element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first valueright- second value- Returns:
- collection of booleans
-
lessThan
Checks if the first value is less than the second element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first valueright- second value- Returns:
- collection of booleans
-
lessThanEquals
Checks if the first value is less than or equal to the second element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first valueright- second value- Returns:
- collection of booleans
-
greaterThan
Checks if the first value is greater than the second element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first valueright- second value- Returns:
- collection of booleans
-
greaterThanEquals
Checks if the first value is greater than or equal to the second element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first valueright- second value- Returns:
- collection of booleans
-
and
Performs bitwise AND operation element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first valueright- second value- Returns:
- result of bitwise AND
-
or
Performs bitwise OR operation element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first valueright- second value- Returns:
- result of bitwise OR
-
xor
Performs bitwise XOR operation element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first valueright- second value- Returns:
- result of bitwise XOR
-
elementWiseAdd
Performs element-wise addition element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first collection or valueright- second collection or value- Returns:
- element-wise addition result
-
elementWiseSubtract
Performs element-wise subtraction element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first collection or valueright- second collection or value- Returns:
- element-wise subtraction result
-
elementWiseMultiply
Performs element-wise multiplication element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first collection or valueright- second collection or value- Returns:
- element-wise multiplication result
-
elementWiseDivide
Performs element-wise division element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first collection or valueright- second collection or value- Returns:
- element-wise division result
-
elementWiseModulo
Performs element-wise modulo element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operands- Parameters:
left- first collection or valueright- second collection or value- Returns:
- element-wise modulo result
-
not
Performs bitwise NOT element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operand- Parameters:
x- collection or value- Returns:
- result after applying bitwise NOT
-
preIncrement
Performs pre-increment element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operand- Parameters:
x- collection or value- Returns:
- result after pre-increment
-
postIncrement
Performs post-increment element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operand- Parameters:
x- collection or value- Returns:
- result after post-increment
-
preDecrement
Performs pre-decrement element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operand- Parameters:
x- collection or value- Returns:
- result after pre-decrement
-
postDecrement
Performs post-decrement element by element of collection, map with themselves or with simple value.- Type Parameters:
T- type of operand- Parameters:
x- collection or value- Returns:
- result after post-decrement
-
negate
Negates a collection or map.- Type Parameters:
T- type of operand- Parameters:
x- collection or value- Returns:
- result after negation
-
logicalNot
Performs logical NOT on a collection or map.- Type Parameters:
T- type of operand- Parameters:
x- collection or value- Returns:
- result after logical NOT
-
getElementAt
Gets an element from a collection by index.- Parameters:
collection- the collection to get fromtargetIndex- index of the element- Returns:
- the element at the given index
-
setElementAt
public static <I extends Number,T> void setElementAt(Collection<T> collection, I targetIndex, T newValue) Sets an element in a collection at a given index.- Type Parameters:
T- type of elementI- type of index- Parameters:
collection- the collection to modifytargetIndex- index to setnewValue- new value to assign
-
removeElementAt
Removes an element from a collection at a given index.- Type Parameters:
T- type of elementI- type of index- Parameters:
collection- the collection to modifytargetIndex- index of the element to remove
-
containsElement
Checks if a tuple, collection, map, or array contains a specific element.- Type Parameters:
T- type of collection/map/array- Parameters:
x- collection, map, or array to checkelement- element to look for- Returns:
- true if the element is found, false otherwise
-
containsKey
Checks if a map contains a specific key.- Type Parameters:
K- type of the keyV- type of the value- Parameters:
map- the map to checkkey- the key to look for- Returns:
- true if the map contains the key, false otherwise
-
addElement
Adds an element to a collection or a map.- Parameters:
x- collection or map to modifyelement- element to add- Returns:
- true if the collection/map was modified
-
removeElement
Removes an element from a collection or a map.- Parameters:
x- collection or map to modifyelement- element to remove- Returns:
- true if the collection/map was modified
-
retainElements
Retains only the elements in a collection that are contained in another collection.- Parameters:
collection- the collection to modifyotherCollection- collection with elements to retain- Returns:
- true if the collection was modified
-
clearCollection
Clears all elements from a collection, map, or array.- Parameters:
x- the collection, map, or array to clear
-
checkParams
private static <T> void checkParams(T left, T right) Checks if at least one of the two parameters is a tuple, collection, map, or array. Throws a NaftahBugError with an Arabic message if the check fails.- Type Parameters:
T- type of operands- Parameters:
left- first value to checkright- second value to check- Throws:
NaftahBugError- if neither left nor right is a collection, map, or array
-
checkParam
private static <T> void checkParam(T x) Checks if the parameter is a tuple, collection, map, or array. Throws a NaftahBugError with an Arabic message if the check fails.- Type Parameters:
T- type of operand- Parameters:
x- value to check- Throws:
NaftahBugError- if x is not a collection, map, or array
-
newNaftahNotCollectionOrMapArgumentError
Creates aNaftahBugErrorindicating that one or more arguments are not valid collection, array, or map types.If
singleArgumentistrue, the error message specifies that a single argument must be a collection, array, or associative array (map). Otherwise, the message specifies that at least one of the two arguments must be of those types.Arabic message:
- For a single argument:
"يجب أن يكون المعامل مصفوفة أو مجموعة أو مصفوفة ترابطية." - For multiple arguments:
"يجب أن يكون على الأقل أحد المعاملين مصفوفة أو مجموعة أو مصفوفة ترابطية ."
- Parameters:
singleArgument-trueif the error refers to a single argument;falseif it refers to multiple arguments- Returns:
- a
NaftahBugErrorwith the appropriate descriptive message
- For a single argument:
-