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

roboguice.event.EventManager Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
package roboguice.event;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import roboguice.event.eventListener.ObserverMethodListener;
import roboguice.inject.ContextSingleton;

import com.google.inject.Inject;

import android.content.Context;

/**
 * Manager class handling the following:
 *
 *   Registration of event observing methods:
 *      registerObserver()
 *      unregisterObserver()
 *      clear()
 *   Raising Events:
 *      fire()
 *
 * @author Adam Tybor
 * @author John Ericksen
 */
@ContextSingleton
public class EventManager {
    @Inject protected Context context;

    protected Map, Set>> registrations = new HashMap, Set>>(); // synchronized set

    /**
     * Register the given EventListener to the contest and event class.
     *
     * @param event observed
     * @param listener to be triggered
     * @param  event type
     */
    public  void registerObserver( Class event, EventListener listener ) {
        Set> observers = registrations.get(event);
        if (observers == null) {
            observers = new CopyOnWriteArraySet>();
            registrations.put(event, observers);
        }

        observers.add(listener);

    }

    /**
     * Registers given method with provided context and event.
     *
     * @param instance to be called
     * @param method to be called
     * @param event observed
     */
    public  void registerObserver(Object instance, Method method, Class event) {
        registerObserver(event, new ObserverMethodListener(instance, method));
    }

    /**
     * Unregisters the provided event listener from the given event
     *
     * @param event observed
     * @param listener to be unregistered
     * @param  event type
     */
    public  void unregisterObserver(Class event, EventListener listener ) {

        final Set> observers = registrations.get(event);
        if (observers == null) return;

        observers.remove(listener);
    }

    /**
     * Unregister all methods observing the given event from the provided context.
     *
     * @param instance to be unregistered
     * @param event observed
     */
    public  void unregisterObserver(Object instance, Class event) {

        final Set> observers = registrations.get(event);
        if (observers == null) return;

        // As documented in http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#synchronizedSet(java.util.Set)
        //noinspection SynchronizationOnLocalVariableOrMethodParameter
        ObserverMethodListener toRemove = null;
        
        for (Iterator> iterator = observers.iterator(); iterator.hasNext();) {
            final EventListener listener = iterator.next();
            if( listener instanceof ObserverMethodListener ) {
                final ObserverMethodListener observer = ((ObserverMethodListener)listener);
                if (observer.getInstance() == instance) {
                    toRemove = observer;
                    break;
                }
            }
        }
        if( toRemove != null ) {
            observers.remove(toRemove);
        }
    }

    /**
     * Raises the event's class' event on the given context.  This event object is passed (if configured) to the
     * registered observer's method.
     *
     * @param event observed
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void fire(Object event) {

        final Set> observers = registrations.get(event.getClass());
        if (observers == null) return;

        for (EventListener observer : observers)
            //noinspection unchecked
            observer.onEvent(event);

    }

    protected Set> copyObservers(Set> observers) {
        // As documented in http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#synchronizedSet(java.util.Set)
        //noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (observers) {
            return new LinkedHashSet>(observers);
        }
    }


    public void destroy() {
        for( Entry, Set>> e : registrations.entrySet() )
            e.getValue().clear();
        registrations.clear();
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy