Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2272

"Iterator.next()" methods should throw "NoSuchElementException"

    XMLWordPrintable

    Details

    • Message:
      Add a "NoSuchElementException" for iteration beyond the end of the collection.
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources
    • FindBugs:
      IT_NO_SUCH_ELEMENT

      Description

      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

      public class MyIterator implements Iterator<String>{
        ...
        public String next(){
          if(!hasNext()){
            return null;
          }
          ...
        }
      }
      

      Compliant Solution

      public class MyIterator implements Iterator<String>{
        ...
        public String next(){
          if(!hasNext()){
            throw new NoSuchElementException();
          }
          ...
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              freddy.mallet Freddy Mallet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: