package lc.mecha.lang;

import java.util.HashMap;
import java.util.Objects;

public class CountingList<T> {
    private final HashMap<T, Integer> counts = new HashMap<>();

    public void increment(T key, int amount) {
        Integer cnt = counts.get(key);
        if (cnt == null) {
            cnt = 0;
        }
        counts.put(key, cnt + amount);
    }

    public HashMap<T, Integer> getCounts() {
        return counts;
    }

    public long total() {
        int t = 0;
        for (Integer i : counts.values()) {
            t += i;
        }
        return t;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CountingList<?> that = (CountingList<?>) o;
        return Objects.equals(counts, that.counts);
    }

    @Override
    public int hashCode() {
        return Objects.hash(counts);
    }

    @Override
    public String toString() {
        return "CountingList{" +
                "counts=" + counts +
                '}';
    }
}
