@Test @SuppressWarnings("unchecked") public void test() { Map<Class<?>, Callback<?>> map = new HashMap<>(); map.put(Marker.class, new CallbackImpl<>(Marker.class)); ((Callback) FindHandlerUtil.findByClass(Marker.class, map)).call(new OneType()); ((Callback) FindHandlerUtil.findByClass(Marker.class, map)).call(new SecondType()); ((Callback) FindHandlerUtil.findByClass(SecondType.class, map)).call(new OneType()); ((Callback) FindHandlerUtil.findByClass(SecondType.class, map)).call(new SecondType()); }
public static <T, H> H findByClass(Class<T> clazz, Map<? extends Class<?>, ? extends H> map) { Class<?> c = clazz; List<Class<?>> ifaces = new ArrayList<>(); while(c != null) { H handler = map.get(c); if(handler != null) { return handler; } ifaces.addAll(Arrays.asList(c.getInterfaces())); c = c.getSuperclass(); } for(Class<?> iface: ifaces) { while(iface != null) { H handler = map.get(iface); if(handler != null) { return handler; } iface = iface.getSuperclass(); } } return null; }
FindHandlerUtil { public static <T, H> H findByClass(Class<T> clazz, Map<? extends Class<?>, ? extends H> map) { Class<?> c = clazz; List<Class<?>> ifaces = new ArrayList<>(); while(c != null) { H handler = map.get(c); if(handler != null) { return handler; } ifaces.addAll(Arrays.asList(c.getInterfaces())); c = c.getSuperclass(); } for(Class<?> iface: ifaces) { while(iface != null) { H handler = map.get(iface); if(handler != null) { return handler; } iface = iface.getSuperclass(); } } return null; } }
FindHandlerUtil { public static <T, H> H findByClass(Class<T> clazz, Map<? extends Class<?>, ? extends H> map) { Class<?> c = clazz; List<Class<?>> ifaces = new ArrayList<>(); while(c != null) { H handler = map.get(c); if(handler != null) { return handler; } ifaces.addAll(Arrays.asList(c.getInterfaces())); c = c.getSuperclass(); } for(Class<?> iface: ifaces) { while(iface != null) { H handler = map.get(iface); if(handler != null) { return handler; } iface = iface.getSuperclass(); } } return null; } private  FindHandlerUtil(); }
FindHandlerUtil { public static <T, H> H findByClass(Class<T> clazz, Map<? extends Class<?>, ? extends H> map) { Class<?> c = clazz; List<Class<?>> ifaces = new ArrayList<>(); while(c != null) { H handler = map.get(c); if(handler != null) { return handler; } ifaces.addAll(Arrays.asList(c.getInterfaces())); c = c.getSuperclass(); } for(Class<?> iface: ifaces) { while(iface != null) { H handler = map.get(iface); if(handler != null) { return handler; } iface = iface.getSuperclass(); } } return null; } private  FindHandlerUtil(); static H findByClass(Class<T> clazz, Map<? extends Class<?>, ? extends H> map); }
FindHandlerUtil { public static <T, H> H findByClass(Class<T> clazz, Map<? extends Class<?>, ? extends H> map) { Class<?> c = clazz; List<Class<?>> ifaces = new ArrayList<>(); while(c != null) { H handler = map.get(c); if(handler != null) { return handler; } ifaces.addAll(Arrays.asList(c.getInterfaces())); c = c.getSuperclass(); } for(Class<?> iface: ifaces) { while(iface != null) { H handler = map.get(iface); if(handler != null) { return handler; } iface = iface.getSuperclass(); } } return null; } private  FindHandlerUtil(); static H findByClass(Class<T> clazz, Map<? extends Class<?>, ? extends H> map);  }