edu.stanford.nlp.objectbank.ReaderIteratorFactory Maven / Gradle / Ivy
Show all versions of stanford-corenlp Show documentation
package edu.stanford.nlp.objectbank;
import edu.stanford.nlp.io.IOUtils;
import edu.stanford.nlp.util.AbstractIterator;
import java.io.*;
import java.net.URL;
import java.util.*;
/**
* A ReaderIteratorFactory provides a means of getting an Iterator
* which returns java.util.Readers over a Collection of input
* sources. Currently supported input sources are: Files, Strings,
* URLs and Readers. A ReaderIteratorFactory may take a Collection
* on construction and new sources may be added either individually
* (via the add(Object) method) or as a Collection (via the
* addAll(Collection method). The implementation automatically
* determines the type of input and produces a java.util.Reader
* accordingly. If you wish to add support for a new kind of input,
* refer the the setNextObject() method of the nested class
* ReaderIterator.
*
* The Readers returned by this class are not closed by the class when you
* move to the next element (nor at any other time). So, if you want the
* files closed, then the caller needs to close them. The caller can only
* do this if they pass in Readers. Otherwise, this class should probably
* close them but currently doesn't.
*
* TODO: Have this class close the files that it opens.
*
* @author Jenny Finkel
* @version 1.0
*/
//TODO: does this always store the same kind of thing in a given instance,
//or do you want to allow having some Files, some Strings, etc.?
public class ReaderIteratorFactory implements Iterable {
/**
* Constructs a ReaderIteratorFactory from the input sources
* contained in the Collection. The Collection should contain
* Objects of type File, String, URL and/or Reader. See class
* description for details.
*
* @param c Collection of input sources.
*/
public ReaderIteratorFactory(Collection c) {
this();
this.c.addAll(c);
}
public ReaderIteratorFactory(Collection c, String encoding){
this();
this.enc = encoding;
this.c.addAll(c);
}
/**
* Convenience constructor to construct a ReaderIteratorFactory from a single
* input source. The Object should be of type File, String, URL and Reader. See class
* description for details.
*
* @param o an input source that can be converted into a Reader
*/
public ReaderIteratorFactory(Object o) {
this(Collections.singleton(o));
}
public ReaderIteratorFactory(Object o, String encoding) {
this(Collections.singleton(o), encoding);
}
public ReaderIteratorFactory() {
c = new ArrayList<>();
}
/**
* The underlying Collection of input sources. Currently supported
* input sources are: Files, Strings, URLs and Readers. The
* implementation automatically determines the type of input and
* produces a java.util.Reader accordingly.
*/
protected Collection