class RestExceptionHandlerRegistry {

    public Optional<ExceptionHandlerBinding> findBindingFor(Class<? extends Throwable> exceptionClass) {
        if (exceptionHandlerBindings == null || exceptionHandlerBindings.isEmpty()) {
            return Optional.empty();
        }
        int currentDistance = Integer.MAX_VALUE;
        ExceptionHandlerBinding closestBinding = null;
        for (ExceptionHandlerBinding binding : exceptionHandlerBindings) {
            int tempDistance = getDistanceBetweenExceptions(exceptionClass, binding.getExceptionClass());
            if (tempDistance < currentDistance) {
                currentDistance = tempDistance;
                closestBinding = binding;
                if (currentDistance == 0) {
                    break;
                }
            }
        }
        return Optional.ofNullable(closestBinding);
    }

}

class RestExceptionHandlerRegistryTest {

    private RestExceptionHandlerRegistry registry;

    @Test
    public void shouldReturnEmpty_IfBindingNotFound() throws Exception {

        ExceptionHandlerBinding binding = ExceptionHandlerBinding.fromAnnotatedClass(IllegalStateExceptionHandler.class);
        registry = new RestExceptionHandlerRegistry(Sets.newHashSet(binding));
        Optional<ExceptionHandlerBinding> foundBinding = registry.findBindingFor(IllegalArgumentException.class);
        assertThat(foundBinding).isEmpty();
    }

}