    001    package com.hammurapi.eventbus;
    002
    003    import java.util.Queue;
    004    import java.util.Set;
    005
    006    import com.hammurapi.common.ExceptionHandler;
    007    import com.hammurapi.common.concurrent.TrackingExecutorService;
    008    import com.hammurapi.eventbus.AbstractEventBus.Handle;
    009    import com.hammurapi.eventbus.AbstractEventBus.Snapshot;
    010    import com.hammurapi.extract.Predicate;
    011
    012    /**
    013     * Wrapper interface.
    014     * @author Pavel Vlasov
    015     *
    016     * @param <E>
    017     * @param <P>
    018     */
    019    public class EventHandlerWrapperFilter<E, P extends Comparable<P>, C, K, H extends EventBus.Handle<E, P, C>, S extends EventStore<E,P,C,H,S>> implements EventHandlerWrapper<E, P, C, K, H, S> {
    020
    021            private EventHandlerWrapper<E, P, C, K, H, S> master;
    022            private Predicate<E, C> predicate;
    023
    024            public boolean consumes() {
    025                    return master.consumes();
    026            }
    027
    028            public P getPriority() {
    029                    return master.getPriority();
    030            }
    031
    032            @Override
    033            public void post(EventDispatchContext<E, P, C, H, S> context,
    034                            InferenceContext<E,P,C,K,H,S> inferenceContext,
    035                            Handle<E, P, C, K>... events) {
    036                    master.post(context, inferenceContext, events);
    037            }
    038
    039            public int getCardinality() {
    040                    return master.getCardinality();
    041            }
    042
    043            public void reset() {
    044                    master.reset();
    045            }
    046
    047            public C getContext() {
    048                    return master.getContext();
    049            }
    050
    051            public Set<K> getRegistrationKeys() {
    052                    return master.getRegistrationKeys();
    053            }
    054
    055            public EventHandler<E, P, C, H, S> getHandler() {
    056                    return master.getHandler();
    057            }
    058
    059            public void takeSnapshot(Snapshot<E, P, C, K, H, S> snapshot, Set<K> taken) {
    060                    master.takeSnapshot(snapshot, taken);
    061            }
    062
    063            public Predicate<E, C> getPredicate() {
    064                    return predicate;
    065            }
    066
    067            public EventHandlerWrapperFilter(EventHandlerWrapper<E, P, C, K, H, S> master, Predicate<E, C> predicate) {
    068                    this.master = peel(master);
    069                    this.predicate = predicate;
    070            }
    071
    072            public EventHandlerWrapper<E, P, C, K, H, S> getMaster() {
    073                    return master;
    074            }
    075
    076            public static <E, P extends Comparable<P>, C, K, H extends EventBus.Handle<E, P, C>, S extends EventStore<E,P,C,H,S>> EventHandlerWrapper<E, P, C, K, H, S> peel(EventHandlerWrapper<E, P, C, K, H, S> handler) {
    077                    if (handler instanceof EventHandlerWrapperFilter) {
    078                            return peel(((EventHandlerWrapperFilter<E, P, C, K, H, S>) handler).getMaster());
    079                    }
    080
    081                    return handler;
    082            }
    083
    084            @Override
    085            public boolean isOneOff() {
    086                    return master.isOneOff();
    087            }
    088
    089            @Override
    090            public Mode getMode() {
    091                    return master.getMode();
    092            }
    093
    094    }