All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.algolia.utils.AlgoliaIterableHelper Maven / Gradle / Ivy

The newest version!
package com.algolia.utils;

import java.util.Iterator;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;

public class AlgoliaIterableHelper {

  private AlgoliaIterableHelper() {
    // Empty.
  }

  public static  Iterable createIterable(Supplier> executeQuery, BooleanSupplier hasNext) {
    return () ->
      new Iterator() {
        private boolean isFirstRequest = true;
        private Iterator currentIterator = null;

        @Override
        public boolean hasNext() {
          if (isFirstRequest || (hasNext.getAsBoolean() && !currentIterator.hasNext())) {
            currentIterator = executeQuery.get();
            isFirstRequest = false;
          }
          return currentIterator != null && currentIterator.hasNext();
        }

        @Override
        public T next() {
          if (currentIterator == null || !currentIterator.hasNext()) {
            currentIterator = executeQuery.get();
            isFirstRequest = false;
          }
          return currentIterator.next();
        }
      };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy