
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
49 lines
965 B
Plaintext
49 lines
965 B
Plaintext
== Why is this an issue?
|
|
|
|
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++``.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
public class MyIterator implements Iterator<String>{
|
|
...
|
|
public String next(){
|
|
if(!hasNext()){
|
|
return null;
|
|
}
|
|
...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
public class MyIterator implements Iterator<String>{
|
|
...
|
|
public String next(){
|
|
if(!hasNext()){
|
|
throw new NoSuchElementException();
|
|
}
|
|
...
|
|
}
|
|
}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Add a "NoSuchElementException" for iteration beyond the end of the collection.
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|