DATACMNS-867 - Java 8 polish of ReflectiveRepositoryInformation.
Introduced Optionals.firstNonEmpty(…) to consume a set of Suppliers until one returns a non-empty Optional.
This commit is contained in:
@@ -21,6 +21,7 @@ import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
@@ -96,6 +97,36 @@ public class Optionals {
|
||||
.findFirst().orElse(defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the given {@link Supplier}s for {@link Optional} results one by one and returns the first non-empty one.
|
||||
*
|
||||
* @param suppliers must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <T> Optional<T> firstNonEmpty(Supplier<Optional<T>>... suppliers) {
|
||||
|
||||
Assert.notNull(suppliers, "Suppliers must not be null!");
|
||||
|
||||
return firstNonEmpty(Streamable.of(suppliers));
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the given {@link Supplier}s for {@link Optional} results one by one and returns the first non-empty one.
|
||||
*
|
||||
* @param suppliers must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static <T> Optional<T> firstNonEmpty(Iterable<Supplier<Optional<T>>> suppliers) {
|
||||
|
||||
Assert.notNull(suppliers, "Suppliers must not be null!");
|
||||
|
||||
return Streamable.of(suppliers).stream()//
|
||||
.map(it -> it.get())//
|
||||
.filter(it -> it.isPresent())//
|
||||
.findFirst().orElse(Optional.empty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next element of the given {@link Iterator} or {@link Optional#empty()} in case there is no next
|
||||
* element.
|
||||
|
||||
Reference in New Issue
Block a user