From af440943a41322147b663a4c069f3b9b33329bb2 Mon Sep 17 00:00:00 2001 From: aaron Date: Tue, 15 Dec 2009 23:04:40 +0000 Subject: [PATCH] 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 --- .../sting/gatk/iterators/PushbackIterator.java | 11 ++++++++++- .../sting/gatk/refdata/IntervalRodIterator.java | 5 +++++ .../org/broadinstitute/sting/gatk/refdata/RodGLF.java | 6 ++++++ .../org/broadinstitute/sting/gatk/refdata/RodVCF.java | 6 ++++++ .../sting/gatk/refdata/SAMPileupRecord.java | 5 +++++ .../sting/gatk/refdata/SeekableRODIterator.java | 5 +++-- .../broadinstitute/sting/gatk/refdata/rodGELI.java | 6 +++++- .../sting/gatk/refdata/rodPicardDbSNP.java | 10 ++++++---- .../sting/gatk/refdata/rodSAMPileup.java | 6 +++++- 9 files changed, 51 insertions(+), 9 deletions(-) diff --git a/java/src/org/broadinstitute/sting/gatk/iterators/PushbackIterator.java b/java/src/org/broadinstitute/sting/gatk/iterators/PushbackIterator.java index 712be5fba..b8633c401 100755 --- a/java/src/org/broadinstitute/sting/gatk/iterators/PushbackIterator.java +++ b/java/src/org/broadinstitute/sting/gatk/iterators/PushbackIterator.java @@ -27,12 +27,21 @@ public class PushbackIterator implements Iterator, Iterable { 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; diff --git a/java/src/org/broadinstitute/sting/gatk/refdata/IntervalRodIterator.java b/java/src/org/broadinstitute/sting/gatk/refdata/IntervalRodIterator.java index ea44e77a1..3aafcb69b 100755 --- a/java/src/org/broadinstitute/sting/gatk/refdata/IntervalRodIterator.java +++ b/java/src/org/broadinstitute/sting/gatk/refdata/IntervalRodIterator.java @@ -34,8 +34,13 @@ public class IntervalRodIterator implements Iterator { 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; diff --git a/java/src/org/broadinstitute/sting/gatk/refdata/RodGLF.java b/java/src/org/broadinstitute/sting/gatk/refdata/RodGLF.java index a74c6a468..56114d3e4 100644 --- a/java/src/org/broadinstitute/sting/gatk/refdata/RodGLF.java +++ b/java/src/org/broadinstitute/sting/gatk/refdata/RodGLF.java @@ -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 { 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; diff --git a/java/src/org/broadinstitute/sting/gatk/refdata/RodVCF.java b/java/src/org/broadinstitute/sting/gatk/refdata/RodVCF.java index b6174fe98..b9cd653ff 100755 --- a/java/src/org/broadinstitute/sting/gatk/refdata/RodVCF.java +++ b/java/src/org/broadinstitute/sting/gatk/refdata/RodVCF.java @@ -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); } diff --git a/java/src/org/broadinstitute/sting/gatk/refdata/SAMPileupRecord.java b/java/src/org/broadinstitute/sting/gatk/refdata/SAMPileupRecord.java index 686185811..60b4bd429 100644 --- a/java/src/org/broadinstitute/sting/gatk/refdata/SAMPileupRecord.java +++ b/java/src/org/broadinstitute/sting/gatk/refdata/SAMPileupRecord.java @@ -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); diff --git a/java/src/org/broadinstitute/sting/gatk/refdata/SeekableRODIterator.java b/java/src/org/broadinstitute/sting/gatk/refdata/SeekableRODIterator.java index 8289acc21..232ef04c4 100644 --- a/java/src/org/broadinstitute/sting/gatk/refdata/SeekableRODIterator.java +++ b/java/src/org/broadinstitute/sting/gatk/refdata/SeekableRODIterator.java @@ -78,7 +78,8 @@ public class SeekableRODIterator implements I public SeekableRODIterator(Iterator it) { this.it = new PushbackIterator(it); records = new LinkedList(); - 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 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; diff --git a/java/src/org/broadinstitute/sting/gatk/refdata/rodGELI.java b/java/src/org/broadinstitute/sting/gatk/refdata/rodGELI.java index 64bf552df..87e6836f1 100755 --- a/java/src/org/broadinstitute/sting/gatk/refdata/rodGELI.java +++ b/java/src/org/broadinstitute/sting/gatk/refdata/rodGELI.java @@ -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()); } diff --git a/java/src/org/broadinstitute/sting/gatk/refdata/rodPicardDbSNP.java b/java/src/org/broadinstitute/sting/gatk/refdata/rodPicardDbSNP.java index 20e8258ef..a259da718 100644 --- a/java/src/org/broadinstitute/sting/gatk/refdata/rodPicardDbSNP.java +++ b/java/src/org/broadinstitute/sting/gatk/refdata/rodPicardDbSNP.java @@ -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()); } diff --git a/java/src/org/broadinstitute/sting/gatk/refdata/rodSAMPileup.java b/java/src/org/broadinstitute/sting/gatk/refdata/rodSAMPileup.java index 917cec9be..33723c165 100644 --- a/java/src/org/broadinstitute/sting/gatk/refdata/rodSAMPileup.java +++ b/java/src/org/broadinstitute/sting/gatk/refdata/rodSAMPileup.java @@ -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();