package org.implab.gradle.internal;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;

public class ReplayableQueue<T> {
    private final List<Consumer<? super T>> consumers = new LinkedList<>();
    private final List<T> values = new LinkedList<>();

    public void add(T value) {
        consumers.forEach(consumer -> consumer.accept(value));
        values.add(value);
    }

    List<T> values() {
        return List.copyOf(values);
    }

    public void forEach(Consumer<? super T> consumer) {
        values.forEach(consumer);
        consumers.add(consumer);
    }
}