Iterator iReferences = references.iterator(); Iterator iCandidates = candidates.iterator();
NodeSetIterator iReferences = references.iterator(); NodeSetIterator iCandidates = candidates.iterator();
    public static NodeSet selectFollowingSiblings(NodeSet candidates, NodeSet references, int contextId) {        if (references.getLength() == 0 || candidates.getLength() == 0)            return NodeSet.EMPTY_SET;        NodeSet result = new ExtArrayNodeSet();        Iterator iReferences = references.iterator();        Iterator iCandidates = candidates.iterator();        NodeProxy reference = (NodeProxy) iReferences.next();        NodeProxy candidate = (NodeProxy) iCandidates.next();           //TODO : review : don't care about preceding siblings        while (true) {            // first, try to find nodes belonging to the same doc            if (reference.getDocument().getDocId() < candidate.getDocument().getDocId()) {                if (iReferences.hasNext())                    reference = (NodeProxy) iReferences.next();                else                    break;            } else if (reference.getDocument().getDocId() > candidate.getDocument().getDocId()) {                if (iCandidates.hasNext())                    candidate = (NodeProxy) iCandidates.next();                else                    break;            } else {                // same document: check if the nodes have the same parent                long pa = getParentId(reference.getDocument(), reference.getGID());                long pb = getParentId(candidate.getDocument(), candidate.getGID());                if (pa < pb) {                    // wrong parent: proceed                    if (iReferences.hasNext())                        reference = (NodeProxy) iReferences.next();                    else                        break;                } else if (pa > pb) {                    // wrong parent: proceed                    if (iCandidates.hasNext())                        candidate = (NodeProxy) iCandidates.next();                    else                        break;                } else {                    // found two nodes with the same parent                    // now, compare the ids: a node is a following sibling                    // if its id is greater than the id of the other node                    if (candidate.getGID() < reference.getGID()) {                        // found a preceding sibling                        if (iCandidates.hasNext())                            candidate = (NodeProxy) iCandidates.next();                        else                            break;                    } else if (candidate.getGID() > reference.getGID()) {                        // found a following sibling                         if (Expression.NO_CONTEXT_ID != contextId)                            //TODO : add transverse context                            candidate.addContextNode(contextId, reference);                        else                            candidate.copyContext(reference);                        result.add(candidate);                                                if (iCandidates.hasNext())                            candidate = (NodeProxy) iCandidates.next();                        else                            break;                        // equal nodes: proceed with next node                    } else {                        if (iCandidates.hasNext())                            candidate = (NodeProxy) iCandidates.next();                        else                            break;                    }                }            }        }        return result;    }    