Android Open Source - texting Event Repository






From Project

Back to project page texting.

License

The source code is released under:

Free to use, distribute, do anything.

If you think the Android project texting 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.skk.texting.evented;
//from  w  w w. ja  v a2  s  .  c o  m
import com.google.inject.Singleton;

import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

@Singleton
public final class EventRepository {

    private final HashMap<Event, ArrayList<WeakReference<EventHandler>>> events;
    private static EventRepository instance = new EventRepository();

    protected EventRepository() {
        events = new HashMap<Event, ArrayList<WeakReference<EventHandler>>>();
    }

    public static EventRepository getInstance() {
        return instance;
    }

    public void register(EventHandler handler, Event eventName) {

        ArrayList<WeakReference<EventHandler>> eventHandlers = emptyIfNull(events.get(eventName));

        if (!getInstances(eventHandlers).contains(handler))
            eventHandlers.add(new WeakReference<EventHandler>(handler));

        events.put(eventName, eventHandlers);
    }

    public void raiseEvent(Event event, EventData data) throws InvocationTargetException, IllegalAccessException {
        ArrayList<EventHandler> handlers = getHandlers(event);
        for (EventHandler handler : handlers) {
            Method method = getMethod(handler, event);
            if (method != null)
                method.invoke(handler, data);
        }
    }

    public ArrayList<EventHandler> getHandlers(Event event) {
        ArrayList<WeakReference<EventHandler>> weakHandlers = events.get(event);

        return getInstances(weakHandlers);
    }

    private <T> ArrayList<T> emptyIfNull(ArrayList<T> list) {
        return list != null ? list : new ArrayList<T>();
    }

    private <T> ArrayList<T> getInstances(ArrayList<WeakReference<T>> weakReferences) {
        ArrayList<T> instances = new ArrayList<T>();

        for (WeakReference<T> weakReference : emptyIfNull(weakReferences)) {
            T instance = weakReference.get();
            if (instance != null) {
                instances.add(instance);
            }
        }

        return instances;
    }

    private Method getMethod(EventHandler handler, Event event) {
        Method[] declaredMethods = handler.getClass().getDeclaredMethods();
        for (Method method : declaredMethods) {
            if (isHandleMethodFor(method, event)) {
                return method;
            }
        }

        return null;
    }

    private boolean isHandleMethodFor(Method method, Event event) {

        HandleEvent annotation = method.getAnnotation(HandleEvent.class);

        return annotation != null && annotation.eventType().equals(event);
    }

}




Java Source Code List

com.skk.texting.ApplicationStart.java
com.skk.texting.TextingApplication.java
com.skk.texting.adaptor.ContactsAdaptor.java
com.skk.texting.adaptor.ConversationAdaptor.java
com.skk.texting.adaptor.TextMessageAdaptor.java
com.skk.texting.adaptor.view.ViewHolder.java
com.skk.texting.adaptor.view.ViewType.java
com.skk.texting.async.AsyncCursorUpdate.java
com.skk.texting.async.BackgroundTask.java
com.skk.texting.constants.ApplicationConstants.java
com.skk.texting.constants.TextMessageConstants.java
com.skk.texting.customview.CustomRelativeLayout.java
com.skk.texting.di.RoboSmallApplication.java
com.skk.texting.domain.ConversationRepository.java
com.skk.texting.domain.Conversation.java
com.skk.texting.domain.Person.java
com.skk.texting.domain.TextMessage.java
com.skk.texting.eventdata.EmptyEventData.java
com.skk.texting.eventdata.IncomingSmsData.java
com.skk.texting.eventdata.RepliedSms.java
com.skk.texting.evented.EventData.java
com.skk.texting.evented.EventHandler.java
com.skk.texting.evented.EventRepository.java
com.skk.texting.evented.Event.java
com.skk.texting.evented.HandleEvent.java
com.skk.texting.factory.PersonFactory.java
com.skk.texting.gesture.MotionEventRecorder.java
com.skk.texting.gesture.OnSwipeGestureHandler.java
com.skk.texting.gesture.SwipeGestureHandler.java
com.skk.texting.gesture.SwipeGestureListener.java
com.skk.texting.listener.IncomingSmsListener.java
com.skk.texting.listener.ListItemClickListener.java
com.skk.texting.viewwrapper.AllContactsWrapper.java
com.skk.texting.viewwrapper.HeaderWrapper.java
com.skk.texting.viewwrapper.MessageConsoleWrapper.java
com.skk.texting.viewwrapper.TextMessagesView.java