feat(commons): add StreamUtil for common stream utilities
ci/woodpecker/push/woodpecker Pipeline failed Details

This commit is contained in:
Johannes Frohnmeyer 2024-05-04 22:53:03 +02:00
parent 704e1987c4
commit 5801716336
Signed by: Johannes
GPG Key ID: E76429612C2929F4
1 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package io.gitlab.jfronny.commons;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class StreamUtil {
/**
* A BiConsumer that keeps only elements of the specified type.
* To be used with Stream.mapMulti.
*
* @param type The type to keep
* @param <E> The type of the elements
* @param <T> The type to keep
* @return A BiConsumer that keeps only elements of the specified type
*/
public static <E, T> BiConsumer<E, Consumer<T>> keepOnly(Class<T> type) {
return (element, consumer) -> {
if (type.isInstance(element)) {
consumer.accept(type.cast(element));
}
};
}
}