| 1 | package com.hammurapi.eventbus.local; |
| 2 | |
| 3 | import java.lang.reflect.Array; |
| 4 | import java.util.Arrays; |
| 5 | import java.util.concurrent.atomic.AtomicBoolean; |
| 6 | |
| 7 | import com.hammurapi.eventbus.AbstractEventBus; |
| 8 | import com.hammurapi.eventbus.AbstractEventBus.Handle; |
| 9 | import com.hammurapi.eventbus.EventHandler; |
| 10 | import com.hammurapi.eventbus.EventStore; |
| 11 | import com.hammurapi.eventbus.InferenceContext; |
| 12 | import com.hammurapi.eventbus.RetractCommand; |
| 13 | |
| 14 | /** |
| 15 | * Stores handles and handler for posting retract command. |
| 16 | * @author Pavel Vlasov |
| 17 | * |
| 18 | */ |
| 19 | class RemoveListener<E, P extends Comparable<P>, C, S extends EventStore<E,P,C,AbstractEventBus.Handle<E,P,C,Long>,S>> { |
| 20 | |
| 21 | /** |
| 22 | * @return Handler which posted this event. |
| 23 | */ |
| 24 | private EventHandler<E, P, C, AbstractEventBus.Handle<E,P,C,Long>, S> handler; |
| 25 | |
| 26 | /** |
| 27 | * @return List of handler inputs which produced this event. |
| 28 | */ |
| 29 | private E[] events; |
| 30 | |
| 31 | private Long handlerId; |
| 32 | |
| 33 | RemoveListener( |
| 34 | EventHandler<E, P, C, Handle<E, P, C, Long>, S> handler, |
| 35 | Long handlerId, |
| 36 | Handle<E, P, C, Long>[] handles, |
| 37 | Class<E> eventType) { |
| 38 | super(); |
| 39 | this.handler = handler; |
| 40 | this.handlerId = handlerId; |
| 41 | events = (E[]) Array.newInstance(eventType, handles.length); |
| 42 | for (int i=0; i<events.length; ++i) { |
| 43 | events[i] = handles[i].getEvent(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | private AtomicBoolean fired = new AtomicBoolean(); |
| 48 | |
| 49 | void postRetractCommand(InferenceContext<E, P, C, Long, Handle<E, P, C, Long>, S> inferenceContext) { |
| 50 | if (!fired.getAndSet(true)) { |
| 51 | inferenceContext.postInferenceCommand(new RetractCommand<E, P, C, Long, AbstractEventBus.Handle<E,P,C,Long>, S>(events, handlerId, handler, inferenceContext.createNext())); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | } |