private void initialize()

in src/main/java/org/apache/sling/graphql/helpers/GenericConnection.java [72:123]


    private void initialize() {
        if(initialized) {
            throw new IllegalStateException("Already initialized");
        }
        initialized = true;

        // Need to visit the stream first to setup the PageInfo, which graphql-java
        // apparently uses before visiting all the edges
        boolean inRange = false;
        int itemsToAdd = limit;
        while(itemsToAdd > 0 && dataIterator.hasNext()) {
            final T node = dataIterator.next();
            boolean addThisNode = false;
            if(!inRange) {
                if(startAfter == null) {
                    inRange = true;
                    addThisNode = true;
                    if(hasPreviousPage == null) {
                        hasPreviousPage = false;
                    }
                } else {
                    final String rawCursor = cursorStringProvider.apply(node);
                    inRange = startAfter.getRawValue().equals(rawCursor);
                    if(hasPreviousPage == null) {
                        hasPreviousPage = true;
                    }
                }
            } else {
                addThisNode = true;
            }

            if(addThisNode) {
                final Edge<T> toAdd = newEdge(node, cursorStringProvider);
                if(startCursor == null) {
                    startCursor = toAdd.getCursor();
                }
                endCursor = toAdd.getCursor();
                edges.add(toAdd);
                itemsToAdd--;
            }
        }

        if(!inRange && limit > 0 && startAfter != null) {
            throw new SlingGraphQLException("Start cursor not found in supplied data:" + startAfter);
        }
        if(hasPreviousPage == null) {
            hasPreviousPage = false;
        }
        if(hasNextPage == null) {
            hasNextPage = dataIterator.hasNext();
        }
    }