package utils;

public class TimingInterceptor{

    private int duration;

    public void measureAndDisplay(String name, Runnable func){
        measureExecutionTime(func);
        displayExecutionTime(name);
        reset();
    }

    private void measureExecutionTime(Runnable func) {

        long start, end;
        start = System.currentTimeMillis();
        func.run();
        end = System.currentTimeMillis();
        this.duration = (int)(end - start);
    }

    private void displayExecutionTime(String name){
        System.out.printf("""
                ###############################
                Name     : %s
                Duration : %dms
                ###############################
                """, name, this.duration);
    }

    private void reset() { this.duration = 0; }


}
