title: using Optional and Suppliers to implement strategy pattern
body: <p>I implemented the code below as a nice generic solution to get something by trying one strategy after another until I find something or return empty if unsuccessful.</p>
  
  <pre><code>import java.util.Optional;
  import java.util.function.Supplier;
  
  public class StrategyEvaluator {
  
      /**
       * Evaluate supplier stategies that produce Optionals until one returns something.
       * @param suppliers
       * @return the optional provided by the first successful supplier or Optional.empty()
       */
      @SafeVarargs
      public static &lt;T&gt; Optional&lt;T&gt; evaluate(Supplier&lt;Optional&lt;T&gt;&gt;...suppliers) {
          for(Supplier&lt;Optional&lt;T&gt;&gt;s: suppliers) {
              Optional&lt;T&gt; maybe = s.get();
              if(maybe.isPresent()) {
                  return maybe;
              }
          }
          return Optional.empty();
      }
  }
  </code></pre>
  
  <p>This here is an example of how I use it:</p>
  
  <pre><code>public Optional&lt;Contact&gt; getContactForOwner(boolean cached, String contactId, String ownerId, boolean followLinks) {
  
      return StrategyEvaluator.evaluate(
              () -&gt; getFromDao(cached, contactId, ownerId, followLinks),
              () -&gt; getUserProfileContact(cached, contactId),
              () -&gt; searchContactByIdAndHandles(contactId, ownerId)
      );
  }
  </code></pre>
  
  <p>So, this works quite nicely and seems to do what it needs to do and it is a lot better than the nested if else's it replaced. However, I'm wondering if I'm not reinventing wheels here and whether there are better ways to do the same. Also I'm wondering whether there are any downsides to this approach in terms of overhead or generics weirdness? Varargs and generics don't really play that well with each other for example, hence the @SafeVarargs.</p>
  
  <p>I'm trying to avoid more heavy handed solutions like for example having a Strategy builder that has a list of suppliers and an evaluate method that I need to assign to an instance which I then reuse (i.e. the traditional Strategy pattern). I'm aiming for a tradeoff between having few lines of code when using it and not too much overhead. This looks kind of neat at least and it does the job.</p>