com.slimgears.util.stream.Optionals Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stream-utils Show documentation
Show all versions of stream-utils Show documentation
General purpose utils / module: stream-utils
package com.slimgears.util.stream;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
public class Optionals {
public static Function> ofType(Class type) {
return val -> Optional
.ofNullable(val)
.filter(type::isInstance)
.map(type::cast);
}
@SafeVarargs
public static Optional or(Supplier>... variants) {
return Stream.of(variants)
.map(Supplier::get)
.filter(Optional::isPresent)
.findFirst()
.orElse(Optional.empty());
}
}