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

com.jgcomptech.tools.events.EventTarget Maven / Gradle / Ivy

package com.jgcomptech.tools.events;

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

/**
 * Allows object event handler registration and forwards received
 * events to the appropriate registered event handlers.
 * @param  the event type to use for the target
 * @since 1.4.0
 */
public class EventTarget {
    private final Map, EventHandler> eventHandlers = new HashMap<>();

    /**
     * Sets the specified singleton handler. There can only be one such handler specified at a time.
     * @param eventType the event type to associate with the given eventHandler
     * @param eventHandler the handler to register, or null to unregister
     */
    public final void addEventHandler(final EventType eventType,
                                final EventHandler eventHandler) { eventHandlers.put(eventType, eventHandler); }

    /**
     * Removes the singleton handler assigned to the specified Event Type.
     * @param eventType the event type to associate with the given eventHandler
     */
    public final void removeEventHandler(final EventType eventType) { eventHandlers.remove(eventType); }

    /**
     * Returns the singleton handler assigned to the specified Event Type.
     * @param eventType the event type
     * @return the singleton handler assigned to the specified Event Type
     */
    public final EventHandler getEventHandler(final EventType eventType) {
        return eventHandlers.get(eventType); }

    /**
     * Fires the handle method in all registered EventHandlers.
     * @param event the event
     * @param eventType the event type
     */
    public final void fire(final Event event, final EventType eventType) {
        for (Map.Entry, EventHandler> entry : eventHandlers.entrySet()) {
            if(entry.getKey().equals(eventType)) {
                entry.getValue().handle((T) event);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy