title: do-while with Java8-Optional
body: <p>I'm frequently using the do-while-checkNextForNull-getNext looping pattern (don't know if there is an official name for it) in some of my projects. But in Java8, the use of Optional is considered as cleaner code than checking for null references in client-code. But when using Optional in this looping pattern, the code gets a bit verbose and ugly, but because Optional has some handy methodS, I would expect that there must exist a cleaner way than the one I came up with below.</p>
  
  <p>Example:</p>
  
  <p>Given the following class.</p>
  
  <pre><code>class Item {
      int nr;
  
      Item(nr) {
          this.nr = nr;
          // an expensive operation
      }
  
      Item next() {
          return ...someCondition....
              ? new Item(nr + 1)
              : null;
      }
  }
  </code></pre>
  
  <p>In which the first item always has nr==1 and each item determines the next item, and you don't want to create unnecessary new items.</p>
  
  <p>I can use the following looping do-while-checkNextForNull-getNext pattern in client-code:</p>
  
  <pre><code>Item item = new Item(1);
  do {
      // do something with the item ....
  } while ((item = item.next()) != null);
  </code></pre>
  
  <p>With Java8-Optional, the given class becomes:</p>
  
  <pre><code>class Item {
      ....
  
      Optional&lt;Item&gt; next() {
          return ...someCondition....
              ? Optional.of(new Item(nr + 1))
              : Optional.empty();
      }
  }
  </code></pre>
  
  <p>And then the do-while-checkNextForNull-getNext looping pattern becomes a bit ugly and verbose:</p>
  
  <pre><code>Item item = new Item(1);
  do {
      // do something with the item ....
  } while ((item = item.next().orElse(null)) != null);
  </code></pre>
  
  <p>The <code>orElse(null)) != null</code> part feels uncomfortable.</p>
  
  <p>I have looked for other kind of loops, but haven't found a better one. Is there a cleaner solution?</p>
  
  <h1>Update:</h1>
  
  <p>It is possible to use a for-each loop while at the same time avoiding null-references (the use of null-references is considered as a bad practice). This solution has been proposed by Xavier Delamotte, and doesn't need Java8-Optional.</p>
  
  <p>Implementation with a generic iterator:</p>
  
  <pre><code>public class Item implements Iterable&lt;Item&gt;, Iterator&lt;Item&gt; {
      int nr;
  
      Item(int nr) { 
          this.nr = nr;
          // an expensive operation
      }
  
      public Item next() {
          return new Item(nr + 1);
      }
  
      public boolean hasNext() {
          return ....someCondition.....;
      }
  
      @Override
      public Iterator&lt;Item&gt; iterator() {
          return new CustomIterator(this);
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class CustomIterator&lt;T extends Iterator&lt;T&gt;&gt; implements Iterator&lt;T&gt; {
      T currentItem;
      boolean nextCalled;
  
      public CustomIterator(T firstItem) {
          this.currentItem = firstItem;
      }
  
      @Override
      public boolean hasNext() {
          return currentItem.hasNext();
      }
  
      @Override
      public T next() {
          if (! nextCalled) {
              nextCalled = true;
              return currentItem;
          } else {
              currentItem = currentItem.next();
              return currentItem;
          }
      }
  }
  </code></pre>
  
  <p>Then client code becomes very simple/clean:</p>
  
  <pre><code>for (Item item : new Item(1)) {
      // do something with the item ....
  }
  </code></pre>
  
  <p>Although this may be seen as a violation of the Iterator contract because the <code>new Item(1)</code> object is included in the loop, whereas normally, the for loop would immediately call next() and thus skipping the first object. In other words: for the first object, next() is violated because it returnS the first object itself.</p>