

package com.simiacryptus.util.io;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Andrew Charneski on 3/11/2017.
 *
 * @param <T> the type parameter
 */
public class AsyncListIterator<T> implements Iterator<T> {
  private final List<T> queue;
  private final Thread thread;
  /**
   * The Index.
   */
  int index = -1;
  
  /**
   * Instantiates a new Async list iterator.
   *
   * @param queue  the queue
   * @param thread the thread
   */
  public AsyncListIterator(final List<T> queue, final Thread thread) {
    this.thread = thread;
    this.queue = queue;
  }
  
  @Override
  protected void finalize() throws Throwable {
    super.finalize();
  }
  
  @Override
  public boolean hasNext() {
    return index < queue.size() || thread.isAlive();
  }
  
  @Nullable
  @Override
  public T next() {
    try {
      while (hasNext()) {
        if (++index < queue.size()) {
          return queue.get(index);
        }
        else {
          Thread.sleep(100);
        }
      }
      return null;
    } catch (@javax.annotation.Nonnull final InterruptedException e) {
      throw new RuntimeException(e);
    }
  }
}
