feat(commons): add StreamUtil for common stream utilities
ci/woodpecker/push/woodpecker Pipeline failed
Details
ci/woodpecker/push/woodpecker Pipeline failed
Details
This commit is contained in:
parent
704e1987c4
commit
5801716336
|
@ -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));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue