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

io.vertx.up.uca.rs.mime.parse.EpsilonIncome Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package io.vertx.up.uca.rs.mime.parse;

import io.vertx.ext.web.RoutingContext;
import io.vertx.up.atom.Epsilon;
import io.vertx.up.atom.agent.Event;
import io.vertx.up.eon.ID;
import io.vertx.up.eon.Values;
import io.vertx.up.exception.WebException;
import io.vertx.up.fn.Fn;
import io.vertx.up.log.Annal;
import io.vertx.up.runtime.ZeroSerializer;
import io.vertx.up.uca.rs.Filler;
import io.vertx.up.util.Ut;

import javax.ws.rs.DefaultValue;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Help to extract epsilon
 */
public class EpsilonIncome implements Income>> {

    private static final Annal LOGGER = Annal.get(EpsilonIncome.class);

    private transient final Atomic atomic
            = Ut.singleton(MimeAtomic.class);

    @Override
    public List> in(final RoutingContext context,
                                    final Event event)
            throws WebException {
        final Method method = event.getAction();
        final Class[] paramTypes = method.getParameterTypes();
        final Annotation[][] annoTypes = method.getParameterAnnotations();
        final List> args = new ArrayList<>();
        for (int idx = 0; idx < paramTypes.length; idx++) {

            /* For each field specification **/
            final Epsilon epsilon = new Epsilon<>();
            epsilon.setArgType(paramTypes[idx]);
            epsilon.setAnnotation(getAnnotation(annoTypes[idx]));
            epsilon.setName(getName(epsilon.getAnnotation()));

            /* Default Value **/
            epsilon.setDefaultValue(getDefault(annoTypes[idx], epsilon.getArgType()));

            /* Epsilon income -> outcome **/
            final Epsilon outcome =
                    atomic.ingest(context, epsilon);
            args.add(Fn.getNull(() -> outcome, outcome));
        }
        return args;
    }

    @SuppressWarnings("all")
    private String getName(final Annotation annotation) {
        return Fn.getSemi(null == annotation, LOGGER,
                () -> ID.IGNORE,
                () -> Fn.getSemi(!Filler.NO_VALUE.contains(annotation.annotationType()),
                        LOGGER,
                        () -> Ut.invoke(annotation, "value"),
                        () -> ID.DIRECT));
    }

    private Annotation getAnnotation(final Annotation[] annotations) {
        final List annotationList = Arrays.stream(annotations)
                .filter(item -> Filler.PARAMS.containsKey(item.annotationType()))
                .collect(Collectors.toList());
        return annotationList.isEmpty() ? null : annotationList.get(Values.IDX);
    }

    private Object getDefault(final Annotation[] annotations,
                              final Class paramType) {
        final List annotationList = Arrays.stream(annotations)
                .filter(item -> item.annotationType() == DefaultValue.class)
                .collect(Collectors.toList());
        return Fn.getSemi(annotationList.isEmpty(), LOGGER,
                () -> null,
                () -> {
                    final Annotation annotation = annotationList.get(Values.IDX);
                    return ZeroSerializer.getValue(paramType,
                            Ut.invoke(annotation, "value"));
                });
    }
}