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

org.vudroid.core.events.SafeEvent Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package org.vudroid.core.events;

import java.lang.reflect.Method;

public abstract class SafeEvent implements Event
{
    private final Class listenerType;

    protected SafeEvent()
    {
        listenerType = getListenerType();
    }

    private Class getListenerType()
    {
        for (Method method : getClass().getMethods())
        {
            if ("dispatchSafely".equals(method.getName()) && !method.isSynthetic())
            {
                return method.getParameterTypes()[0];
            }
        }
        throw new RuntimeException("Couldn't find dispatchSafely method");
    }

    @SuppressWarnings({"unchecked"})
    public final void dispatchOn(Object listener)
    {
        if (listenerType.isAssignableFrom(listener.getClass()))
        {
            dispatchSafely((T) listener);
        }
    }

    public abstract void dispatchSafely(T listener);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy