All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.datalking.web.support.HandlerMethodArgumentResolverComposite Maven / Gradle / Ivy

The newest version!
package com.github.datalking.web.support;

import com.github.datalking.common.MethodParameter;
import com.github.datalking.util.Assert;
import com.github.datalking.web.bind.WebDataBinderFactory;
import com.github.datalking.web.context.request.WebRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yaoo on 4/29/18
 */
public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgumentResolver {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    private final List argumentResolvers = new LinkedList<>();

    private final Map argumentResolverCache = new ConcurrentHashMap<>(256);

    public List getResolvers() {
        return Collections.unmodifiableList(this.argumentResolvers);
    }

    public boolean supportsParameter(MethodParameter parameter) {
        return getArgumentResolver(parameter) != null;
    }

    public Object resolveArgument(MethodParameter parameter,
                                  ModelAndViewContainer mavContainer,
                                  WebRequest webRequest,
                                  WebDataBinderFactory binderFactory) throws Exception {

        HandlerMethodArgumentResolver resolver = getArgumentResolver(parameter);
        Assert.notNull(resolver, "Unknown parameter type [" + parameter.getParameterType().getName() + "]");

        return resolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
    }


    private HandlerMethodArgumentResolver getArgumentResolver(MethodParameter parameter) {

        HandlerMethodArgumentResolver result = this.argumentResolverCache.get(parameter);

        if (result == null) {
            for (HandlerMethodArgumentResolver methodArgumentResolver : this.argumentResolvers) {

                if (methodArgumentResolver.supportsParameter(parameter)) {
                    result = methodArgumentResolver;
                    this.argumentResolverCache.put(parameter, result);
                    break;
                }
            }
        }

        return result;
    }

    public HandlerMethodArgumentResolverComposite addResolver(HandlerMethodArgumentResolver argumentResolver) {
        this.argumentResolvers.add(argumentResolver);
        return this;
    }

    public HandlerMethodArgumentResolverComposite addResolvers(List argumentResolvers) {

        if (argumentResolvers != null) {

            for (HandlerMethodArgumentResolver resolver : argumentResolvers) {

                this.argumentResolvers.add(resolver);
            }
        }
        return this;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy