net.yudichev.jiotty.common.lang.DispatchingConflatingConsumer Maven / Gradle / Ivy
package net.yudichev.jiotty.common.lang;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Supplier;
import static com.google.common.base.Preconditions.checkNotNull;
public final class DispatchingConflatingConsumer implements Consumer {
private final Executor executor;
private final Consumer> delegate;
private final ConflatingInbox inbox = new ConflatingInbox<>();
public DispatchingConflatingConsumer(Executor executor, Consumer> delegate) {
this.executor = checkNotNull(executor);
this.delegate = checkNotNull(delegate);
}
@Override
public void accept(T t) {
if (inbox.add(t)) {
executor.execute(this::processQueue);
}
}
private void processQueue() {
delegate.accept(() -> inbox.get().orElseThrow(IllegalStateException::new));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy