1 | package com.hammurapi.eventbus.local; |
2 | |
3 | import java.util.Queue; |
4 | |
5 | import com.hammurapi.eventbus.AbstractEventBus; |
6 | import com.hammurapi.eventbus.AbstractEventBus.Handle; |
7 | import com.hammurapi.eventbus.EventDispatchContext; |
8 | import com.hammurapi.eventbus.EventHandler; |
9 | import com.hammurapi.eventbus.EventHandlerBase.Mode; |
10 | import com.hammurapi.eventbus.EventStore; |
11 | import com.hammurapi.eventbus.InferenceCommand; |
12 | import com.hammurapi.eventbus.InferenceContext; |
13 | import com.hammurapi.eventbus.InferencePolicy; |
14 | import com.hammurapi.eventbus.PostCommand; |
15 | import com.hammurapi.eventbus.RemoveCommand; |
16 | import com.hammurapi.extract.Predicate; |
17 | |
18 | class LocalEventDispatchContextImpl<E, P extends Comparable<P>, C, S extends EventStore<E, P, C, AbstractEventBus.Handle<E, P, C, Long>, S>> implements EventDispatchContext<E, P, C, AbstractEventBus.Handle<E,P,C,Long>, S> { |
19 | |
20 | static ThreadLocal<LocalEventDispatchContextImpl<?, ?, ?, ?>> threadContext = new ThreadLocal<LocalEventDispatchContextImpl<?,?,?,?>>(); |
21 | |
22 | private Handle<E,P,C,Long>[] handles; |
23 | private Long registrationKey; |
24 | private E[] events; |
25 | private EventHandler<E, P, C, AbstractEventBus.Handle<E,P,C,Long>, S> eventHandler; |
26 | |
27 | private Mode mode; |
28 | |
29 | private InferenceContext<E, P, C, Long, Handle<E, P, C, Long>, S> inferenceContext; |
30 | |
31 | public LocalEventDispatchContextImpl( |
32 | InferenceContext<E, P, C, Long, AbstractEventBus.Handle<E,P,C,Long>, S> inferenceContext, |
33 | EventHandler<E, P, C, AbstractEventBus.Handle<E,P,C,Long>, S> eventHandler, |
34 | Long registrationKey, |
35 | Handle<E,P,C,Long>[] handles, |
36 | E[] events, |
37 | Mode mode) { |
38 | this.inferenceContext = inferenceContext; |
39 | this.handles = handles; |
40 | this.registrationKey = registrationKey; |
41 | this.events = events; |
42 | this.eventHandler = eventHandler; |
43 | this.mode = mode; |
44 | } |
45 | |
46 | @Override |
47 | public void consume(int index) { |
48 | if (!eventHandler.consumes()) { |
49 | throw new IllegalStateException("This handler's declared that it doesn't consume events"); |
50 | } |
51 | InferenceCommand<E, P, C, Long, Handle<E, P, C, Long>, S> consumeCommand = new RemoveCommand<E, P, C, Long, Handle<E, P, C, Long>, S>(handles[index], registrationKey, eventHandler, inferenceContext, false, handles); |
52 | inferenceContext.postInferenceCommand(consumeCommand); |
53 | } |
54 | |
55 | @Override |
56 | public void consume(E event) { |
57 | for (int i=0; i<events.length; ++i) { |
58 | if (event==events[i]) { |
59 | consume(i); |
60 | } |
61 | } |
62 | } |
63 | |
64 | @Override |
65 | public void post(E event, Predicate<E, S>... validators) { |
66 | InferenceCommand<E, P, C, Long, Handle<E, P, C, Long>, S> postCommand = new PostCommand<E, P, C, Long, Handle<E, P, C, Long>, S>(event, false, registrationKey, eventHandler, handles, inferenceContext, validators); |
67 | inferenceContext.postInferenceCommand(postCommand); |
68 | } |
69 | |
70 | @Override |
71 | public void removeHandler() { |
72 | inferenceContext.getBus().removeHandlers(registrationKey); |
73 | } |
74 | |
75 | @Override |
76 | public void update(E event) { |
77 | for (int i=0; i<events.length; ++i) { |
78 | if (event==events[i]) { |
79 | if (handles[i] instanceof FacadeHandle) { |
80 | update(((FacadeHandle<E,P,C,S>) handles[i]).getMaster()); |
81 | } else { |
82 | update((MasterHandle<E, P, C, S>) handles[i]); |
83 | } |
84 | } |
85 | } |
86 | } |
87 | |
88 | void update(MasterHandle<E, P, C, S> handle) { |
89 | InferenceCommand<E, P, C, Long, Handle<E, P, C, Long>, S> removeCommand = new RemoveCommand<E, P, C, Long, Handle<E, P, C, Long>, S>(handle, registrationKey, eventHandler, inferenceContext, true, handles); |
90 | inferenceContext.postInferenceCommand(removeCommand); |
91 | |
92 | InferenceCommand<E, P, C, Long, Handle<E, P, C, Long>, S> postCommand = new PostCommand<E, P, C, Long, Handle<E, P, C, Long>, S>(handle, registrationKey, eventHandler, handles, inferenceContext); |
93 | inferenceContext.postInferenceCommand(postCommand); |
94 | } |
95 | |
96 | @Override |
97 | public S getEventStore() { |
98 | return (S) ((LocalEventBusBase) inferenceContext.getBus()).unmodifiableStore; |
99 | } |
100 | |
101 | @Override |
102 | public Mode getMode() { |
103 | return mode; |
104 | } |
105 | |
106 | } |