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;
}
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();
pushback(x);
return x;
}
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iteration has no more elements.
*/
public T next() {
if (pushedElement != null) {
final T ret = pushedElement;

View File

@ -34,8 +34,13 @@ public class IntervalRodIterator implements Iterator<IntervalRod> {
return iter.hasNext();
}
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iterator has no more elements.
*/
@Override
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());
//System.out.printf("IntervalRod next is %s%n", r);
return r;

View File

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

View File

@ -12,6 +12,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
@ -282,7 +283,12 @@ public class RodVCF extends BasicReferenceOrderedDatum implements VariationRod,
return mReader.hasNext();
}
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iterator has no more elements.
*/
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);
}

View File

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

View File

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

View File

@ -78,8 +78,12 @@ public class rodGELI extends BasicReferenceOrderedDatum {
public boolean hasNext() {
return iterator.hasNext();
}
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iterator has no more elements.
*/
public rodGELI next() {
if (!this.hasNext()) throw new NoSuchElementException("RodGELI next called on iterator with no more elements");
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.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
import java.util.Arrays;
import java.util.*;
/**
* @author alecw@broadinstitute.org
@ -270,7 +267,12 @@ public class rodPicardDbSNP implements VariationRod {
return reader.hasNext();
}
/**
* @return the next element in the iteration.
* @throws NoSuchElementException - iterator has no more elements.
*/
public rodPicardDbSNP next() {
if (!this.hasNext()) throw new NoSuchElementException("rodPicardDbSNP next called on iterator with no more elements");
return new rodPicardDbSNP(reader.next());
}

View File

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