/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */

package net.ranides.assira.events.mockup;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.ranides.assira.collection.maps.HashMultiMap;
import net.ranides.assira.collection.maps.MultiMap;
import net.ranides.assira.events.Event;
import net.ranides.assira.events.EventListener;
import net.ranides.assira.trace.ExceptionUtils;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class EventHitMap {

    private final MultiMap<String, Integer> map = new HashMultiMap<>();
        
        private int counter = 0;

        public List<String> names() {
            return new ArrayList<>(new TreeSet<>(map.keySet()));
        }
        
        public List<Integer> values(String name) {
            return new ArrayList<>(new TreeSet<>(map.getAll(name)));
        }
        
        public <T extends Event> EventListener<T> listener(String name) {
            return new HitListener<>(name);
        }

        private class HitListener<T extends Event> implements EventListener<T> {

            private final String name;

            public HitListener(String name) {
                this.name = name;
            }

            @Override
            public void handleEvent(T event) {
                synchronized(map) {
                    map.put(name, counter++);
                }
            }
        }
        
}
