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

io.lettuce.core.dynamic.DeclaredCommandMethod Maven / Gradle / Ivy

Go to download

Advanced and thread-safe Java Redis client for synchronous, asynchronous, and reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs and much more.

The newest version!
package io.lettuce.core.dynamic;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;

import org.reactivestreams.Publisher;

import io.lettuce.core.dynamic.batch.BatchExecutor;
import io.lettuce.core.dynamic.parameter.ExecutionSpecificParameters;
import io.lettuce.core.dynamic.parameter.Parameter;
import io.lettuce.core.dynamic.parameter.Parameters;
import io.lettuce.core.dynamic.support.ResolvableType;
import io.lettuce.core.dynamic.support.TypeInformation;
import io.lettuce.core.internal.LettuceAssert;

/**
 * Abstraction of a method that is designated to execute a Redis command method. Enriches the standard {@link Method} interface
 * with specific information that is necessary to construct {@link io.lettuce.core.protocol.RedisCommand} for the method.
 *
 * @author Mark Paluch
 * @since 5.0
 */
public class DeclaredCommandMethod implements CommandMethod {

    private final Method method;

    private final ResolvableType returnType;

    private final List> arguments = new ArrayList<>();

    private final ExecutionSpecificParameters parameters;

    private final ResolvableType actualReturnType;

    private final boolean futureExecution;

    private final boolean reactiveExecution;

    /**
     * Create a new {@link DeclaredCommandMethod} given a {@link Method}.
     *
     * @param method must not be null.
     */
    private DeclaredCommandMethod(Method method) {
        this(method, new ExecutionSpecificParameters(method));
    }

    /**
     * Create a new {@link DeclaredCommandMethod} given a {@link Method} and {@link Parameters}.
     *
     * @param method must not be null.
     * @param parameters must not be null.
     */
    private DeclaredCommandMethod(Method method, ExecutionSpecificParameters parameters) {

        LettuceAssert.notNull(method, "Method must not be null");
        LettuceAssert.notNull(parameters, "Parameters must not be null");

        this.method = method;
        this.returnType = ResolvableType.forMethodReturnType(method);
        this.parameters = parameters;
        this.futureExecution = Future.class.isAssignableFrom(getReturnType().getRawClass());
        this.reactiveExecution = ReactiveTypes.supports(getReturnType().getRawClass());

        Collections.addAll(arguments, method.getParameterTypes());

        ResolvableType actualReturnType = this.returnType;

        while (Future.class.isAssignableFrom(actualReturnType.getRawClass())) {
            ResolvableType[] generics = actualReturnType.getGenerics();

            if (generics.length != 1) {
                break;
            }

            actualReturnType = generics[0];
        }

        this.actualReturnType = actualReturnType;
    }

    /**
     * Create a new {@link DeclaredCommandMethod} given a {@link Method}.
     *
     * @param method must not be null.
     */
    public static CommandMethod create(Method method) {
        return new DeclaredCommandMethod(method);
    }

    /**
     * @return the method {@link Parameters}.
     */
    @Override
    public Parameters getParameters() {
        return parameters;
    }

    /**
     * @return the {@link Method}.
     */
    @Override
    public Method getMethod() {
        return method;
    }

    /**
     * @return declared {@link Method} return {@link TypeInformation}.
     */
    @Override
    public ResolvableType getReturnType() {
        return returnType;
    }

    /**
     * @return the actual {@link Method} return {@link TypeInformation} after unwrapping.
     */
    @Override
    public ResolvableType getActualReturnType() {
        return actualReturnType;
    }

    /**
     * Lookup a method annotation.
     *
     * @param annotationClass the annotation class.
     * @return the annotation object or {@code null} if not found.
     */
    @Override
    public  A getAnnotation(Class annotationClass) {
        return method.getAnnotation(annotationClass);
    }

    /**
     * @param annotationClass the annotation class.
     * @return {@code true} if the method is annotated with {@code annotationClass}.
     */
    @Override
    public boolean hasAnnotation(Class annotationClass) {
        return method.getAnnotation(annotationClass) != null;
    }

    /**
     * @return the method name.
     */
    @Override
    public String getName() {
        return method.getName();
    }

    /**
     * @return {@code true} if the method uses asynchronous execution declaring {@link Future} as result type.
     */
    @Override
    public boolean isFutureExecution() {
        return futureExecution;
    }

    /**
     * @return {@code true} if the method uses reactive execution declaring {@link Publisher} as result type.
     */
    @Override
    public boolean isReactiveExecution() {
        return reactiveExecution;
    }

    /**
     * @return {@code true} if the method defines a {@link io.lettuce.core.dynamic.batch.CommandBatching} argument.
     */
    @Override
    public boolean isBatchExecution() {
        return parameters.hasCommandBatchingIndex()
                || (method.getName().equals("flush") && method.getDeclaringClass().equals(BatchExecutor.class));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (!(o instanceof DeclaredCommandMethod))
            return false;

        DeclaredCommandMethod that = (DeclaredCommandMethod) o;

        if (method != null ? !method.equals(that.method) : that.method != null)
            return false;
        if (returnType != null ? !returnType.equals(that.returnType) : that.returnType != null)
            return false;
        return arguments != null ? arguments.equals(that.arguments) : that.arguments == null;

    }

    @Override
    public int hashCode() {
        int result = method != null ? method.hashCode() : 0;
        result = 31 * result + (returnType != null ? returnType.hashCode() : 0);
        result = 31 * result + (arguments != null ? arguments.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return method.toGenericString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy