Fixing a bug that Steven uncovered; we had an abigous contract for peek() in PushbackIterator, and SeekableRODIterator wasn't checking to see if it's PushbackIterator hasNext() was true before calling peek().

Changed peek() to element() to be consistant with the Java standards of the Queue and Stack classes (element() throws an exception if a record isn't available).  

Also updated some of the ROD iterator next() methods to throw NoSuchElementException if next() is called when a record isn't available.

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2372 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
aaron 2009-12-15 23:04:40 +00:00
parent 1035abc85f
commit af440943a4
9 changed files with 51 additions and 9 deletions

View File

@ -27,12 +27,21 @@ public class PushbackIterator<T> implements Iterator<T>, Iterable<T> {
return this; return this;
} }
public T peek() { /**
* Retrieves, but does not remove, the head of this iterator.
* @return T the next element in the iterator
* @throws NoSuchElementException - if the iterator doesn't have a next element
*/
public T element() {
T x = next(); T x = next();
pushback(x); pushback(x);
return x; return x;
} }
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iteration has no more elements.
*/
public T next() { public T next() {
if (pushedElement != null) { if (pushedElement != null) {
final T ret = pushedElement; final T ret = pushedElement;

View File

@ -34,8 +34,13 @@ public class IntervalRodIterator implements Iterator<IntervalRod> {
return iter.hasNext(); return iter.hasNext();
} }
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iterator has no more elements.
*/
@Override @Override
public IntervalRod next() { public IntervalRod next() {
if (!this.hasNext()) throw new NoSuchElementException("IntervalRodIterator next called on iterator with no more elements");
IntervalRod r = new IntervalRod(trackName, iter.next()); IntervalRod r = new IntervalRod(trackName, iter.next());
//System.out.printf("IntervalRod next is %s%n", r); //System.out.printf("IntervalRod next is %s%n", r);
return r; return r;

View File

@ -16,6 +16,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
/** /**
@ -349,8 +350,13 @@ public class RodGLF implements VariationRod, Iterator<RodGLF> {
return (mReader.hasNext()); return (mReader.hasNext());
} }
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iterator has no more elements.
*/
@Override @Override
public RodGLF next() { public RodGLF next() {
if (!this.hasNext()) throw new NoSuchElementException("RodGLF next called on iterator with no more elements");
mRecord = mReader.next(); mRecord = mReader.next();
mLoc = GenomeLocParser.createGenomeLoc(mReader.getReferenceName(), mReader.getCurrentLocation(), mReader.getCurrentLocation()); mLoc = GenomeLocParser.createGenomeLoc(mReader.getReferenceName(), mReader.getCurrentLocation(), mReader.getCurrentLocation());
return this; return this;

View File

@ -12,6 +12,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
/** /**
@ -282,7 +283,12 @@ public class RodVCF extends BasicReferenceOrderedDatum implements VariationRod,
return mReader.hasNext(); return mReader.hasNext();
} }
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iterator has no more elements.
*/
public RodVCF next() { public RodVCF next() {
if (!this.hasNext()) throw new NoSuchElementException("RodVCF next called on iterator with no more elements");
return new RodVCF(name, mReader.next(), mReader); return new RodVCF(name, mReader.next(), mReader);
} }

View File

@ -584,8 +584,13 @@ public class SAMPileupRecord implements Genotype, GenotypeList {
return parser.hasNext(); return parser.hasNext();
} }
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iterator has no more elements.
*/
@Override @Override
public SAMPileupRecord next() { public SAMPileupRecord next() {
if (!this.hasNext()) throw new NoSuchElementException("SAMPileupRecord next called on iterator with no more elements");
// if ( z == 0 ) t = System.currentTimeMillis(); // if ( z == 0 ) t = System.currentTimeMillis();
lastProcessedLine = parser.next(); lastProcessedLine = parser.next();
SAMPileupRecord n = new SAMPileupRecord(rodName); SAMPileupRecord n = new SAMPileupRecord(rodName);

View File

@ -78,7 +78,8 @@ public class SeekableRODIterator<ROD extends ReferenceOrderedDatum> implements I
public SeekableRODIterator(Iterator<ROD> it) { public SeekableRODIterator(Iterator<ROD> it) {
this.it = new PushbackIterator<ROD>(it); this.it = new PushbackIterator<ROD>(it);
records = new LinkedList<ROD>(); records = new LinkedList<ROD>();
ROD r = this.it.peek(); ROD r = null;
if (this.it.hasNext()) r = this.it.element();
name = (r==null?null:r.getName()); name = (r==null?null:r.getName());
} }
@ -132,7 +133,7 @@ public class SeekableRODIterator<ROD extends ReferenceOrderedDatum> implements I
// covered by new records, so we need to load them too: // covered by new records, so we need to load them too:
while ( it.hasNext() ) { while ( it.hasNext() ) {
ROD r = it.peek(); ROD r = it.element();
if ( r == null ) { if ( r == null ) {
it.next(); it.next();
continue; continue;

View File

@ -78,8 +78,12 @@ public class rodGELI extends BasicReferenceOrderedDatum {
public boolean hasNext() { public boolean hasNext() {
return iterator.hasNext(); return iterator.hasNext();
} }
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iterator has no more elements.
*/
public rodGELI next() { public rodGELI next() {
if (!this.hasNext()) throw new NoSuchElementException("RodGELI next called on iterator with no more elements");
return new rodGELI(rodName, iterator.next()); return new rodGELI(rodName, iterator.next());
} }

View File

@ -31,10 +31,7 @@ import org.broadinstitute.sting.utils.GenomeLoc;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.util.*;
import java.util.List;
import java.util.Collections;
import java.util.Arrays;
/** /**
* @author alecw@broadinstitute.org * @author alecw@broadinstitute.org
@ -270,7 +267,12 @@ public class rodPicardDbSNP implements VariationRod {
return reader.hasNext(); return reader.hasNext();
} }
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iterator has no more elements.
*/
public rodPicardDbSNP next() { public rodPicardDbSNP next() {
if (!this.hasNext()) throw new NoSuchElementException("rodPicardDbSNP next called on iterator with no more elements");
return new rodPicardDbSNP(reader.next()); return new rodPicardDbSNP(reader.next());
} }

View File

@ -113,9 +113,13 @@ public class rodSAMPileup extends BasicReferenceOrderedDatum implements Genotype
return parser.hasNext(); return parser.hasNext();
} }
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iterator has no more elements.
*/
@Override @Override
public rodSAMPileup next() { public rodSAMPileup next() {
if (!this.hasNext()) throw new NoSuchElementException("rodSAMPileup next called on iterator with no more elements");
rodSAMPileup result = new rodSAMPileup(rodName); rodSAMPileup result = new rodSAMPileup(rodName);
SAMPileupRecord r = parser.next(); SAMPileupRecord r = parser.next();