net.pincette.rs.HeadTail Maven / Gradle / Ivy
package net.pincette.rs;
import java.util.concurrent.Flow.Processor;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* A processor that doesn't emit the head of a stream, but instead gives it to a function.
*
* @param the type of the incoming values.
* @param the type of the outgoing values.
* @author Werner Donn\u00e9
* @since 3.0
*/
public class HeadTail extends Mapper {
private final Consumer head;
private boolean headSeen;
/**
* Creates the processor.
*
* @param head the function that receives the first value.
* @param tail the function that receives all other values.
*/
public HeadTail(final Consumer head, final Function tail) {
super(tail);
this.head = head;
}
public static Processor headTail(final Consumer head, final Function tail) {
return new HeadTail<>(head, tail);
}
@Override
public void onNext(final T value) {
if (value == null) {
throw new NullPointerException("Can't emit null.");
}
if (!headSeen) {
headSeen = true;
head.accept(value);
more();
} else {
super.onNext(value);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy