public Stream stream()

in src/main/java/com/awslabs/resultsiterator/implementations/ResultsIteratorAbstract.java [69:125]


    public Stream<T> stream() {
        Iterator<T> iterator = new Iterator<T>() {
            List<T> output = List.empty();
            boolean started = false;
            String nextToken = null;
            AwsRequest request;

            private void performRequest() {
                if (!started) {
                    // First time around configure the request
                    request = configureRequest();

                    // The setup is complete, don't do it again
                    started = true;
                }

                awsResponse = queryNextResults(request);

                output = output.appendAll(getResultData());

                nextToken = getNextToken();

                if (nextToken == null) {
                    return;
                }

                request = setNextToken(request, nextToken);
            }

            @Override
            public boolean hasNext() {
                if (!started) {
                    // We haven't started, attempt a request
                    performRequest();
                }

                while ((output.size() == 0) && (nextToken != null)) {
                    // Output array is empty but the next token is not null, attempt a request
                    performRequest();
                }

                // Next token is NULL, return whether or not the output array is empty
                return output.size() != 0;
            }

            @Override
            public T next() {
                T nextValue = output.get();
                output = output.removeAt(0);

                return nextValue;
            }
        };

        // This stream does not have a known size, does not contain NULL elements, and can not be run in parallel
        return Stream.ofAll(iterator);
    }