com.jgcomptech.tools.events.EventManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-ultimate-tools Show documentation
Show all versions of java-ultimate-tools Show documentation
A large repository of scripts for use in any Java program.
package com.jgcomptech.tools.events;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Allows global event handler registration and forwards received
* events to the appropriate registered event handlers.
* @since 1.4.0
*/
public final class EventManager {
private static EventManager instance;
private final Map events = new HashMap<>();
private EventManager() { }
public static EventManager getInstance() {
if(instance == null) instance = new EventManager();
return instance;
}
private Event registerNewEvent(final String eventName,
final EventTarget extends Event> target) {
if (eventName == null || eventName.trim().isEmpty()) {
throw new IllegalArgumentException("Event name cannot be null!");
}
if (target == null) {
throw new IllegalArgumentException("Event target cannot be null!");
}
return events.put(eventName, new Event(target));
}
public T registerNewEvent(final String eventName,
final Class classRef,
final EventType extends T> eventType)
throws Exception {
return registerNewEvent(eventName, classRef, new EventTarget<>(), eventType);
}
public T registerNewEvent(final String eventName,
final Class classRef,
final EventType extends T> eventType,
final List