public static <X> Optional<X> unless(@Nonnull Optional<X> first, Supplier<X> second) {
        return first.isPresent() ? first : Optional.of(second.get());
    }
public static < X > Optional < X > unless ( @ Nonnull Optional < X > first , Supplier < X > second ) { return first . isPresent ( ) ? first : Optional . of ( second . get ( ) ) ; }
Provide approximation of scala's Option.orElse(=&gt;Y). For when you want to evaluate
alternate values when an optional value is absent, where the alternates are not Optionals,
and the result will also be an Optional. (What happens in an Optional stays in an Optional.)
If first is absent, then lazily evaluate second, resulting in another Option.
<p>
It's important to note that the lambda in apply will only get evaluated if the argument "first"
is not present.
</p>
e.g.: z = unless(x).apply(() -&gt; somePossiblyExpensiveExpressionResultingInAnX);

@param first the value to check for presence and use if available
@param second alternate value supplier, only evaluate if first is absent
@param <X> type of value specified contained in first's Optional.

@return first if first is present, otherwise the supplier is evaluated (see example above)