package lc.mecha.lang;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * This class provides a standard map-of-sets, using lazy allocation.
 *
 * @author Alex Leigh
 * @since mk11 (ZERO CONTRAST)
 */
public class MapOfSets<E> {
    private HashMap<String, Set<E>> stack;

    /**
     * Return the given set to the caller. If the set has not yet been allocated, <code>null</code> is
     * returned.
     */
    public Set<E> getSet(final String eventType) {
        if (stack == null) return null;
        return stack.get(eventType);
    }

    /**
     * Get or create the given set and return it to the caller. If the set has not yet been allocated, a new one
     * will be created.
     */
    public Set<E> getOrCreateSet(final String eventType) {
        if (stack == null) stack = new HashMap<>();
        return stack.computeIfAbsent(eventType, k -> new HashSet<>());
    }
}
