Force-Framework

Mark Brennand

ArrayV1

global with sharing class ArrayV1
Description

Class providing the Array API.

See README for full details of the Array API.

Copyright (c) 2025 Mark Brennand, released under MIT License.

AuthorMark Brennand

Methods

  over

global static ArrayV1.Methods over(final List<Object> objects)
Description

Creates 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.

  over

global static ArrayV1.Methods over(final Object source)
Description

Creates the iterator from an Object.

If the Object is Iterable a List is built from it, otherwise a List is used with a single element, the object.

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.

Parametersource: The source object.
ReturnsThe methods that may be called on the array.

ArrayV1.APIException

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

ArrayV1.Callback

global with sharing abstract class Callback
Description

Class 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 OptionalV1 function(final Object element, final OptionalV1 currentValue, final Integer index)
Description

This 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 OptionalV1 shared()
DescriptionReturns the shared data for the callback.
ReturnsThe shared data.

  sharing

global void sharing(final Object shared)
DescriptionRecords the data to be shared in the callback.
Parametershared: The data to be shared.

ArrayV1.Methods

global interface Methods extends Iterable<Object>
Description

Iterator methods.

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

Methods

  at

OptionalV1 at(final Integer index)
Description

Returns 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(final List<Object> elements)
DescriptionAppends the given array to the list of objects.
Parameterelements: The array to be added to the original.
ReturnsThe new array.

  every

Boolean every(final ArrayV1.Callback callback)
Description

Returns true if all the elements of the array have a value that is matched by the callback function.

The callback function must return true if the array element is a match.

Parametercallback: The callback class to be invoked for each array element.
ReturnsTrue if all the elements match.

  filter

List<Object> filter(final ArrayV1.Callback callback)
Description

Builds 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 ArrayV1.Callback callback)
Description

Calls 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.

  join

String join(final String separator)
DescriptionReturns a string comprised of the array elements joined by the separator.
Parameterseparator: Separator for array elements in output string.
ReturnsThe joined array elements.

  length

Integer length()
DescriptionReturns the number of elements in the array.
ReturnsThe number of elements in the array.

  pop

DescriptionRemoves the last element from the array and returns it.
ReturnsThe last element of the array.

  push

void push(final Object obj)
DescriptionAdds the given object to the end of the array.
Parameterobj: The object to add to the array.

  reduce

Object reduce(final ArrayV1.Callback callback)
Description

Calls the callback for each element in the array being iterated over.

In the first call to the callback, the currentValue will be the first element of the array and the element argument will be the second element of the array. In subsequent calls, the currentValue will be the value returned by the last callback.

Parametercallback: 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(final ArrayV1.Callback callback, final Object initialValue)
Description

Builds 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.

  reduceRight

Object reduceRight(final ArrayV1.Callback callback)
Description

Calls the callback for each element in the array iterated over in reverse order.

In the first call to the callback, the currentValue will be the last element of the array and the element argument will be the second to last element of the array. In subsequent calls, the currentValue will be the value returned by the last callback.

Parametercallback: The callback class to be invoked for each array element.
ReturnsThe return value from the last element of the array iterated over.

  reduceRight

Object reduceRight(final ArrayV1.Callback callback, final Object initialValue)
Description

Builds an object based on the processing of each element of the array iterated over in reverse order.

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.

  reverse

List<Object> reverse()
DescriptionReturns a new array with the elements of the original array in reversed order.
ReturnsThe new array with the reversed elements.

  sharing

ArrayV1.Methods sharing(final 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().

  shift

OptionalV1 shift()
DescriptionRemoves the first element from the array and returns it.
ReturnsThe first element of the array.

  some

Boolean some(final ArrayV1.Callback callback)
Description

Returns true if the array contains a value that is matched by the callback function.

The callback function must return true if the array element is a match.

Parametercallback: The callback class to be invoked for each array element.
ReturnsTrue if a matching value is found.

  sort

List<Object> sort()
DescriptionSorts an array of Comparable objects.
ReturnsA new array containing the sorted objects.

  sort

List<Object> sort(final Comparator comparator)
DescriptionSorts an array using the given Comparable
Parametercomparator: The Comparator to use to compare the objects.
ReturnsA new array containing the sorted objects.

  transform

List<Object> transform(final ArrayV1.Callback callback)
Description

Builds 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.

The list returned will be of type ANY and as a result may not be cast to the type the list contains.

Parametercallback: The callback class to be invoked for each array element.
ReturnsThe list of transformed objects.

  transform

List<Object> transform(final ArrayV1.Callback callback, final List<Object> returnList)
Description

Builds 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.

The return list must be of the type of the transformed elements. This allows the returned list to be cast to the type of the element.

Parametercallback: The callback class to be invoked for each array element.
ParameterreturnList: The list of the type the transformed elements.
ReturnsThe list of transformed objects.