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

org.jamon.codegen.SequentialIterator Maven / Gradle / Ivy

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.jamon.codegen;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class SequentialIterator implements Iterator {
  public SequentialIterator(Iterator[] iters) {
    this.iters = iters.clone();
  }

  @SuppressWarnings("unchecked")
  public SequentialIterator(Iterator iter1, Iterator iter2) {
    this(new Iterator[] { iter1, iter2 });
  }

  @SuppressWarnings("unchecked")
  public SequentialIterator(
    Iterator iter1, Iterator iter2, Iterator iter3) {
    this(new Iterator[] { iter1, iter2, iter3 });
  }

  @SuppressWarnings("unchecked")
  public SequentialIterator(
    Iterator iter1,
    Iterator iter2,
    Iterator iter3,
    Iterator iter4) {
    this(new Iterator[] { iter1, iter2, iter3, iter4 });
  }

  private final Iterator[] iters;

  private int currentIter = 0;

  @Override
  public boolean hasNext() {
    if (currentIter >= iters.length) {
      return false;
    }
    else if (iters[currentIter].hasNext()) {
      return true;
    }
    else {
      currentIter++;
      return hasNext();
    }
  }

  @Override
  public T next() throws NoSuchElementException {
    if (hasNext()) {
      return iters[currentIter].next();
    }
    else {
      throw new NoSuchElementException();
    }
  }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy