All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jtransc.game.event.EventDispatcher Maven / Gradle / Ivy

Go to download

JVM AOT compiler currently generating Javascript and Haxe, with initial focus on Kotlin and games.

There is a newer version: 0.5.0
Show newest version
package com.jtransc.game.event;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class EventDispatcher {
    public interface Handler {
        void handle(T value);
    }
    
    private Map, ArrayList>> eventHandlers = new HashMap, ArrayList>>(); 

    public  void addEventListener(Class clazz, Handler handler) {
        if (!eventHandlers.containsKey(clazz)) {
            eventHandlers.put((Class) clazz, new ArrayList>());
        }
        eventHandlers.get((Class) clazz).add((Handler) handler);
    }

    public  void removeEventListener(Class clazz, Handler handler) {
        if (!eventHandlers.containsKey(clazz)) return;
        eventHandlers.get(clazz).remove(handler);
    }

    public void dispatchEvent(Event event) {
        ArrayList> handlers = eventHandlers.get(event.getClass());
        if (handlers != null) {
            for (Handler objectHandler : handlers) {
                objectHandler.handle(event);
            }
        }
    }
}