title: Optional.ifAbsentThrow()?
body: <p>Suppose I need to find a <code>value</code> of a certain <code>order</code>, then get its <code>id</code>, and then its <code>localized-id</code>. And if I can't do it, I want to throw and exception:</p>
  
  <pre><code>return values.stream()
               .filter(value -&gt; value.getOrder("order") == order)
               .findAny()
               .map(Attribute::getId)
               .map(Id::getLocalizedId)
               .orElseThrow(() -&gt; new RuntimeException("Could not get the localized id of the value of order " + order));
  </code></pre>
  
  <p>The problem is that the exception is not very detailed: it tells me I can't get the localized-id, but not why.</p>
  
  <p>I miss some <code>Optional.ifAbsentThrow</code> method that would allow me to do this:</p>
  
  <pre><code>return values.stream()
               .filter(value -&gt; value.getOrder("order") == order)
               .findAny()
               .ifAbsentThrow(() -&gt; new RuntimeException("Could not find value of order " + order));
               .map(Attribute::getId)
               .ifAbsentThrow(() -&gt; new RuntimeException("Value of order " + order + " has no id"));
               .map(Id::getLocalizedId)
               .orElseThrow(() -&gt; new RuntimeException("Could get the id but not the localized id of the value of order " + order));
  </code></pre>
  
  <p>To solve this problem, I have created the following <code>ifAbsentThrow</code> method:</p>
  
  <pre><code>public static &lt;T, X extends RuntimeException&gt; Predicate&lt;T&gt; ifAbsentThrow(Supplier&lt;? extends X&gt; exceptionSupplier) throws RuntimeException {
      return valor -&gt; { 
                      if (valor == null) throw exceptionSupplier.get();
                      return true; 
                      };
      }
  </code></pre>
  
  <p>And I use it like this:</p>
  
  <pre><code>return values.stream()
               .filter(value -&gt; value.getOrder("order") == order)
               .findAny()
               .filter(ifAbsentThrow(() -&gt; new RuntimeException("Could not find value of order " + order));
               .map(Attribute::getId)
               .filter(ifAbsentThrow(() -&gt; new RuntimeException("Value of order " + order + " has no id"));
               .map(Id::getLocalizedId)
               .orElseThrow(() -&gt; new RuntimeException("Could get the id but not the localized id of the value of order " + order));
  </code></pre>
  
  <p><strong>My Questions:</strong> </p>
  
  <ul>
  <li>1) Am I missing something here? Is Optional really missing this
  functionality or I shouldn't be doing this for some reason? </li>
  <li>2) Is there a better, recommended way of throwing more detailed exceptions
  for missing values?</li>
  </ul>
  
  <p><strong>Edit:</strong> It now seems to me that <code>Optional.ifAbsentThrow</code> does not exist because it would be a way of dealing with <code>null</code> values, and <code>Optional</code> is all about not using <code>null</code> values in the first place. <code>Optional</code> clearly doesn't play well with null values, it gets verbose if you mix them. However, in the real world I find it difficult to deal with this all-or-nothing proposition: Some code gets translated to Optionals, while other remains using nullable values. To help me mix them, and refactor nullables to Optionals only when necessary, I believe I will be using the <code>GetNonNull</code> class below, built upon knowledge I gained from @Alex and @Holgers answers in this page. </p>