Array Accessors
Zero garbage array accessors enable iteration over message array fields without allocating iterator objects. They use the XIterator pattern to provide zero-garbage traversal of arrays.
Overview
When a message or state object contains an array field, traditional Java iteration using for-each or Iterator creates temporary objects that contribute to garbage. Rumi's zero garbage array accessors provide a way to iterate over arrays without any allocations.
XIterator Pattern
The XIterator pattern provides zero-garbage iteration through reusable iterator objects. When ADM generates code for array fields, it produces iterateXXX() methods that return an XIterator.
Generated Methods
For an array field declared as:
<message name="Order">
<OrderLine name="lines" array="true"/>
</message>The ADM code generator produces:
getLines()
Returns a List view of the array (allocates)
getLinesAt(int index)
Returns the element at the specified index
getLinesCount()
Returns the number of elements
getLinesIterator()
Returns an XIterator for zero-garbage iteration
addLines(OrderLine)
Adds an element to the end of the array
lendLines(OrderLine)
'Lends' (adds) an element the caller continues to own
setLinesAt(int index, OrderLine)
Replaces, in place, the element at the index
lendLinesAt(int index, OrderLine)
Replaces, in place, the element at the index, 'lending' the value
clearLinesAt(int index)
Removes and disposes the single element at the index
clearLines()
Removes all elements
Modifying Array Elements by Index
For array fields whose element type is an embedded entity, the elements are pooled, lifecycle-managed objects. The by-index mutators (setXXXAt, lendXXXAt, clearXXXAt) let you change a single element without rebuilding the whole array, disposing exactly the element they replace or remove and leaving every other element untouched:
setXXXAt and lendXXXAt require a non-null value; use clearXXXAt(index) to remove an element. All three throw IndexOutOfBoundsException (or IllegalArgumentException) for an out-of-range index.
Zero Garbage Iteration
Use the XIterator for zero-garbage array traversal:
The XIterator is reused across calls and does not allocate.
Use Cases
Filtering:
Aggregation:
Modifying Elements:
Comparison with Standard Java Iteration
Standard (allocates):
Zero Garbage:
The zero-garbage approach reuses the same XIterator instance on each call to iterateXXX(), eliminating allocations.
See Also
Coding for Zero Garbage - Overview of zero garbage techniques
Embedded Entities - Lifecycle-managed objects for complex messages
Application Object Pooling - Creating custom pooled objects
Last updated

