Created
September 17, 2015 06:09
-
-
Save leNicDev/cf01a13f8940958342c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Event { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.*; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
public @interface EventHandler { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.*; | |
import java.util.concurrent.ConcurrentHashMap; | |
/** | |
* @author leNic | |
* @version 1.0 | |
* Completely Thread-Safe | |
*/ | |
public class EventManager { | |
private static Map<Class<? extends Event>, Map<Listener, Method>> listeners = new ConcurrentHashMap<>(); | |
/** | |
* Add a new event class | |
* @param event Class object of the event | |
*/ | |
public static void addEvent(Class<? extends Event> event){ | |
listeners.putIfAbsent(event, new ConcurrentHashMap<>()); | |
} | |
/** | |
* Dispatch an event globally | |
* @param event Instance of the event | |
* @return Returns whether the event was dispatched successfully | |
*/ | |
public static boolean dispatchEvent(Event event){ | |
final Map<Listener, Method> methods = listeners.get(event.getClass()); | |
if(methods == null || methods.size() == 0) | |
return false; | |
try { | |
for (Map.Entry<Listener, Method> entry : methods.entrySet()) | |
entry.getValue().invoke(entry.getKey(), event); | |
return true; | |
} catch (IllegalAccessException | InvocationTargetException e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
/** | |
* Register new event listener | |
* @param listener | |
*/ | |
public static void registerListener(Listener listener){ | |
for(Method method : listener.getClass().getMethods()){ | |
if(!method.isAnnotationPresent(EventHandler.class)) | |
continue; | |
final Class<?>[] parameters = method.getParameterTypes(); | |
if(parameters.length != 1) | |
continue; | |
listeners.keySet().forEach(event -> { | |
if(parameters[0].isAssignableFrom(event)) | |
listeners.get(event).putIfAbsent(listener, method); | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Listener { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment