001    package com.hammurapi.eventbus.tests;
002    
003    import java.util.HashSet;
004    import java.util.Set;
005    
006    import com.hammurapi.eventbus.AbstractEventBus;
007    import com.hammurapi.eventbus.AbstractEventBus.Handle;
008    import com.hammurapi.eventbus.AbstractEventHandler;
009    import com.hammurapi.eventbus.EventDispatchContext;
010    import com.hammurapi.eventbus.EventHandler;
011    import com.hammurapi.eventbus.EventHandlerBase.Mode;
012    import com.hammurapi.eventbus.JavaBinder;
013    import com.hammurapi.eventbus.local.LocalEventBus;
014    import com.hammurapi.eventbus.local.LocalEventDispatchJoinContext;
015    import com.hammurapi.eventbus.local.LocalEventStore;
016    import com.hammurapi.extract.Predicate;
017    
018    public class TemplateJavaBinder implements JavaBinder<Object, Object, Long, AbstractEventBus.Handle<Object, Integer, Object, Long>, LocalEventStore<Object,Integer,Object>, LocalEventBus<Object, Integer, Object>, JoinHandler> {
019    
020            @Override
021            public Set<Long> bind(JoinHandler instance, LocalEventBus<Object,Integer,Object> bus) {
022                    Set<Long> ret = new HashSet<Long>();
023                    
024                    ret.add(bindHandleHello(instance, bus));
025                    ret.add(bindJoin(instance, bus));
026                    
027                    return ret;
028            }
029            
030            private Long bindHandleHello(final Object instance, LocalEventBus<Object, Integer, Object> bus) {
031                    
032                    Predicate<Object, Object>[] predicates = new Predicate[1];
033                    
034                    predicates[0] = null;
035                    
036                    boolean oneOff = false;
037                    
038                    Mode mode = Mode.POST;
039                    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) {
040    
041                            @Override
042                            public void post(EventDispatchContext<Object, Integer, Object, Handle<Object, Integer, Object, Long>, LocalEventStore<Object, Integer, Object>> context, Object... events) {
043                                    ((JoinHandler) instance).handleHello((String) events[0]);
044                            }
045                    };
046                    
047                    return bus.addHandler(eventHandler);
048                    
049            }
050            
051            private Long bindJoin(final Object instance, LocalEventBus<Object, Integer, Object> bus) {
052                    
053                    Predicate<Object, Object>[] predicates = new Predicate[1];
054                    
055                    predicates[0] = null;
056                    boolean oneOff = false;
057                    
058                    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) {
059    
060                            @Override
061                            public void post(EventDispatchContext<Object, Integer, Object, Handle<Object, Integer, Object, Long>, LocalEventStore<Object, Integer, Object>> context, Object... events) {
062                                    String arg0 = (String) events[0];
063                                    String arg1 = (String) events[1];
064                                    Integer arg2 = (Integer) events[2];
065                                    ((JoinHandler) instance).join(wrap(context), arg0, arg1, arg2);
066                            }
067    
068                            private LocalEventDispatchJoinContext<Object, Integer, Object> wrap(final EventDispatchContext<Object, Integer, Object, Handle<Object, Integer, Object, Long>, LocalEventStore<Object, Integer, Object>> context) {
069                                    return new LocalEventDispatchJoinContext<Object, Integer, Object>() {
070    
071                                            @Override
072                                            public void post(Object event, Predicate<Object, LocalEventStore<Object, Integer, Object>>... validators) {
073                                                    context.post(event, validators);
074                                            }
075    
076                                            @Override
077                                            public void consume(int index) {
078                                                    context.consume(index);
079                                            }
080    
081                                            @Override
082                                            public void consume(Object event) {
083                                                    context.consume(event);
084                                            }
085    
086                                            @Override
087                                            public void update(Object event) {
088                                                    context.update(event);
089                                            }
090    
091                                            @Override
092                                            public void removeHandler() {
093                                                    context.removeHandler();
094                                            }
095    
096                                            @Override
097                                            public LocalEventStore<Object, Integer, Object> getEventStore() {
098                                                    return context.getEventStore();
099                                            }
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    }