package de.accso.flexinale.common.shared_kernel;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public final class ThreadSafeCounterMap<K> {
    private final ConcurrentHashMap<K, AtomicLong> backingMap = new ConcurrentHashMap<>();

    public long increment(K key) {
        return backingMap.computeIfAbsent(key, k -> new AtomicLong()).incrementAndGet();
    }

    public long get(K key) {
        AtomicLong counter = backingMap.get(key);
        return counter != null ? counter.get() : 0;
    }

    public void set(K key, Long value) {
        backingMap.put(key, new AtomicLong(value));
    }
}
