From 580171633649cffb1dff977a61936b2eb913b5c5 Mon Sep 17 00:00:00 2001 From: JFronny Date: Sat, 4 May 2024 22:53:03 +0200 Subject: [PATCH] feat(commons): add StreamUtil for common stream utilities --- .../io/gitlab/jfronny/commons/StreamUtil.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 commons/src/main/java/io/gitlab/jfronny/commons/StreamUtil.java diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/StreamUtil.java b/commons/src/main/java/io/gitlab/jfronny/commons/StreamUtil.java new file mode 100644 index 0000000..7612afe --- /dev/null +++ b/commons/src/main/java/io/gitlab/jfronny/commons/StreamUtil.java @@ -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 The type of the elements + * @param The type to keep + * @return A BiConsumer that keeps only elements of the specified type + */ + public static BiConsumer> keepOnly(Class type) { + return (element, consumer) -> { + if (type.isInstance(element)) { + consumer.accept(type.cast(element)); + } + }; + } +}