package ch.usi.inf.sp.callgraph.renderer;

import java.util.*;
import java.util.function.Consumer;

public class DuplicateAwareSet<E> {

    private final Map<E, E> elementsToElements = new HashMap<>();

    public E addIfNew(final E element) {
        if (elementsToElements.containsKey(element)) {
            return elementsToElements.get(element);
        }

        elementsToElements.put(element, element);
        return element;
    }

    public E addIfNew(final E element, Consumer<E> ifNew) {
        final E added = addIfNew(element);

        if (element == added) {
            ifNew.accept(element);
        }

        return added;
    }

    public Set<E> getAll() {
        return new HashSet<>(elementsToElements.keySet());
    }
}
