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

public class StringStream implements InChannel<Integer> {

  /**
   * The current character in the string stream that we must return.
   */
  private int nextData = 0;

  /**
   * The string that will give us the data.
   */
  private String data;

  /**
   * Constructor.
   *
   * @param inputData
   *        the collection from where we will read data.
   */
  public StringStream(final Collection<String> inputData) {
    data = "";
    Iterator<String> i = inputData.iterator();
    while (i.hasNext()) {
      data += i.next();
    }
  }

  /**
   * Write the data into the file.
   * @return the value just read.
   * @throws IOException
   *         In case we fail to write the data.
   */
  public final Integer read() throws IOException {
    if (hasData()) {
      int i = data.charAt(nextData);
      nextData++;
      return i;
    } else {
      throw new IOException("Trying to read empty input channel.");
    }
  }

  /**
   * Is true if there is more data to be read, and it is false if the last
   * data has been read.
   * @return a boolean value.
   */
  public final boolean hasData() {
    return nextData < data.length();
  }
}
