ClassUtils { public static Option<Class<?>> selectMostRelated(Collection<? extends Class<?>> classes, Class<?> to) { Class<?> selected = null; for (Class<?> clazz : classes) { if (clazz.isAssignableFrom(to)) { if (selected != null && clazz.isAssignableFrom(selected)) { continue; } selected = clazz; } } return Option.of(selected); } private  ClassUtils(); static Class<?> getNonPrimitiveClass(Class<?> clazz); static Option<Class<?>> selectMostRelated(Collection<? extends Class<?>> classes, Class<?> to); static boolean isAssignableFrom(Class<?> toClass, @Nullable Object fromObject); static boolean isAssignableFrom(@Nullable Class<?> toClass, @Nullable Class<?> fromClass); static Class<?>[] getClasses(Object... values); static boolean exists(String name); @SuppressWarnings("unchecked") static Option<Class<? extends T>> forName(String name); static final Map<Class<?>, Class<?>> PRIMITIVE_EQUIVALENT; }
@Test void selectMostRelated() { Collection<Class<?>> classes = Arrays.asList(A.class, C.class); Assertions.assertEquals(A.class, ClassUtils.selectMostRelated(classes, A.class).getOrNull()); Assertions.assertEquals(A.class, ClassUtils.selectMostRelated(classes, B.class).getOrNull()); Assertions.assertEquals(C.class, ClassUtils.selectMostRelated(classes, C.class).getOrNull()); }