Android Open Source - MVC Event Dispatcher






From Project

Back to project page MVC.

License

The source code is released under:

Apache License

If you think the Android project MVC listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.musselwhizzle.mvc.events;
// w  w  w.j  a v a2  s  .c  o  m
import java.util.HashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import android.util.Log;


public class EventDispatcher implements Dispatcher {

    private static final String TAG = EventDispatcher.class.getSimpleName();

    private HashMap<String, CopyOnWriteArrayList<EventListener>> listenerMap;
    private Dispatcher target;

    public EventDispatcher() {
        this(null);
    }
    public EventDispatcher(Dispatcher target) {
        listenerMap = new HashMap<String, CopyOnWriteArrayList<EventListener>>();
        this.target = (target != null) ? target : this;
    }

    @Override
    public void addListener(String type, EventListener listener) {
        synchronized (listenerMap) {
            CopyOnWriteArrayList<EventListener> list = listenerMap.get(type);
            if (list == null) {
                list = new CopyOnWriteArrayList<EventListener>();
                listenerMap.put(type, list);
            }
            list.add(listener);
        }
    }

    @Override
    public void removeListener(String type, EventListener listener) {
        synchronized (listenerMap) {
            CopyOnWriteArrayList<EventListener> list = listenerMap.get(type);
            if (list == null) return;
            list.remove(listener);
            if (list.size() == 0) {
                listenerMap.remove(type);
            }
        }
    }

    @Override
    public boolean hasListener(String type, EventListener listener) {
        synchronized (listenerMap) {
            CopyOnWriteArrayList<EventListener> list = listenerMap.get(type);
            if (list == null) return false;
            return list.contains(listener);
        }
    }

    @Override
    public void dispatchEvent(Event event) {
        if (event == null) {
            Log.e(TAG, "can not dispatch null event");
            return;
        }
        String type = event.getType();
        event.setSource(target);
        CopyOnWriteArrayList<EventListener> list;
        synchronized (listenerMap) {
            list = listenerMap.get(type);
        }
        if (list == null) return;
        for (EventListener l : list) {
            l.onEvent(event);
        }
    }

    public void dispose() {
        synchronized (listenerMap) {
            listenerMap.clear();
        }
        target = null;
    }
}




Java Source Code List

com.musselwhizzle.mvc.activities.MainActivity.java
com.musselwhizzle.mvc.events.Dispatcher.java
com.musselwhizzle.mvc.events.EventDispatcher.java
com.musselwhizzle.mvc.events.EventListener.java
com.musselwhizzle.mvc.events.Event.java
com.musselwhizzle.mvc.events.SimpleEvent.java
com.musselwhizzle.mvc.models.AppModel.java
com.musselwhizzle.mvc.views.MainView.java
com.musselwhizzle.mvc.widgets.DigitObjectPool.java
com.musselwhizzle.mvc.widgets.Digit.java