Package one.x1f.sip.foundation.core.util
Class StreamHelper
java.lang.Object
one.x1f.sip.foundation.core.util.StreamHelper
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional<T>
findAtMostOne
(Stream<T> stream, Predicate<T> predicate, Supplier<RuntimeException> multipleException) Returns at most one element from the givenstream
matching the givenpredicate
, but throws the exception retrieved frommultipleException
if more match.typeFilter
(Class<T> type) Filters a stream of objects by type.
-
Constructor Details
-
StreamHelper
public StreamHelper()
-
-
Method Details
-
typeFilter
Filters a stream of objects by type.Usage example:
List<Object>.stream().flatMap(typeFilter(String.class)).toList()
- Type Parameters:
S
- The type of the streamT
- Filtering type- Parameters:
type
- The filtering type- Returns:
- Filtering function
-
findAtMostOne
public static <T> Optional<T> findAtMostOne(Stream<T> stream, Predicate<T> predicate, Supplier<RuntimeException> multipleException) Returns at most one element from the givenstream
matching the givenpredicate
, but throws the exception retrieved frommultipleException
if more match.- Type Parameters:
T
- Element type- Parameters:
stream
- Stream to retrieve unique element frompredicate
- Predicate for filteringmultipleException
- Error thrown if more than one element matches the predicate- Returns:
- Optional element found
-