// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Gatherer;

// Sampler<T>:
//   receive one T at a time
//   no state
//   pass one T at a time to next level
// where only every n element get passed
public class Sampler<T> implements Gatherer <T, T, T> {
    private int frequency;
    private int counter;
    public Sampler(int frequency) {
        this.frequency = frequency;
        this.counter = 0;
    }
    @Override
    public Integrator<T, T, T> integrator() {
        return Gatherer.Integrator.ofGreedy((win, elm, targstm) -> {
            counter++;
            if(counter % frequency == 0) {
                return targstm.push(elm);
            } else {
                return true;
            }
        });
    }
}
