1 | package com.hammurapi.eventbus.tests; |
2 | |
3 | import java.util.HashSet; |
4 | import java.util.Set; |
5 | |
6 | import com.hammurapi.eventbus.AbstractEventBus; |
7 | import com.hammurapi.eventbus.AbstractEventBus.Handle; |
8 | import com.hammurapi.eventbus.AbstractEventHandler; |
9 | import com.hammurapi.eventbus.EventDispatchContext; |
10 | import com.hammurapi.eventbus.EventHandler; |
11 | import com.hammurapi.eventbus.EventHandlerBase.Mode; |
12 | import com.hammurapi.eventbus.JavaBinder; |
13 | import com.hammurapi.eventbus.local.LocalEventBus; |
14 | import com.hammurapi.eventbus.local.LocalEventDispatchJoinContext; |
15 | import com.hammurapi.eventbus.local.LocalEventStore; |
16 | import com.hammurapi.extract.Predicate; |
17 | |
18 | public class TemplateJavaBinder implements JavaBinder<Object, Object, Long, AbstractEventBus.Handle<Object, Integer, Object, Long>, LocalEventStore<Object,Integer,Object>, LocalEventBus<Object, Integer, Object>, JoinHandler> { |
19 | |
20 | @Override |
21 | public Set<Long> bind(JoinHandler instance, LocalEventBus<Object,Integer,Object> bus) { |
22 | Set<Long> ret = new HashSet<Long>(); |
23 | |
24 | ret.add(bindHandleHello(instance, bus)); |
25 | ret.add(bindJoin(instance, bus)); |
26 | |
27 | return ret; |
28 | } |
29 | |
30 | private Long bindHandleHello(final Object instance, LocalEventBus<Object, Integer, Object> bus) { |
31 | |
32 | Predicate<Object, Object>[] predicates = new Predicate[1]; |
33 | |
34 | predicates[0] = null; |
35 | |
36 | boolean oneOff = false; |
37 | |
38 | Mode mode = Mode.POST; |
39 | EventHandler<Object, Integer, Object, Handle<Object, Integer, Object, Long>, LocalEventStore<Object, Integer, Object>> eventHandler = new AbstractEventHandler<Object, Integer, Object, AbstractEventBus.Handle<Object,Integer,Object,Long>, LocalEventStore<Object,Integer,Object>>(1, 0, instance, false, oneOff, mode , predicates) { |
40 | |
41 | @Override |
42 | public void post(EventDispatchContext<Object, Integer, Object, Handle<Object, Integer, Object, Long>, LocalEventStore<Object, Integer, Object>> context, Object... events) { |
43 | ((JoinHandler) instance).handleHello((String) events[0]); |
44 | } |
45 | }; |
46 | |
47 | return bus.addHandler(eventHandler); |
48 | |
49 | } |
50 | |
51 | private Long bindJoin(final Object instance, LocalEventBus<Object, Integer, Object> bus) { |
52 | |
53 | Predicate<Object, Object>[] predicates = new Predicate[1]; |
54 | |
55 | predicates[0] = null; |
56 | boolean oneOff = false; |
57 | |
58 | EventHandler<Object, Integer, Object, Handle<Object, Integer, Object, Long>, LocalEventStore<Object, Integer, Object>> eventHandler = new AbstractEventHandler<Object, Integer, Object, AbstractEventBus.Handle<Object,Integer,Object,Long>, LocalEventStore<Object,Integer,Object>>(3, 0, instance, false, oneOff, Mode.POST, predicates) { |
59 | |
60 | @Override |
61 | public void post(EventDispatchContext<Object, Integer, Object, Handle<Object, Integer, Object, Long>, LocalEventStore<Object, Integer, Object>> context, Object... events) { |
62 | String arg0 = (String) events[0]; |
63 | String arg1 = (String) events[1]; |
64 | Integer arg2 = (Integer) events[2]; |
65 | ((JoinHandler) instance).join(wrap(context), arg0, arg1, arg2); |
66 | } |
67 | |
68 | private LocalEventDispatchJoinContext<Object, Integer, Object> wrap(final EventDispatchContext<Object, Integer, Object, Handle<Object, Integer, Object, Long>, LocalEventStore<Object, Integer, Object>> context) { |
69 | return new LocalEventDispatchJoinContext<Object, Integer, Object>() { |
70 | |
71 | @Override |
72 | public void post(Object event, Predicate<Object, LocalEventStore<Object, Integer, Object>>... validators) { |
73 | context.post(event, validators); |
74 | } |
75 | |
76 | @Override |
77 | public void consume(int index) { |
78 | context.consume(index); |
79 | } |
80 | |
81 | @Override |
82 | public void consume(Object event) { |
83 | context.consume(event); |
84 | } |
85 | |
86 | @Override |
87 | public void update(Object event) { |
88 | context.update(event); |
89 | } |
90 | |
91 | @Override |
92 | public void removeHandler() { |
93 | context.removeHandler(); |
94 | } |
95 | |
96 | @Override |
97 | public LocalEventStore<Object, Integer, Object> getEventStore() { |
98 | return context.getEventStore(); |
99 | } |
100 | |
101 | @Override |
102 | public void consumeJoin(int index) { |
103 | context.consume(index); |
104 | } |
105 | |
106 | @Override |
107 | public void consumeJoin(Object event) { |
108 | context.consume(event); |
109 | } |
110 | |
111 | @Override |
112 | public com.hammurapi.eventbus.EventHandlerBase.Mode getMode() { |
113 | return context.getMode(); |
114 | } |
115 | |
116 | }; |
117 | } |
118 | |
119 | }; |
120 | |
121 | return bus.addHandler(eventHandler); |
122 | |
123 | } |
124 | |
125 | |
126 | } |