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

io.shiftleft.overflowdb.util.MultiIterator2 Maven / Gradle / Ivy

There is a newer version: 1.115
Show newest version
package io.shiftleft.overflowdb.util;

import org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Subclass-safe variant of MultiIterator
 */
public final class MultiIterator2 implements Iterator, Serializable {

  private final List> iterators = new ArrayList<>();
  private int current = 0;

  public void addIterator(final Iterator iterator) {
    this.iterators.add(iterator);
  }

  @Override
  public boolean hasNext() {
    if (this.current >= this.iterators.size())
      return false;

    Iterator currentIterator = this.iterators.get(this.current);

    while (true) {
      if (currentIterator.hasNext()) {
        return true;
      } else {
        this.current++;
        if (this.current >= iterators.size())
          break;
        currentIterator = iterators.get(this.current);
      }
    }
    return false;
  }

  @Override
  public void remove() {
    this.iterators.get(this.current).remove();
  }

  @Override
  public T next() {
    if (this.iterators.isEmpty()) throw FastNoSuchElementException.instance();

    Iterator currentIterator = iterators.get(this.current);
    while (true) {
      if (currentIterator.hasNext()) {
        return currentIterator.next();
      } else {
        this.current++;
        if (this.current >= iterators.size())
          break;
        currentIterator = iterators.get(current);
      }
    }
    throw FastNoSuchElementException.instance();
  }

  public void clear() {
    this.iterators.clear();
    this.current = 0;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy