public class MyIterator implements Iterator<Tree<String>> { private Iterator<File> fileIt; private Iterator<Tree<String>> treeIt = Iterators.emptyIterator(); public MyIterator(Iterator<File> fileIt) { this.fileIt = fileIt; } private Iterator<Tree<String>> getTreeIterator() { String nextPath = fileIt.next().getAbsolutePath(); Iterator<Tree<String>> treeIt = new Trees.PennTreeReader(IOUtils.openInHard(nextPath)); Method<Tree<String>, Tree<String>> transformMethod = new Method<Tree<String>, Tree<String>>() { public Tree<String> call(Tree<String> obj) { return treeTransformer.transformTree(obj); } }; Filter<Tree<String>> treeFilter = new Filter<Tree<String>>() { public boolean accept(Tree<String> t) { int length = t.getYield().size(); return (length >= minLength && length <= maxLength); } }; treeIt = new Iterators.TransformingIterator<Tree<String>, Tree<String>>(treeIt, transformMethod); treeIt = new Iterators.FilteredIterator<Tree<String>>(treeFilter, treeIt); return treeIt; } private boolean queueNext() { if (treeIt.hasNext()) { return true; } if (!fileIt.hasNext()) { return false; } treeIt = getTreeIterator(); return queueNext(); } public boolean hasNext() { return queueNext(); } public Tree<String> next() { queueNext(); return treeTransformer.transformTree(treeIt.next()); } public void remove() { throw new UnsupportedOperationException(); } }
public Iterator<Tree<String>> iterator() { List<File> files = IOUtils.getFilesUnder(dataRoot, IOUtil.getFileFilter(prefix, extension)); initializeTreeTransformer(); Iterator<Tree<String>> treeIt = new MyIterator(files.iterator()); return Iterators.maxLengthIterator(treeIt, maxNumTrees); }