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

com.twitter.elephantbird.pig.util.LoadFuncTupleIterator Maven / Gradle / Ivy

There is a newer version: 4.17
Show newest version
package com.twitter.elephantbird.pig.util;

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

import org.apache.pig.LoadFunc;
import org.apache.pig.data.Tuple;

/**
 * Utility to simplify iteration over Tuples loaded by some {@link LoadFunc}.
 *
 * @author Andy Schlaikjer
 */
public class LoadFuncTupleIterator implements Iterator {
  private final LoadFunc loadFunc;
  private boolean hasNextCalled;
  private Tuple tuple;

  public LoadFuncTupleIterator(LoadFunc loadFunc) {
    super();
    this.loadFunc = loadFunc;
  }

  @Override
  public boolean hasNext() {
    if (!hasNextCalled) {
      hasNextCalled = true;
      if (tuple == null) {
        try {
          tuple = loadFunc.getNext();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    }
    return tuple != null;
  }

  @Override
  public Tuple next() {
    if (!hasNext())
      throw new NoSuchElementException();
    Tuple next = tuple;
    hasNextCalled = false;
    tuple = null;
    return next;
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy