001    package com.hammurapi.eventbus;
002    
003    import java.util.ArrayList;
004    import java.util.Collection;
005    
006    import com.hammurapi.common.TokenExpander;
007    import com.hammurapi.extract.Predicate;
008    
009    /**
010     * This class adds a single method to the base class - it creates predicate/handler pairs from 
011     * @author Pavel Vlasov
012     *
013     * @param <E>
014     * @param <C>
015     */
016    public class ReflectiveEventHandlerFactory<E, C, H extends EventBus.Handle<E,Integer,C>, S extends EventStore<E,Integer,C,H,S>> extends IntrospectorBase<E, C> {
017    
018            protected ReflectiveEventHandlerFactory(ClassLoader classLoader, TokenExpander tokenExpander) {
019                    super(classLoader, tokenExpander);
020            }
021            
022            /**
023             * Introspects class and creates predicate/handler pairs.
024             * @param handlerClass
025             * @param eventType
026             * @return
027             */
028            @SuppressWarnings("unchecked")
029            public Collection<ReflectiveEventHandler<E,C,H,S>> createHandlers(C instance, Class<E> eventType) {
030                    Collection<ReflectiveEventHandler<E,C,H,S>> ret = new ArrayList<ReflectiveEventHandler<E,C,H,S>>();
031                    for (IntrospectionResult<E, C> ir: introspect((Class<C>) instance.getClass(), eventType)) {
032                            ReflectiveEventHandler<E, C, H, S> handler = new ReflectiveEventHandler<E,C,H,S>(instance, ir.getOffset(), ir.getMethod(), ir.getParameterTypes(), ir.getHandlerAnnotation(), eventType, ir.getPredicates());                       
033                            ret.add(handler);
034                    }
035    
036                    return ret;
037            }
038    }