int count = Math.min(childrenCount, helper.maxCollectionSize(expression));
// Iterate through each child of the collection
for (int index = 0; index < count; index++) {
Expression child = collectionExpression.getChild(index);
int childLength = 0;
// At the beginning of the child
if (position == length) {
helper.addTheBeginningOfChild(expression, collectionExpression, index, hasComma);
break;
}
// Each expression within the collection has to be separated by a comma, the previous
// expression and the expression at the current index are not separated by a comma
// Example: "SELECT e FROM Employee e GROUP" <- [ "Employee e", "GROUP" ]
else if ((index > 0) && !hasComma) {
length += child.getLength();
// At the end of the child
if (position == length) {
helper.addAtTheEndOfChild(expression, collectionExpression, index, hasComma, false);
break;
}
// To be valid, each child has to be separated by a comma,
// ask the helper if it should continue with the next child
if (!helper.canContinue(expression, collectionExpression, index)) {
break;
}
// Special case when reaching the end of the collection
else if (index + 1 == count) {
helper.addAtTheEndOfChild(expression, collectionExpression, index, hasComma, false);
}
}
else {
childLength = child.getLength();
// At the end of the child
if ((position == length + childLength) ||
(virtualSpace && (position == length + childLength + SPACE_LENGTH))) {