rspec/rules/S2272/java/rule.adoc

33 lines
679 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
By contract, any implementation of the ``++java.util.Iterator.next()++`` method should throw a ``++NoSuchElementException++`` exception when the iteration has no more elements. Any other behavior when the iteration is done could lead to unexpected behavior for users of this ``++Iterator++``.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
public class MyIterator implements Iterator<String>{
...
public String next(){
if(!hasNext()){
return null;
}
...
}
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
public class MyIterator implements Iterator<String>{
...
public String next(){
if(!hasNext()){
throw new NoSuchElementException();
}
...
}
}
----