Start line:  
End line:  

Snippet Preview

Snippet HTML Code

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

User: Tomas Date: 10.03.13 Time: 17:46
 
 public class ListenerBuilder {
     private final Map<Class<? extends ObservableEvent>, ObservableHandler<?>> classHandlerMap = new LinkedHashMap<Class<? extends ObservableEvent>, ObservableHandler<?>>();
     private final Map<Class<?>, ObservableFilter<?>> classFilterMap = new LinkedHashMap<Class<?>, ObservableFilter<?>>();
 
     @SuppressWarnings("unchecked")
     public <T extends ObservableEventListenerBuilder addHandler(ObservableHandler<T> handler) {
         return addHandler((Class<T>) getTypeArgument(ObservableHandler.classhandler), handler);
     }
 
     public <T extends ObservableEventListenerBuilder addHandler(Class<T> clazzObservableHandler<T> handler) {
         if (.containsKey(clazz)) {
             throw new IllegalArgumentException(clazz.getName() + " already handled by " + .get(clazz));
         }
 
         .put(clazzhandler);
         return this;
     }
 
     @SuppressWarnings("unchecked")
     public <T> ListenerBuilder addFilter(ObservableFilter<T> filter) {
         return addFilter((Class<T>) getTypeArgument(ObservableFilter.classfilter), filter);
     }
 
     public <T> ListenerBuilder addFilter(Class<T> clazzObservableFilter<T> filter) {
         if (.containsKey(clazz)) {
             throw new IllegalArgumentException(clazz.getName() + " already filtered by " + .get(clazz));
         }
 
         .put(clazzfilter);
         return this;
     }
 
     public ObservableListener build() {
         if (.isEmpty()) {
             throw new IllegalArgumentException("Must be at least one handler to build listener");
         }
 
         final List<Class<? extends ObservableEvent>> handlerClasses = new ArrayList<Class<? extends ObservableEvent>>(.keySet());
         final List<ObservableHandler<?>> handlers = new ArrayList<ObservableHandler<?>>(.values());
         final List<Class<?>> filterClasses = new ArrayList<Class<?>>(.keySet());
         final List<ObservableFilter<?>> filters = new ArrayList<ObservableFilter<?>>(.values());
 
         ObservableHandler handler;
         if (handlers.size() == 1) {
             handler = new SingleObservableHandler(handlerClasses.get(0), handlers.get(0));
         }
         else {
             handler = new MultiObservableHandler(handlerClasseshandlers);
         }
 
         ObservableFilter filter;
         if (filters.isEmpty()) {
             filter = .;
         }
         else if (.size() == 1) {
             filter = new SingleObservableFilter(filterClasses.get(0), filters.get(0));
         }
         else {
             filter = new MultiObservableFilter(filterClassesfilters);
         }
 
         return new ComplexObservableListener(handlerfilter);
     }
 
     private static class SingleObservableHandler implements ObservableHandler<ObservableEvent> {
         private final Class<? extends ObservableEventclazz;
         private final ObservableHandler<?> handler;
 
         private SingleObservableHandler(Class<? extends ObservableEventclazzObservableHandler<?> handler) {
             this. = clazz;
             this. = handler;
         }
 
         @SuppressWarnings("unchecked")
         @Override
         public void handleObservableEvent(ObservableEvent eventList<Observablechain) {
             if (.isInstance(event)) {
                 ((ObservableHandler).handleObservableEvent(eventchain);
             }
         }
     }
 
     private static class MultiObservableHandler implements ObservableHandler<ObservableEvent> {
         private final List<Class<? extends ObservableEvent>> classes;
         private final List<ObservableHandler<?>> handlers;
 
        private MultiObservableHandler(List<Class<? extends ObservableEvent>> classesList<ObservableHandler<?>> handlers) {
            this. = classes;
            this. = handlers;
        }
        @SuppressWarnings("unchecked")
        @Override
        public void handleObservableEvent(ObservableEvent eventList<Observablechain) {
            for (int i = 0; i < .size(); i++) {
                if (.get(i).isInstance(event)) {
                    ((ObservableHandler.get(i)).handleObservableEvent(eventchain);
                    break;
                }
            }
        }
    }
    private static class SingleObservableFilter implements ObservableFilter<Observable> {
        private final Class<?> clazz;
        private final ObservableFilter<?> filter;
        private SingleObservableFilter(Class<?> clazzObservableFilter<?> filter) {
            this. = clazz;
            this. = filter;
        }
        @SuppressWarnings("unchecked")
        @Override
        public boolean filterObservable(Observable observableList<Observablechain) {
            if (.isInstance(observable)) {
                return ((ObservableFilter).filterObservable(observablechain);
            }
            return false;
        }
    }
    private static class MultiObservableFilter implements ObservableFilter<Observable> {
        private final List<Class<?>> classes;
        private final List<ObservableFilter<?>> filters;
        private MultiObservableFilter(List<Class<?>> classesList<ObservableFilter<?>> filters) {
            this. = classes;
            this. = filters;
        }
        @SuppressWarnings("unchecked")
        @Override
        public boolean filterObservable(Observable observableList<Observablechain) {
            for (int i = 0; i < .size(); i++) {
                if (.get(i).isInstance(observable)) {
                    return ((ObservableFilter.get(i)).filterObservable(observablechain);
                }
            }
            return false;
        }
    }
    private static class ComplexObservableListener implements ObservableListener {
        private final ObservableHandler handler;
        private final ObservableFilter filter;
        private ComplexObservableListener(ObservableHandler handlerObservableFilter filter) {
            this. = handler;
            this. = filter;
        }
        @SuppressWarnings("unchecked")
        @Override
        public boolean filterObservable(Object observableList<Observablechain) {
            return .filterObservable(observablechain);
        }
        @SuppressWarnings("unchecked")
        @Override
        public void handleObservableEvent(ObservableEvent eventList<Observablechain) {
            .handleObservableEvent(eventchain);
        }
    }
    @SuppressWarnings("unchecked")
    protected static <R, T> Class<? extends R> getTypeArgument(Class<T> parameterClass, T generic) {
        return getTypeArgument(parameterClass, (Class<? extends T>) generic.getClass());
    }
    @SuppressWarnings("unchecked")
    protected static <R, T> Class<? extends R> getTypeArgument(Class<T> parameterClassClass<? extends T> clazz) {
        for (Type type : clazz.getGenericInterfaces()) {
            if (type instanceof ParameterizedType) {
                ParameterizedType ptype = (ParameterizedTypetype;
                if (parameterClass.isAssignableFrom((Class<?>) ptype.getRawType())) {
                    Type itype = ptype.getActualTypeArguments()[0];
                    Class<? extends R> pclass;
                    if (itype instanceof ParameterizedType) {
                        pclass = (Class<? extends R>) ((ParameterizedTypeitype).getRawType();
                    }
                    else {
                        pclass = (Class<? extends R>) itype;
                    }
                    return pclass;
                }
            }
        }
        throw new IllegalArgumentException(parameterClass + " class is not found in handler " + clazz);
    }
New to GrepCode? Check out our FAQ X