Force-Framework

Mark Brennand

ArrayV1

global with sharing class ArrayV1
DescriptionCopyright (c) 2025 Mark Brennand, released under MIT License.

Class providing the Array API.

See README for full details of the Array API.
AuthorMark Brennand

Methods

  over

global static Methods over(final List<Object> objects)
DescriptionCreates the iterator for an array of objects.

The method to iterate the array with may then be called.

If data is to be shared between callbacks, call the shared() method before the iterator method.
Parameterobjects: The array of objects to be iterated.
ReturnsThe methods that may be called on the array.

ArrayV1.APIException

global with sharing class APIException extends Exception
DescriptionException thrown when an operation in the Array API fails.

ArrayV1.Callback

global with sharing abstract class Callback
DescriptionClass representing a callback to process each iterated array element.

The function() method must be overridden to code the logic to be performed on the array element.

Data shared on initiation of the iterator is accessed using the shared() method.

Methods

  function

global abstract Object function(Object element, Object currentValue, Integer index)
DescriptionThis class must be extended to code the logic to be performed on each array element.

The method will be called for each element. The currentValue will be null, except when the callback is being called from the reduce() method. In this case, its value will be the accumulated result of all the previous callbacks.

See the iterator methods for details of the return values expected from the callback.
Parameterelement: The current array element.
ParametercurrentValue: The value accumulated over the array iteration.
Parameterindex: The positional index of the array element.
ReturnsA value representing the result of the processing.

  shared

global Object shared()
DescriptionReturns the shared data for the callback.
ReturnsThe shared data.

ArrayV1.Methods

global interface Methods
DescriptionIterator methods.

The over() method must be called first to use these methods.

Methods

  at

Object at(Integer index)
DescriptionReturns the element at the given index.

If index is negative, the element relative to the end of the array is returned. Use -1 for the last element.
Parameterindex: The positional index of the element to return.
ReturnsThe new array.

  concat

List<Object> concat(List<Object> elements)
DescriptionAppends the given array to the list of objects.
Parameterelements: The array to be added to the original.
ReturnsThe new array.

  filter

List<Object> filter(Callback callback)
DescriptionBuilds a list of results matching the filter condition.

The callback function must return true if the array element is to be added to the return list or false if not.
Parametercallback: The callback class to be invoked for each array element.
ReturnsList of filtered results.

  forEach

void forEach(final Callback callback)
DescriptionCalls the callback for each element in the array being iterated over.

The currentValue argument to the callback function will be null.

The return value from the callback function is ignored.
Parametercallback: The callback class to be invoked for each array element.

  reduce

Object reduce(Callback callback)
DescriptionCalls the callback for each element in the array being iterated over.

The currentValue argument of the callback function will be first element of the array when first called for the iteration. In subsequent calls, the currentValue will be the last value returned by the callback's function() method.
Parametercallback: @param callback The callback class to be invoked for each array element.
ReturnsThe return value from the last element of the array iterated over.

  reduce

Object reduce(Callback callback, Object initialValue)
DescriptionBuilds an object based on the processing of each element of the array.

The currentValue argument of the callback function will be the value of the initialValue argument for the iteration. In subsequent calls, the currentValue will be the last value returned by the callback's function() method.
Parametercallback: The callback class to be invoked for each array element.
ParameterinitialValue: The starting value for the iteration.
ReturnsThe return value from the last element of the array iterated over.

  sharing

Methods sharing(Object shared)
DescriptionSets the data to be shared between callbacks.
Parametershared: The data to be shared between callbacks.
ReturnsThe Methods implementation, allowing the call to be chained with over().

  transform

List<Object> transform(Callback callback)
DescriptionBuilds a list of objects with a transformation applied.

This is the equivalent of the Javascript map() function.

The callback function must apply the transformation and return the transformed object.
Parametercallback: The callback class to be invoked for each array element.
ReturnsThe list of transformed objects.