next
method should throw NoSuchElementException
JAVA-W0146348 }
349
350 @Override
351 public Map.Entry<String, JsonNode> next() {352 return wrap(itr.next());353 }354
355 @Override
356 public void remove() {
333 }
334
335 @Override
336 public Map.Entry<String, JsonNode> next() {337 return wrap(itr.next());338 }339
340 @Override
341 public void remove() {
333 }
334
335 @Override
336 public Map.Entry<String, JsonNode> next() {337 return wrap(itr.next());338 }339
340 @Override
341 public void remove() {
This class implements the java.util.Iterator
interface. However, its next()
method is not capable of throwing java.util.NoSuchElementException
.
This is a violation of the Iterator
interface's contract, and will not work with code that expects next()
to throw when the iterator is exhausted.
The next()
method should be changed so it throws NoSuchElementException
if is called when there are no more elements to return.
This is a nonconforming implementation and may mislead API consumers.
// Within iterator implementation
@Override
public T next() {
if (hasNext()) { ... }
else return null;
}
This implementation should be preferred:
@Override
public T next() {
if (hasNext()) { ... }
else throw NoSuchElementException();
}
If the iterator will never throw, it may be preferable to write hasNext()
to always return true
, while throwing if hasNext()
returns false. Obviously that would never occur, but it can serve to convey the intent. Always document such behavior for consumers of your API.
@Override
public boolean hasNext() {
return true;
}
Otherwise, a NoSuchElementException
must be thrown to ensure conformance with the Iterator
API.