Skip to content

Commit f3f7395

Browse files
author
Mohamed Ezzat
committed
squid:S2272 - "Iterator.next()" methods should throw "NoSuchElementException"
1 parent 0154b0b commit f3f7395

File tree

8 files changed

+40
-8
lines changed

8 files changed

+40
-8
lines changed

biojava-alignment/src/main/java/org/biojava/nbio/alignment/GuideTree.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,11 @@ public boolean hasNext() {
318318

319319
@Override
320320
public GuideTreeNode<S, C> next() {
321-
while (hasNext()) {
321+
if(!hasNext()){
322+
throw new NoSuchElementException();
323+
}
324+
325+
while (hasNext()) {
322326
Node next = nodes.peek(), child1 = (Node) next.getChild1(), child2 = (Node) next.getChild2();
323327
if (child1 != null && !child1.isVisited()) {
324328
nodes.push(child1);

biojava-core/src/main/java/org/biojava/nbio/core/search/io/Hit.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.util.Iterator;
2424
import java.util.List;
25+
import java.util.NoSuchElementException;
26+
2527
import org.biojava.nbio.core.sequence.template.Sequence;
2628

2729
/**
@@ -130,7 +132,10 @@ public boolean hasNext() {
130132

131133
@Override
132134
public Hsp next() {
133-
return hsps.get(current++);
135+
if(!hasNext()){
136+
throw new NoSuchElementException();
137+
}
138+
return hsps.get(current++);
134139
}
135140

136141
@Override

biojava-core/src/main/java/org/biojava/nbio/core/search/io/Result.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.Iterator;
2525
import java.util.List;
2626
import java.util.Set;
27+
import java.util.NoSuchElementException;
28+
2729
import org.biojava.nbio.core.sequence.template.Sequence;
2830

2931
/**
@@ -168,7 +170,10 @@ public boolean hasNext() {
168170

169171
@Override
170172
public Hit next() {
171-
return hits.get(currentResult++);
173+
if(!hasNext()){
174+
throw new NoSuchElementException();
175+
}
176+
return hits.get(currentResult++);
172177
}
173178

174179
@Override

biojava-core/src/main/java/org/biojava/nbio/core/search/io/SearchIO.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Iterator;
2828
import java.util.List;
2929
import java.util.ServiceLoader;
30+
import java.util.NoSuchElementException;
3031

3132
/**
3233
* Designed by Paolo Pavan.
@@ -172,7 +173,10 @@ public boolean hasNext() {
172173

173174
@Override
174175
public Result next() {
175-
return results.get(currentResult++);
176+
if(!hasNext()){
177+
throw new NoSuchElementException();
178+
}
179+
return results.get(currentResult++);
176180
}
177181

178182
@Override

biojava-core/src/main/java/org/biojava/nbio/core/sequence/views/WindowedSequence.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.util.Iterator;
2828
import java.util.List;
29+
import java.util.NoSuchElementException;
2930

3031
/**
3132
* A sliding window view of a sequence which does not implement any
@@ -142,7 +143,10 @@ public boolean hasNext() {
142143

143144
@Override
144145
public SequenceView<C> next() {
145-
SequenceView<C> v = seq.getSubSequence(currentIndex, currentIndex + offset);
146+
if(!hasNext()){
147+
throw new NoSuchElementException();
148+
}
149+
SequenceView<C> v = seq.getSubSequence(currentIndex, currentIndex + offset);
146150
currentIndex = currentIndex + window;
147151
return v;
148152
}

biojava-genome/src/main/java/org/biojava/nbio/genome/parsers/gff/LocIterator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.slf4j.LoggerFactory;
2626

2727
import java.util.Iterator;
28+
import java.util.NoSuchElementException;
2829

2930
/**
3031
* Move a sliding window over a Location.
@@ -169,7 +170,10 @@ public Location remainder()
169170
@Override
170171
public Location next()
171172
{
172-
return next( mWindowSize, mIncrement );
173+
if(!hasNext()){
174+
throw new NoSuchElementException();
175+
}
176+
return next( mWindowSize, mIncrement );
173177
}
174178

175179
/**

biojava-ontology/src/main/java/org/biojava/nbio/ontology/IntegerOntology.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ public boolean hasNext() {
8080

8181
@Override
8282
public Object next() {
83-
return resolveInt(i++);
83+
if(!hasNext()){
84+
throw new NoSuchElementException();
85+
}
86+
return resolveInt(i++);
8487
}
8588

8689
@Override

biojava-structure/src/main/java/org/biojava/nbio/structure/ResidueRange.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,10 @@ public boolean hasNext() {
275275

276276
@Override
277277
public ResidueNumber next() {
278-
ResidueNumber pos = next.getKey();
278+
if(!hasNext()){
279+
throw new NoSuchElementException();
280+
}
281+
ResidueNumber pos = next.getKey();
279282
loadNext();
280283
return pos;
281284
}

0 commit comments

Comments
 (0)