All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.yudichev.jiotty.common.lang.throttling.ThresholdGatedConsumerImpl Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package net.yudichev.jiotty.common.lang.throttling;

import javax.annotation.concurrent.NotThreadSafe;
import java.util.function.Consumer;

import static com.google.common.base.Preconditions.checkNotNull;

@NotThreadSafe
final class ThresholdGatedConsumerImpl implements ThresholdGatedConsumer {
    private final Consumer delegate;

    private final int threshold;
    private int count;

    ThresholdGatedConsumerImpl(int threshold, Consumer delegate) {
        this.delegate = checkNotNull(delegate);
        this.threshold = threshold;
    }

    @Override
    public void accept(T t) {
        if (count == threshold) {
            delegate.accept(t);
        }
        count++;
    }

    @Override
    public void reset() {
        count = 0;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy