package ex6;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;

public final class Profiler {

    private static final Map<String, LongAdder> methodToLoopCount = new ConcurrentHashMap<>();

    static {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            for (final var entry : methodToLoopCount.entrySet()) {
                System.out.printf("Method name: %s - # executed loops: %d%n", entry.getKey(), entry.getValue().longValue());
            }
        }));
    }

    private Profiler() {
    }

    public static void countLoop(String methodName) {
        methodToLoopCount.computeIfAbsent(methodName, k -> new LongAdder());
        methodToLoopCount.get(methodName).increment();
    }
}
