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

com.opencredo.concursus.mapping.reflection.MethodSelectors Maven / Gradle / Ivy

The newest version!
package com.opencredo.concursus.mapping.reflection;

import com.opencredo.concursus.domain.time.StreamTimestamp;
import com.opencredo.concursus.mapping.annotations.HandlesEvent;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.function.Predicate;

public final class MethodSelectors {

    private MethodSelectors() {
    }

    private static Predicate isAnnotatedWith(Class annotation) {
        return method -> method.isAnnotationPresent(annotation);
    }

    private static final Predicate handlesEvent = isAnnotatedWith(HandlesEvent.class);

    private static Predicate returnsType(Class returnType) {
        return method -> method.getReturnType().equals(returnType);
    }

    private static final Predicate returnsVoid = returnsType(void.class);

    private static final Predicate isStatic = method -> Modifier.isStatic(method.getModifiers());

    private static final Predicate isInstance = isStatic.negate();

    private static Predicate parameterCountGte(int minParameterCount) {
        return method -> method.getParameterCount() >= minParameterCount;
    }

    private static Predicate hasParameterType(int index, Class expected) {
        return method -> method.getParameterTypes()[index].equals(expected);
    }

    private static Predicate firstParameterIs(Class expected) {
        return hasParameterType(0, expected);
    }

    private static Predicate secondParameterIs(Class expected) {
        return hasParameterType(1, expected);
    }

    private static Predicate hasTimestampAndIdParameters = parameterCountGte(2)
            .and(firstParameterIs(StreamTimestamp.class))
            .and(secondParameterIs(String.class));

    public static final Predicate isEventEmittingMethod =
            returnsVoid.and(isInstance).and(hasTimestampAndIdParameters);

    public static final Predicate isFactoryMethod = handlesEvent
            .and(isStatic)
            .and(parameterCountGte(1))
            .and(firstParameterIs(String.class));

    public static final Predicate isUpdateMethod = handlesEvent.and(isInstance).and(returnsVoid);

    public static final Predicate isCommandIssuingMethod =
            isInstance.and(hasTimestampAndIdParameters);

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy