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

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

There is a newer version: 1.5.1
Show newest version
package com.jgcomptech.tools.events;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

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
     */
    @SuppressWarnings("unchecked")
    public final void fire(final Event event, final EventType eventType) {
        for (final var entry : eventHandlers.entrySet()) {
            if(entry.getKey().equals(eventType)) {
                entry.getValue().handle((T) event);
            }
        }
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;

        if (!(o instanceof EventTarget)) return false;

        final var eventTarget = (EventTarget) o;

        return new EqualsBuilder()
                .append(eventHandlers, eventTarget.eventHandlers)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(eventHandlers)
                .toHashCode();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("eventHandlers", eventHandlers)
                .toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy