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

com.files.ListIteratorIterable Maven / Gradle / Ivy

package com.files;

import java.io.IOException;
import java.util.Iterator;

public class ListIteratorIterable implements Iterable {
  private final ListIterator listIterator;

  public ListIteratorIterable(ListIterator listIterator) {
    this.listIterator = listIterator;
  }

  @Override
  public Iterator iterator() {
    return new Iterator() {
      private int index = 0;
      private int page = 0;

      @Override
      public boolean hasNext() {
        return page == 0 || listIterator.hasNextPage() || this.index < listIterator.data.size() - 1;
      }

      @Override
      public T next() {
        this.index++;
        if (this.index >= listIterator.data.size()) {
          if (listIterator.hasNextPage() || this.page == 0) {
            try {
              System.out.println("Fetching next page");
              listIterator.loadNextPage();
            } catch (IOException e) {
              return null;
            }
            this.index = 0;
            this.page++;
          } else {
            return null;
          }
        }

        return listIterator.data.get(index);
      }

      @Override
      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy