title: Is there an elegant way to get the first non null value of multiple method returns in Java?
body: <p>You have already seen this many times yourself, of that I'm sure:</p>
  
  <pre><code>public SomeObject findSomeObject(Arguments args) {
      SomeObject so = queryFirstSource(args); // the most likely source first, hopefully
      if (so != null) return so;
  
      so = querySecondSource(args); // a source less likely than the first, hopefully
      if (so != null) return so;
  
      so = queryThirdSource(args); // a source less likely than the previous, hopefully
      if (so != null) return so;
  
      // and so on
  }
  </code></pre>
  
  <p>We have different sources where an object we search could be. As a more vivid example we could image that we first check if a userid is in a list of privileged users. If not we check if the userid is in the list of allowed users. Else we return null. (It's not the best example but I hope it's a vivid-enough one.)</p>
  
  <p><a href="https://code.google.com/p/guava-libraries/" rel="noreferrer">Guava</a> offers us some helpers that could beautify that code above:</p>
  
  <pre><code>public SomeObject findSomeObject(Arguments args) {
      // if there are only two objects
      return com.google.common.base.Objects.firstNonNull(queryFirstSource(args), querySecondSource(args));
  
      // or else
      return com.google.common.collect.Iterables.find(
          Arrays.asList(
              queryFirstSource(args)
              , querySecondSource(args)
              , queryThirdSource(args)
              // , ...
          )
          , com.google.common.base.Predicates.notNull()
      );
   }
  </code></pre>
  
  <p>But, as the more experienced among us will have already seen, this may perform bad if the lookups (i.e. <code>queryXXXXSource(args)</code>) take a certain amount of time. This is because we now query all sources first and then pass the results to the method that finds the first among those results which is not <code>null</code>.</p>
  
  <p>In contrast to the first example, where the next source is only evaluated when the former does not return something, this second solution may look better at first but could perform much worse.</p>
  
  <p>Here's where we come to my actual question and to where I suggest something of that I hope someone has already implemented the base of it or of that someone might propose a even smarted solution.</p>
  
  <p>In plain English: <em>Has someone already implemented such a <code>defferedFirstNonNull</code> (see below) or something similar? Is there an easy plain-Java solution to achieve this with the new <a href="http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="noreferrer">Stream</a> framework? Can you propose another elegant solution that achieves the same result?</em></p>
  
  <p><strong>Rules:</strong> Java 8 is allowed as well as active maintained and well-known third party libraries like Google's Guava or Apache's Commons Lang with Apache License or similar (No GPL!).</p>
  
  <p>The proposed solution:</p>
  
  <pre><code>public SomeObject findSomeObject(Arguments args) {
      return Helper.deferredFirstNonNull(
          Arrays.asList(
              args -&gt; queryFirstSource(args)
              , args -&gt; querySourceSource(args)
              , args -&gt; queryThirdSource(args)
          )
          , x -&gt; x != null
       )
   }
  </code></pre>
  
  <p>So the method <code>defferedFirstNonNull</code> would evaluate each lambda expression after another and as soon as the predicate (<code>x -&gt; x != null</code>) is true (i.e. we found a match) the method would return the result immediately and would not query any further source.</p>
  
  <p>PS: I know that the expressions <code>args -&gt; queryXXXXSource(args)</code> could be shortened to <code>queryXXXXSource</code>. But that would render the proposed solution harder to read because it's not obvious on first sight what is going to happen.</p>