package ex4;

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> exceptionNameToCount = new ConcurrentHashMap<>();

    static {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            for (final var entry : exceptionNameToCount.entrySet()) {
                System.out.printf("Exception: %s - occurrences: %d%n", entry.getKey(), entry.getValue().intValue());
            }
        }));
    }

    private Profiler() {
    }

    public static void countException(Throwable e) {
        final String exceptionName = e.getClass().getName();
        exceptionNameToCount.computeIfAbsent(exceptionName, k -> new LongAdder()).increment();
    }


}
