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

jpower.event.EventBus Maven / Gradle / Ivy

There is a newer version: 0.0.5
Show newest version
package jpower.event;

import jpower.core.Wrapper;

import java.util.List;
import java.util.ArrayList;

/**
 * Implementation of the core EventBus
 */
public class EventBus implements IEventBus
{

    /**
     * Registered handlers
     */
    protected final List handlers;

    public EventBus()
    {
        handlers = new ArrayList<>();
    }

    @Override
    public void register(final Object object)
    {
        handlers.add(new RegisteredHandler(object).setAnnotationType(EventHandler.class).registerMethods());
    }

    @Override
    public boolean unregister(final Object object)
    {
        for (RegisteredHandler rh : handlers)
        {
            if (rh.getObject() == object)
            {
                handlers.remove(rh);
                return true;
            }
        }
        return false;
    }

    @Override
    public void post(final Object event)
    {
        Wrapper didRun = new Wrapper<>(false);
        handlers.forEach(handler -> {
            if (handler.executeEvent(event))
            {
                didRun.set(true);
            }
        });
        if (!DeadEvent.class.isAssignableFrom(event.getClass()) && !didRun.get())
        {
            post(new DeadEvent(event));
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy