1 | package com.hammurapi.eventbus.local; |
2 | |
3 | import java.lang.reflect.Array; |
4 | import java.util.ArrayList; |
5 | import java.util.Collection; |
6 | import java.util.List; |
7 | import java.util.Map; |
8 | import java.util.concurrent.Callable; |
9 | import java.util.concurrent.ExecutorService; |
10 | import java.util.concurrent.Future; |
11 | import java.util.concurrent.atomic.AtomicReference; |
12 | |
13 | import com.hammurapi.eventbus.AbstractEventBus; |
14 | import com.hammurapi.eventbus.AbstractEventBus.Handle; |
15 | import com.hammurapi.eventbus.EventHandlerWrapper; |
16 | import com.hammurapi.eventbus.EventStore; |
17 | import com.hammurapi.eventbus.PredicatedInferenceNode; |
18 | import com.hammurapi.extract.Extractor; |
19 | import com.hammurapi.extract.Predicate; |
20 | |
21 | class LocalPredicatedInferenceNode<E, P extends Comparable<P>, C, S extends EventStore<E, P, C, AbstractEventBus.Handle<E, P, C, Long>, S>> extends PredicatedInferenceNode<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S> { |
22 | |
23 | private final class CollectorTask implements Callable<Collection<EventHandlerWrapper<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>>> { |
24 | private final E event; |
25 | private final AtomicReference<Collection<Future<Collection<EventHandlerWrapper<E, P, C, Long, Handle<E, P, C, Long>, S>>>>> collector; |
26 | private final ExecutorService executor; |
27 | private final Map<C, Map<Extractor<E, ? super Boolean, C>, ? super Boolean>> cache; |
28 | |
29 | private CollectorTask( |
30 | E event, |
31 | AtomicReference<Collection<Future<Collection<EventHandlerWrapper<E, P, C, Long, Handle<E, P, C, Long>, S>>>>> collector, |
32 | ExecutorService executor, |
33 | Map<C, Map<Extractor<E, ? super Boolean, C>, ? super Boolean>> cache) { |
34 | this.event = event; |
35 | this.collector = collector; |
36 | this.executor = executor; |
37 | this.cache = cache; |
38 | } |
39 | |
40 | @Override |
41 | public Collection<EventHandlerWrapper<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>> call() throws Exception { |
42 | @SuppressWarnings("unchecked") |
43 | E[] events = (E[]) Array.newInstance(matcher.getEventBus().getEventType(), 1); |
44 | events[0] = event; |
45 | |
46 | if (getPredicate().extract(getContext(), cache, events)) { |
47 | for (PredicatedInferenceNode<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S> trueChild: trueChildren) { |
48 | trueChild.collectHandlers(cache, executor, collector, event); |
49 | } |
50 | return trueHandlers; |
51 | } else { |
52 | for (PredicatedInferenceNode<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S> falseChild: falseChildren) { |
53 | falseChild.collectHandlers(cache, executor, collector, event); |
54 | } |
55 | return falseHandlers; |
56 | } |
57 | } |
58 | } |
59 | |
60 | public LocalPredicatedInferenceNode( |
61 | LocalPredicatedInferenceNode<E, P, C, S> parent, |
62 | LocalPredicateChainingMatcher<E,P,C,S> matcher, |
63 | Predicate<E, C> predicate, |
64 | C context, |
65 | Long id) { |
66 | super(parent, matcher, predicate, context, id); |
67 | } |
68 | |
69 | private List<PredicatedInferenceNode<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>> falseChildren = new ArrayList<PredicatedInferenceNode<E,P,C,Long, AbstractEventBus.Handle<E, P, C, Long>, S>>(); |
70 | private List<EventHandlerWrapper<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>> falseHandlers = new ArrayList<EventHandlerWrapper<E,P,C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>>(); |
71 | private List<PredicatedInferenceNode<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>> trueChildren = new ArrayList<PredicatedInferenceNode<E,P,C,Long, AbstractEventBus.Handle<E, P, C, Long>, S>>(); |
72 | private List<EventHandlerWrapper<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>> trueHandlers = new ArrayList<EventHandlerWrapper<E,P,C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>>(); |
73 | |
74 | @Override |
75 | protected Callable<Collection<EventHandlerWrapper<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>>> createCollectorTask( |
76 | Map<C, Map<Extractor<E, ? super Boolean, C>, ? super Boolean>> cache, |
77 | ExecutorService executor, |
78 | AtomicReference<Collection<Future<Collection<EventHandlerWrapper<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>>>>> collector, |
79 | E event) { |
80 | |
81 | return new CollectorTask(event, collector, executor, cache); |
82 | } |
83 | |
84 | @Override |
85 | protected List<PredicatedInferenceNode<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>> getFalseChildren() { |
86 | return falseChildren; |
87 | } |
88 | |
89 | @Override |
90 | protected List<EventHandlerWrapper<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>> getFalseHandlers() { |
91 | return falseHandlers; |
92 | } |
93 | |
94 | @Override |
95 | protected List<PredicatedInferenceNode<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>> getTrueChildren() { |
96 | return trueChildren; |
97 | } |
98 | |
99 | @Override |
100 | protected List<EventHandlerWrapper<E, P, C, Long, AbstractEventBus.Handle<E, P, C, Long>, S>> getTrueHandlers() { |
101 | return trueHandlers; |
102 | } |
103 | |
104 | @Override |
105 | public String toString() { |
106 | return "LocalPredicatedInferenceNode [hashCode()=" +Integer.toHexString(hashCode())+"]"; //+ " getPredicate()=" + getPredicate() + "]"; |
107 | } |
108 | |
109 | |
110 | } |