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

com.fastchar.core.FastConverters Maven / Gradle / Ivy

package com.fastchar.core;

import com.fastchar.annotation.AFastPriority;
import com.fastchar.asm.FastParameter;
import com.fastchar.interfaces.IFastParamConverter;
import com.fastchar.utils.FastClassUtils;

import java.lang.reflect.Modifier;
import java.util.*;

@SuppressWarnings("UnusedReturnValue")
public class FastConverters {
    private List> paramConverters = new ArrayList<>();

    public FastConverters add(Class converters) {
        if (!FastClassUtils.checkNewInstance(converters)) {
            return this;
        }
        paramConverters.add(converters);
        sortConverter();
        return this;
    }

    private void sortConverter() {
        Comparator> comparator = new Comparator>() {
            @Override
            public int compare(Class o1, Class o2) {
                int priority1 = 0;
                int priority2 = 0;

                if (o1.isAnnotationPresent(AFastPriority.class)) {
                    AFastPriority aFastPriority = o1.getAnnotation(AFastPriority.class);
                    priority1 = aFastPriority.value();
                }
                if (o2.isAnnotationPresent(AFastPriority.class)) {
                    AFastPriority aFastPriority = o2.getAnnotation(AFastPriority.class);
                    priority2 = aFastPriority.value();
                }
                return Integer.compare(priority2, priority1);
            }
        };
        Collections.sort(paramConverters, comparator);
    }



    public Object convertParam(FastAction action, FastParameter parameter) throws Exception {
        int[] marker = new int[1];
        Object value = null;
        for (Class paramConverter : paramConverters) {
            IFastParamConverter iFastParamConverter = FastClassUtils.newInstance(paramConverter);
            if (iFastParamConverter == null) {
                continue;
            }
            value = iFastParamConverter.convertValue(action, parameter, marker);
            if (marker[0] == 1) {
                break;
            }
        }
        return value;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy