/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jdbi.v3.sqlobject;

import static java.util.stream.Collectors.toList;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.ToIntFunction;
import java.util.stream.Stream;

/**
 * Applies decorations to method handlers, according to any {@link SqlMethodDecoratingAnnotation decorating annotations}
 * present on the method. If multiple decorating annotations are present, the order of application can be controlled
 * using the {@link DecoratorOrder} annotation.
 * <p>
 * This decorator is registered out of the box.
 * </p>
 */
public class SqlMethodAnnotatedHandlerDecorator implements HandlerDecorator {
    @Override
    public Handler decorateHandler(Handler base, Class<?> sqlObjectType, Method method) {
        Handler handler = base;

        List<Class<? extends Annotation>> annotationTypes = Stream.of(method.getAnnotations())
                .map(Annotation::annotationType)
                .filter(type -> type.isAnnotationPresent(SqlMethodDecoratingAnnotation.class))
                .collect(toList());

        Stream.of(method, sqlObjectType)
                .map(e -> e.getAnnotation(DecoratorOrder.class))
                .filter(Objects::nonNull)
                .findFirst()
                .ifPresent(order -> {
                    annotationTypes.sort(createDecoratorComparator(order).reversed());
                });

        List<HandlerDecorator> decorators = annotationTypes.stream()
                .map(type -> type.getAnnotation(SqlMethodDecoratingAnnotation.class))
                .map(a -> buildDecorator(a.value()))
                .collect(toList());

        for (HandlerDecorator decorator : decorators) {
            handler = decorator.decorateHandler(handler, sqlObjectType, method);
        }

        return handler;
    }

    private Comparator<Class<? extends Annotation>> createDecoratorComparator(DecoratorOrder order) {
        List<Class<? extends Annotation>> ordering = Arrays.asList(order.value());

        ToIntFunction<Class<? extends Annotation>> indexOf = type -> {
            int index = ordering.indexOf(type);
            return index == -1 ? ordering.size() : index;
        };

        return Comparator.comparingInt(indexOf::applyAsInt);
    }

    private static HandlerDecorator buildDecorator(Class<? extends HandlerDecorator> decoratorClass) {
        HandlerDecorator decorator;
        try {
            decorator = decoratorClass.getConstructor().newInstance();
        } catch (ReflectiveOperationException e) {
            throw new IllegalStateException("Decorator class " + decoratorClass + "cannot be instantiated", e);
        }
        return decorator;
    }

}

@@ -1948,30 +1948,8 @@
 r -%3E
- %7B%0A                   
  ann
@@ -2014,27 +2014,8 @@
 d())
-;%0A                %7D
 );%0A%0A
@@ -2636,61 +2636,39 @@
     
-ToIntFunction%3CClass%3C? extends Annotation%3E%3E indexOf = 
+return Comparator.comparingInt(
 type
@@ -2792,69 +2792,8 @@
    %7D
-;%0A%0A        return Comparator.comparingInt(indexOf::applyAsInt
 );%0A 
@@ -2905,44 +2905,8 @@
 ) %7B%0A
-        HandlerDecorator decorator;%0A
     
@@ -2931,19 +2931,14 @@
     
-decorator =
+return
  dec
@@ -3158,34 +3158,8 @@
   %7D%0A
-        return decorator;%0A
     

Simplify code based on code review comments.