Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jmmo.observable.event;
  
  
  import java.util.List;

User: Tomas Date: 19.02.13 Time: 17:00
 
 public class Event {
 
     @SuppressWarnings("unchecked")
     public static <T1 extends ObservableEventObservableListener listener(Class<T1> class1Handler1<T1> handler1) {
         handler1.setClasses(class1);
         return handler1;
     }
 
     @SuppressWarnings("unchecked")
     public static <T1 extends ObservableEvent, T2 extends ObservableEventObservableListener listener(Class<T1> class1,
                                                                                                        Class<T2> class2Handler2<T1, T2> handler2) {
         handler2.setClasses(class1class2);
         return handler2;
     }
 
     @SuppressWarnings("unchecked")
     public static <T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent>
                   ObservableListener listener(Class<T1> class1Class<T2> class2Class<T3> class3Handler3<T1, T2, T3> handler3) {
         handler3.setClasses(class1class2class3);
         return handler3;
     }
 
     @SuppressWarnings("unchecked")
     public static <T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent, T4 extends ObservableEvent>
     ObservableListener listener(Class<T1> class1Class<T2> class2Class<T3> class3Class<T4> class4Handler4<T1, T2, T3, T4> handler4) {
         handler4.setClasses(class1class2class3class4);
         return handler4;
     }
 
     @SuppressWarnings("unchecked")
     public static <T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent,
             T4 extends ObservableEvent, T5 extends ObservableEventObservableListener listener(Class<T1> class1Class<T2> class2Class<T3> class3,
                                                                                                 Class<T4> class4Class<T5> class5Handler5<T1, T2, T3, T4, T5> handler5) {
         handler5.setClasses(class1class2class3class4class5);
         return handler5;
     }
 
     @SuppressWarnings("unchecked")
     public static <T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent,
             T4 extends ObservableEvent, T5 extends ObservableEvent, T6 extends ObservableEvent>
             ObservableListener listener(Class<T1> class1Class<T2> class2Class<T3> class3Class<T4> class4Class<T5> class5,
             Class<T6> class6Handler6<T1, T2, T3, T4, T5, T6> handler6) {
         handler6.setClasses(class1class2class3class4class5class6);
         return handler6;
     }
 
     @SuppressWarnings("unchecked")
     public static <T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent,
             T4 extends ObservableEvent, T5 extends ObservableEvent, T6 extends ObservableEvent, T7 extends ObservableEvent>
             ObservableListener listenerClass<T1> class1Class<T2> class2Class<T3> class3Class<T4> class4Class<T5> class5,
                                     Class<T6> class6Class<T7> class7Handler7<T1, T2, T3, T4, T5, T6, T7> handler7) {
         handler7.setClasses(class1class2class3class4class5class6class7);
         return handler7;
     }
 
     @SuppressWarnings("unchecked")
     public static <T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent, T4 extends ObservableEvent,
             T5 extends ObservableEvent, T6 extends ObservableEvent, T7 extends ObservableEvent, T8 extends ObservableEvent>
             ObservableListener listenerClass<T1> class1Class<T2> class2Class<T3> class3Class<T4> class4Class<T5> class5,
                                   Class<T6> class6Class<T7> class7Class<T8> class8Handler8<T1, T2, T3, T4, T5, T6, T7, T8> handler8) {
         handler8.setClasses(class1class2class3class4class5class6class7class8);
         return handler8;
     }
 
     @SuppressWarnings("unchecked")
     public static <T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent, T4 extends ObservableEvent,
             T5 extends ObservableEvent, T6 extends ObservableEvent, T7 extends ObservableEvent, T8 extends ObservableEvent, T9 extends ObservableEvent>
             ObservableListener listenerClass<T1> class1Class<T2> class2Class<T3> class3Class<T4> class4Class<T5> class5,Class<T6> class6,
                                           Class<T7> class7Class<T8> class8Class<T9> class9Handler9<T1, T2, T3, T4, T5, T6, T7, T8, T9> handler9) {
         handler9.setClasses(class1class2class3class4class5class6class7class8class9);
         return handler9;
     }
 
     protected interface HandlerIface1<T1> extends ObservableListener {
         void event1(T1 eventList<Observablechain);
     }
     protected static abstract class HandlerBase<T1> implements HandlerIface1<T1> {
         private Class<? extends ObservableEvent>[] classes;
 
         protected final void setClasses(Class<? extends ObservableEvent> ...classes) {
             if (this. != null) {
                 throw new IllegalStateException("Classes is always set to " + this);
             }
             this. = classes;
         }
 
         @Override
        public final void handleObservableEvent(ObservableEvent eventList<Observablechain) {
            for (int i = 0; i < .i++) {
                if ([i].isInstance(event)) {
                    handle(ieventchain);
                    break;
                }
            }
        }
        @SuppressWarnings("unchecked")
        protected final void handle(int indexObservableEvent eventList<Observablechain) {
            switch (index) {
                case 0: ((HandlerIface1this).event1(eventchain); break;
                case 1: ((HandlerIface2this).event2(eventchain); break;
                case 2: ((HandlerIface3this).event3(eventchain); break;
                case 3: ((HandlerIface4this).event4(eventchain); break;
                case 4: ((HandlerIface5this).event5(eventchain); break;
                case 5: ((HandlerIface6this).event6(eventchain); break;
                case 6: ((HandlerIface7this).event7(eventchain); break;
                case 7: ((HandlerIface8this).event8(eventchain); break;
                case 8: ((HandlerIface9this).event9(eventchain); break;
                defaultthrow new  IllegalArgumentException("No handler for index " + index);
            }
        }
        @Override
        public boolean filterObservable(Object observableList<Observablechain) {
            return true;
        }
    }
    public static abstract class Handler1<T1 extends ObservableEventextends HandlerBase<T1> {}
    protected interface HandlerIface2<T1, T2> extends HandlerIface1<T1> {
        void event2(T2 eventList<Observablechain);
    }
    public static abstract class Handler2<T1 extends ObservableEvent, T2 extends ObservableEventextends HandlerBase<T1> implements HandlerIface2<T1, T2> {}
    protected interface HandlerIface3<T1, T2, T3> extends HandlerIface2<T1, T2> {
        void event3(T3 eventList<Observablechain);
    }
    public static abstract class Handler3<T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent>
                                                     extends HandlerBase<T1> implements HandlerIface3<T1, T2, T3> {}
    protected interface HandlerIface4<T1, T2, T3, T4> extends HandlerIface3<T1, T2, T3> {
        void event4(T4 eventList<Observablechain);
    }
    public static abstract class Handler4<T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent,
                      T4 extends ObservableEventextends HandlerBase<T1> implements HandlerIface4<T1, T2, T3, T4> {}
    protected interface HandlerIface5<T1, T2, T3, T4, T5> extends HandlerIface4<T1, T2, T3, T4> {
        void event5(T5 eventList<Observablechain);
    }
    public static abstract class Handler5<T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent,
                 T4 extends ObservableEvent, T5 extends ObservableEventextends HandlerBase<T1> implements HandlerIface5<T1, T2, T3, T4, T5> {}
    protected interface HandlerIface6<T1, T2, T3, T4, T5, T6> extends HandlerIface5<T1, T2, T3, T4, T5> {
        void event6(T6 eventList<Observablechain);
    }
    public static abstract class Handler6<T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent,
            T4 extends ObservableEvent, T5 extends ObservableEvent, T6 extends ObservableEvent>
            extends HandlerBase<T1> implements HandlerIface6<T1, T2, T3, T4, T5, T6> {}
    protected interface HandlerIface7<T1, T2, T3, T4, T5, T6, T7> extends HandlerIface6<T1, T2, T3, T4, T5, T6> {
        void event7(T7 eventList<Observablechain);
    }
    public static abstract class Handler7<T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent,
            T4 extends ObservableEvent, T5 extends ObservableEvent, T6 extends ObservableEvent, T7 extends ObservableEvent>
            extends HandlerBase<T1> implements HandlerIface7<T1, T2, T3, T4, T5, T6, T7> {}
    protected interface HandlerIface8<T1, T2, T3, T4, T5, T6, T7, T8> extends HandlerIface7<T1, T2, T3, T4, T5, T6, T7> {
        void event8(T8 eventList<Observablechain);
    }
    public static abstract class Handler8<T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent,
            T4 extends ObservableEvent, T5 extends ObservableEvent, T6 extends ObservableEvent, T7 extends ObservableEvent, T8 extends ObservableEvent>
            extends HandlerBase<T1> implements HandlerIface8<T1, T2, T3, T4, T5, T6, T7, T8> {}
    protected interface HandlerIface9<T1, T2, T3, T4, T5, T6, T7, T8, T9> extends HandlerIface8<T1, T2, T3, T4, T5, T6, T7, T8> {
        void event9(T9 eventList<Observablechain);
    }
    public static abstract class Handler9<T1 extends ObservableEvent, T2 extends ObservableEvent, T3 extends ObservableEvent,
            T4 extends ObservableEvent, T5 extends ObservableEvent, T6 extends ObservableEvent, T7 extends ObservableEvent,
            T8 extends ObservableEvent, T9 extends ObservableEvent>
            extends HandlerBase<T1> implements HandlerIface9<T1, T2, T3, T4, T5, T6, T7, T8, T9> {}
    //Multi args listener
    public static ObservableListener listener(final ObservableFilter filterfinal ObservableHandler...handlers) {
        final List<Class<? extends ObservableEvent>> classes = new ArrayList<Class<? extends ObservableEvent>>(handlers.length);
        for (ObservableHandler handler : handlers) {
            classes.add(getEventClass(handler.getClass()));
        }
        return new ObservableListener() {
            @SuppressWarnings("unchecked")
            @Override
            public boolean filterObservable(Object observableList<Observablechain) {
                return filter.filterObservable(observablechain);
            }
            @SuppressWarnings("unchecked")
            @Override
            public void handleObservableEvent(ObservableEvent eventList<Observablechain) {
                for (int i = 0; i < handlers.lengthi++) {
                    if (classes.get(i).isInstance(event)) {
                        handlers[i].handleObservableEvent(eventchain);
                        break;
                    }
                }
            }
        };
    }
    public static ObservableListener listener(final ObservableHandler...handlers) {
        return listener(.handlers);
    }
    @SuppressWarnings("unchecked")
    protected static Class<? extends ObservableEventgetEventClass(Class<? extends ObservableHandlerclazz) {
        return ListenerBuilder.getTypeArgument(ObservableHandler.classclazz);
    }
New to GrepCode? Check out our FAQ X